本文整理汇总了C#中UserManager.IsInRole方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.IsInRole方法的具体用法?C# UserManager.IsInRole怎么用?C# UserManager.IsInRole使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.IsInRole方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddUserAndRole
internal void AddUserAndRole()
{
ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
RoleStore<IdentityRole> roleStore = new RoleStore<IdentityRole>(context);
RoleManager<IdentityRole> roleMgr = new RoleManager<IdentityRole>(roleStore);
//create admin role
if(!roleMgr.RoleExists("admin")) {
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "admin" });
}
//create master user
UserManager<ApplicationUser> userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
ApplicationUser appUser = new ApplicationUser {
UserName = "Adam",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Baseball1!");
//add to admin role
if(!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "admin")) {
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "admin");
}
}
示例2: Menu
public ActionResult Menu()
{
ApplicationDbContext userscontext = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(userscontext);
var userManager = new UserManager<ApplicationUser>(userStore);
var roleStore = new RoleStore<IdentityRole>(userscontext);
var roleManager = new RoleManager<IdentityRole>(roleStore);
if (User.Identity.IsAuthenticated)
{
if (userManager.IsInRole(this.User.Identity.GetUserId(), "Admin"))
{
return PartialView("_AdminMenuView");
}
else if (userManager.IsInRole(this.User.Identity.GetUserId(), "Principal"))
{
return PartialView("_PrincipalenuView");
}
else
{
return PartialView("_Student");
}
}
return PartialView("_Empty");
}
示例3: Details
// GET: AspNetUsers/Details/5
public async Task<ActionResult> Details(string id)
{
AspNetUserDetailsViewModel mdl = new AspNetUserDetailsViewModel();
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
mdl.User = await db.AspNetUsers.FindAsync(id);
if(mdl.User == null)
{
return HttpNotFound();
}
mdl.Roles = await db.AspNetRoles.ToListAsync();
var ctx = new ApplicationDbContext();
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(ctx));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ctx));
if(userManager.IsInRole(mdl.User.Id, "NetUser"))
{
mdl.RoleId = RoleManager.FindByName("NetUser")?.Id;
}
if(userManager.IsInRole(mdl.User.Id, "Admin"))
{
mdl.RoleId = RoleManager.FindByName("Admin")?.Id;
}
return View(mdl);
}
示例4: AddPermisionToADM
private void AddPermisionToADM(ApplicationDbContext db)
{
var userManarge = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
var user = userManarge.FindByName("[email protected]");
if (!userManarge.IsInRole(user.Id, "View"))
{
userManarge.AddToRole(user.Id, "View");
}
if (!userManarge.IsInRole(user.Id, "Create"))
{
userManarge.AddToRole(user.Id, "Create");
}
if (!userManarge.IsInRole(user.Id, "Edit"))
{
userManarge.AddToRole(user.Id, "Edit");
}
if (!userManarge.IsInRole(user.Id, "Delete"))
{
userManarge.AddToRole(user.Id, "Delete");
}
if (!userManarge.IsInRole(user.Id, "Adm"))
{
userManarge.AddToRole(user.Id, "Adm");
}
}
示例5: InitializeRoles
/// <summary>
/// Checks for the three roles - Admin, Employee and Complainant and
/// creates them if not present
/// </summary>
public static void InitializeRoles()
{ // Access the application context and create result variables.
ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdUserResult;
// Create a RoleStore object by using the ApplicationDbContext object.
// The RoleStore is only allowed to contain IdentityRole objects.
var roleStore = new RoleStore<IdentityRole>(context);
RoleManager roleMgr = new RoleManager();
if (!roleMgr.RoleExists("Administrator"))
{
roleMgr.Create(new ApplicationRole { Name = "Administrator" });
}
if (!roleMgr.RoleExists("Employee"))
{
roleMgr.Create(new ApplicationRole { Name = "Employee" });
}
if (!roleMgr.RoleExists("Complainant"))
{
roleMgr.Create(new ApplicationRole { Name = "Complainant" });
}
if (!roleMgr.RoleExists("Auditor"))
{
roleMgr.Create(new ApplicationRole { Name = "Auditor" });
}
// Create a UserManager object based on the UserStore object and the ApplicationDbContext
// object. Note that you can create new objects and use them as parameters in
// a single line of code, rather than using multiple lines of code, as you did
// for the RoleManager object.
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "Administrator",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Admin123");
// If the new "canEdit" user was successfully created,
// add the "canEdit" user to the "canEdit" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Administrator"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "Administrator");
}
appUser = new ApplicationUser
{
UserName = "Auditor",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Auditor123");
// If the new "canEdit" user was successfully created,
// add the "canEdit" user to the "canEdit" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Auditor"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "Auditor");
}
}
示例6: Index
public ActionResult Index()
{
MyIdentityDbContext db = new MyIdentityDbContext();
UserStore<MyIdentityUser> userStore = new UserStore<MyIdentityUser>(db);
UserManager<MyIdentityUser> userManager = new UserManager<MyIdentityUser>(userStore);
MyIdentityUser user = userManager.FindByName(HttpContext.User.Identity.Name);
NorthWindEntities northwindDb = new NorthWindEntities();
List<Customer> customers = null;
if (userManager.IsInRole(user.Id, "Administrator"))
{
customers = northwindDb.Customers.ToList();
}
if (userManager.IsInRole(user.Id, "Operator"))
{
customers = northwindDb.Customers.Where(m => m.City == "USA").ToList();
}
ViewBag.FullName = user.FullName + " (" + user.UserName + ") !";
return View(customers);
}
示例7: GetCurrentUserRoleById
public EnumHelper.Roles GetCurrentUserRoleById(string userId)
{
var store = new UserStore<ApplicationUser>(_context);
var manager = new UserManager<ApplicationUser>(store);
EnumHelper.Roles userRole = EnumHelper.Roles.Viewer;
if (manager.IsInRole(userId, EnumHelper.Roles.Admin.ToString()))
{
return EnumHelper.Roles.Admin;
}
else if (manager.IsInRole(userId, EnumHelper.Roles.Author.ToString()))
{
return EnumHelper.Roles.Author;
}
return userRole;
}
示例8: AddUserAndRole
internal void AddUserAndRole()
{
// access the application context and create result variables.
Models.ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
// create roleStore object that can only contain IdentityRole objects by using the ApplicationDbContext object.
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
// create admin role if it doesn't already exist
if (!roleMgr.RoleExists("admin"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "admin" });
}
// create a UserManager object based on the UserStore object and the ApplicationDbContext object.
// defines admin email account
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Pa$$word1");
// If the new admin user was successfully created, add the new user to the "admin" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "admin"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "admin");
}
}
示例9: AddUserAndRole
internal void AddUserAndRole()
{
Models.ApplicationDbContext context = new Models.ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
if (!roleMgr.RoleExists("administrator"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "administrator" });
}
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "administrator",
};
IdUserResult = userMgr.Create(appUser, "1qaz2wsxE");
var user = userMgr.FindByName("administrator");
if (!userMgr.IsInRole(user.Id, "administrator"))
{
//userMgr.RemoveFromRoles(user.Id, "read", "edit");
IdUserResult = userMgr.AddToRole(userMgr.FindByName("administrator").Id, "administrator");
}
}
示例10: DeleteRoleForUser
public ActionResult DeleteRoleForUser(string UserName, string RoleName)
{
var account = new AccountController();
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
if (UserManager.IsInRole(user.Id, RoleName))
{
UserManager.RemoveFromRole(user.Id, RoleName);
var employee = context.Employees.Single(x => x.employeeFirstName == user.FirstName && x.employeeLastName ==user.LastName);
var role = context.RolesForEmployees.Single(x => x.roleName == RoleName);
var tmp = context.EmployeeRoles.Single(x => x.role == role && x.employee == employee);
context.EmployeeRoles.Remove(tmp);
context.SaveChanges();
ViewBag.ResultMessage = "Role removed from this user successfully !";
}
else
{
ViewBag.ResultMessage = "This user doesn't belong to selected role.";
}
// 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("ManageUserRoles");
}
示例11: AddUserRole
public static void AddUserRole(string userName, string roleName)
{
using (var context = new ApplicationDbContext())
{
try
{
if (!context.Roles.Any(r => r.Name == roleName)) return;
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var store = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(store);
var user = userManager.FindByName(userName);
var role = roleManager.FindByName(roleName);
if (userManager.IsInRole(user.Id, role.Name)) return;
userManager.AddToRole(user.Id, role.Name);
context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
// Retrieve the error messages as a list of strings.
// ReSharper disable once UnusedVariable
var errorMessages = ex.EntityValidationErrors
.SelectMany(x => x.ValidationErrors)
.Select(x => x.ErrorMessage);
throw;
}
}
}
示例12: btnRegister_Click
protected void btnRegister_Click(object sender, EventArgs e)
{
var userMgr = new UserManager();
var employee = new Employee()
{
UserName = UserName.Text,
FirstName = FirstName.Text,
LastName = LastName.Text,
PhoneNumber = PhoneNumber.Text,
Email = Email.Text,
Department = (Grievance.GrievanceTypes)Convert.ToInt32(Department.SelectedValue)
};
IdentityResult IdUserResult = userMgr.Create(employee, Password.Text);
if (IdUserResult.Succeeded)
{
if (!userMgr.IsInRole(employee.Id, "Employee")) // Only users of type "Employee" can be created from the "Register Employee" page.
{
IdUserResult = userMgr.AddToRole(employee.Id, "Employee");
}
SuccessMessage.Text = "Employee created successfully";
SuccessMessage.Visible = true;
ErrorMessage.Visible = false;
UserName.Text = FirstName.Text = LastName.Text = PhoneNumber.Text = Email.Text = Password.Text = ConfirmPassword.Text = string.Empty;
}
else
{
ErrorMessage.Text = IdUserResult.Errors.FirstOrDefault();
ErrorMessage.Visible = true;
SuccessMessage.Visible = false;
}
}
示例13: AddUserAndRole
internal void AddUserAndRole()
{
Models.ApplicationDbContext db = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
var roleStore = new RoleStore<IdentityRole>(db);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
if (!roleMgr.RoleExists("canEdit"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });
}
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
var appUser = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "NhatSinh123*");
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canEdit"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit");
}
}
示例14: UpdateUser
internal bool UpdateUser(string username, string realUsername, string permission)
{
IdentityResult result = null;
Models.ApplicationDbContext context = new Models.ApplicationDbContext();
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var user = userMgr.FindByName(username);
if (!string.IsNullOrEmpty(realUsername))
{
user.RealUserName = realUsername;
result = userMgr.Update(user);
}
if (!string.IsNullOrEmpty(permission) && !userMgr.IsInRole(user.Id, permission))
{
userMgr.RemoveFromRoles(user.Id, "read", "edit", "administrator");
switch (permission)
{
case "administrator":
result = userMgr.AddToRole(user.Id, "administrator");
break;
case "edit":
result = userMgr.AddToRole(user.Id, "edit");
break;
default:
result = userMgr.AddToRole(user.Id, "read");
break;
}
}
if (result == IdentityResult.Success) return true; else return false;
}
示例15: Index
// GET: Maintenance/UserRoleMaintenance
public ActionResult Index()
{
if (Request.IsAuthenticated)
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
if (!userManager.IsInRole(User.Identity.GetUserId(), "System Admin"))
{
return RedirectToAction("AccessBlock", "Account", new { area = "" });
}
else
{
// 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();
var schemeList = db.Schemes.Select(m => new { Value = m.Name, Text = m.Name }).Distinct().ToList();
ViewBag.Roles = list;
var listUsers = context.Users.OrderBy(r => r.UserName).ToList().Select(rr => new SelectListItem { Value = rr.UserName.ToString(), Text = rr.UserName }).ToList();
ViewBag.Users = listUsers;
ViewBag.SchemeList = new MultiSelectList(schemeList, "Value", "Text");
return View();
}
}
else
{
return RedirectToAction("Login", "Account", new { area = "" });
}
}