本文整理汇总了C#中Microsoft.AspNet.Identity.EntityFramework.IdentityManager.ClearUserRoles方法的典型用法代码示例。如果您正苦于以下问题:C# IdentityManager.ClearUserRoles方法的具体用法?C# IdentityManager.ClearUserRoles怎么用?C# IdentityManager.ClearUserRoles使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.AspNet.Identity.EntityFramework.IdentityManager
的用法示例。
在下文中一共展示了IdentityManager.ClearUserRoles方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UserRoles
public ActionResult UserRoles(SelectUserRolesViewModel model)
{
if (ModelState.IsValid)
{
var db = new ApplicationDbContext();
var user = db.Users.First(m => m.UserName == model.UserName);
var im = new IdentityManager();
im.ClearUserRoles(user.Id);
foreach (var role in model.Roles)
{
if (role.Selected)
{
im.AddUserToRole(user.Id, role.RoleName);
}
}
return RedirectToAction("Index");
}
return View();
}
示例2: UserRoles
public virtual ActionResult UserRoles(SelectUserRolesViewModel model)
{
if (ModelState.IsValid)
{
var idManager = new IdentityManager();
var Db = new ApplicationDbContext();
var user = Db.Users.First(u => u.UserName == model.UserName);
idManager.ClearUserRoles(user.Id);
foreach (var role in model.Roles)
{
if (role.Selected)
{
idManager.AddUserToRole(user.Id, role.RoleName);
}
}
return RedirectToAction("index");
}
return View();
}
示例3: Edit
public async Task<ActionResult> Edit([Bind(Include = "Id,UserName,HoVaTen,DonVi_ID,Email,TrangThai,RoleId")] EditUserViewModel ht_User)
{
if (S4T_HaTinhBase.GetUserSession() == null) return RedirectToAction("Login", "Account", new { returnUrl = Request.Url.PathAndQuery });
var per = S4T_HaTinhBase.CheckPermission(Request.RequestContext.RouteData.GetRequiredString("controller"));
if (per != PermissionType.Write) return Content(ExceptionViewer.GetMessage("UPDATE_NOT_PERMISSION"));
try
{
ModelState.Remove("UserName");
ModelState.Remove("DonVi_ID");
ModelState.Remove("RoleId");
var objUserOld = db.AspNetUsers.FirstOrDefault(o => o.Id == ht_User.Id);
if (objUserOld == null) return JavaScript("Không tìm thấy thông tin người dùng");
ht_User.DonVi_ID = objUserOld.DonVi_ID;
if (ModelState.IsValid)
{
objUserOld.HoVaTen = ht_User.HoVaTen;
objUserOld.Email = ht_User.Email;
objUserOld.TrangThai = ht_User.TrangThai;
db.Entry(objUserOld).State = EntityState.Modified;
var objUserRoleOld = db.AspNetUserRoles.FirstOrDefault(o => o.UserId == ht_User.Id);
if (objUserRoleOld != null && !objUserRoleOld.RoleId.Equals(ht_User.RoleId))
{
IdentityManager mana = new IdentityManager();
// Xóa role cũ cho User
mana.ClearUserRoles(ht_User.Id);
// Add role mới cho User
var roleName = db.AspNetRoles.FirstOrDefault(o => o.Id == ht_User.RoleId).Name;
mana.AddUserToRole(ht_User.Id, roleName);
}
await db.SaveChangesAsync();
}
else
{
if (ht_User.DonVi_ID == DonVi.SoThongTinTruyenThong){
GetViewBag(NhomDoiTuong.SoTTTT);
}
else
GetViewBag(NhomDoiTuong.DonVi);
return View(ht_User);
}
}
catch (DbEntityValidationException ex)
{
var sb = new StringBuilder();
foreach (var failure in ex.EntityValidationErrors)
{
sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
foreach (var error in failure.ValidationErrors)
{
sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
sb.AppendLine();
}
}
throw new DbEntityValidationException(
"Entity Validation Failed - errors follow:\n" +
sb.ToString(), ex
); // Add the original exception as the innerException
}
if (ht_User.DonVi_ID == DonVi.SoThongTinTruyenThong)
return RedirectToAction("ListChuyenVienSo");
else
return RedirectToAction("ListUser");
}
示例4: UserRoles
public ActionResult UserRoles(SelectUserRolesViewModel model)
{
if (ModelState.IsValid)
{
var idManager = new IdentityManager();
var Db = new ApplicationDbContext();
var user = Db.Users.First(u => u.UserName == model.UserName);
idManager.ClearUserRoles(user.Id);
var allRoles = Db.Roles;
if (!String.IsNullOrEmpty(model.WebRole))
{
//Try to get this
var newRole = allRoles.Where(r => r.Name == model.WebRole).FirstOrDefault();
if (newRole != null)
{
idManager.AddUserToRole(user.Id, newRole.Name);
}
}
if (!String.IsNullOrEmpty(model.GestorRole))
{
//Try to get this
var newRole = allRoles.Where(r => r.Name == model.GestorRole).FirstOrDefault();
if (newRole != null)
{
idManager.AddUserToRole(user.Id, newRole.Name);
}
}
return RedirectToAction("index");
}
return View(model);
}
示例5: UserRoles
public ActionResult UserRoles(SelectUserRolesViewModel model)
{
if (ModelState.IsValid)
{
using (var db = new ApplicationDbContext())
{
var idManager = new IdentityManager();
var user = db.Users.First(u => u.UserName == model.UserName);
idManager.ClearUserRoles(user.Id);
foreach (var role in model.Roles)
{
if (role.Selected)
idManager.AddUserToRole(user.Id, role.RoleName);
}
return RedirectToAction("Index");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}