本文整理汇总了C#中ApplicationUserManager.IsInRole方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationUserManager.IsInRole方法的具体用法?C# ApplicationUserManager.IsInRole怎么用?C# ApplicationUserManager.IsInRole使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ApplicationUserManager
的用法示例。
在下文中一共展示了ApplicationUserManager.IsInRole方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitializeAppEnvironment
private void InitializeAppEnvironment()
{
//app environment configuration
using (var db = new ApplicationDbContext())
{
db.Database.CreateIfNotExists();
var roleStore = new RoleStore<IdentityRole>(db);
var role = roleStore.FindByNameAsync("Admin").Result;
if (role == null)
{
roleStore.CreateAsync(new IdentityRole("Admin")).Wait();
}
var userStore = new UserStore<ApplicationUser>(db);
var manager = new ApplicationUserManager(userStore);
var admin = manager.FindByName("admin");
if (admin == null)
{
admin = new ApplicationUser
{
UserName = "admin",
Email = "[email protected]",
EmailConfirmed = true,
CreateDate = DateTime.Now
};
var r = manager.CreateAsync(admin, "~Pwd123456").Result;
}
if (!manager.IsInRole(admin.Id, role.Name))
{
manager.AddToRole(admin.Id, role.Name);
}
}
}
示例2: ChangeRole
public ActionResult ChangeRole( string id )
{
Person p = db.Users.Where( pp => id == pp.Id ).FirstOrDefault();
var userManager = new ApplicationUserManager( new UserStore<Person>( db ) );
if( userManager.IsInRole( p.Id, "editor" ) ) {
userManager.RemoveFromRole( p.Id, "editor" );
userManager.AddToRole( p.Id, "admin" );
} else if( userManager.IsInRole( p.Id, "admin" ) ) {
userManager.RemoveFromRole( p.Id, "admin" );
userManager.AddToRole( p.Id, "user" );
} else {
userManager.RemoveFromRole( p.Id, "user" );
userManager.AddToRole( p.Id, "editor" );
}
db.SaveChanges();
return RedirectToAction( "AdminTable" );
}
示例3: Post
public void Post(RoleUsersDto dto)
{
var context = ApplicationDbContext.Create();
var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
var roleManager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context));
if (!roleManager.RoleExists(dto.RoleName)) return;
foreach (
var user in
dto.UserNames.Select(userName => userManager.FindByName(userName))
.Where(user => user != null)
.Where(user => !userManager.IsInRole(user.Id, dto.RoleName)))
{
userManager.AddToRole(user.Id, dto.RoleName);
}
foreach (
var user in
dto.UserNames.Select(userName => userManager.FindByName(userName))
.Where(user => user != null)
.Where(user => userManager.IsInRole(user.Id, dto.RoleName)))
{
userManager.RemoveFromRole(user.Id, dto.RoleName);
}
}
示例4: DeleteRoleForUser
public ActionResult DeleteRoleForUser(string UserName, string RoleName)
{
try
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
if (manager.IsInRole(user.Id, RoleName))
{
manager.RemoveFromRole(user.Id, RoleName);
TempData["ValidationMessage"] = ("Success: " + " " + UserName + " " + "Was Removed From the " + " " + RoleName + " " + "Role");
}
else
{
TempData["ValidationMessage"] = ("Error: " + " " + UserName + " " + "Was Not Successfully Removed From the " + " " + RoleName + " " + "Role");
}
}
catch
{
TempData["ValidationMessage"] = ("Error: Something Went Wrong");
}
// prepopulat roles for the view dropdown
var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
ViewBag.Roles = list;
return View("ManageRole");
}
示例5: Edit
public ActionResult Edit([Bind(Include = "UserId,Roles,UserName,Email")] UserEdit userEdit)
{
if (userEdit.UserId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var user = _db.Users.Find(userEdit.UserId);
if (user == null)
{
return HttpNotFound();
}
if (!ModelState.IsValid) return View(userEdit);
_userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
foreach (var role in userEdit.Roles)
{
if (role.IsChecked && !_userManager.IsInRole(user.Id, role.Name))
_userManager.AddToRole(user.Id, role.Name);
if (!role.IsChecked && _userManager.IsInRole(user.Id, role.Name))
_userManager.RemoveFromRole(user.Id, role.Name);
}
_db.SaveChanges();
// This was intended to force roles to update without manual logout login, but for now it's more trouble than it's worth
/*var authenticationManager = HttpContext.GetOwinContext().Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = _userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity);*/
return RedirectToAction("Index");
}