本文整理汇总了C#中RoleStore类的典型用法代码示例。如果您正苦于以下问题:C# RoleStore类的具体用法?C# RoleStore怎么用?C# RoleStore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RoleStore类属于命名空间,在下文中一共展示了RoleStore类的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: Create
/*This should be removed after the first admin gets made */
// GET: MakeMeAdmin/Create
public ActionResult Create(string email)
{
using (var context = new ApplicationDbContext())
{
var fadmin = context.KeyValueSettings.FirstOrDefault(s => s.Key == "FirstAdminSet");
if (fadmin == null || fadmin.Value == "false")
{
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
roleManager.Create(new IdentityRole("Admin"));
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = userManager.FindByEmail(email);
userManager.AddToRole(user.Id, "Admin");
if (fadmin == null)
{
context.KeyValueSettings.Add(new KeyValueSettings() { Key = "FirstAdminSet", Value = "true" });
}
else
{
fadmin.Value = "true";
}
context.SaveChanges();
return Json(true, JsonRequestBehavior.AllowGet);
}
}
return Json(false, JsonRequestBehavior.AllowGet);
}
示例3: 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");
}
示例4: 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]fo",
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);
}
}
}
示例5: gv_RowDataBound
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddList = (DropDownList)e.Row.FindControl("DropDownList1");
Models.ApplicationDbContext context = new ApplicationDbContext();
var roleStore = new RoleStore<IdentityRole>(context);
var myRoles = new List<string>();
var roleMgr = new RoleManager<IdentityRole>(roleStore);
foreach (var role in roleMgr.Roles.ToList())
{
myRoles.Add(role.Name);
}
ddList.DataSource = myRoles;
ddList.DataBind();
DataRowView dr = e.Row.DataItem as DataRowView;
ddList.SelectedValue = dr["role"].ToString();
}
}
}
示例6: 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");
}
}
示例7: 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;
}
}
}
示例8: SeedRoles
public static void SeedRoles()
{
var context = new ApplicationDbContext();
if (!context.Roles.Any())
{
if (!context.Roles.Any())
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var roleCreateResult = roleManager.Create(new IdentityRole("Admin"));
if (!roleCreateResult.Succeeded)
{
throw new Exception(string.Join("; ", roleCreateResult.Errors));
}
var roleStore = new RoleStore<IdentityRole>(context);
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = new ApplicationUser() { UserName = "[email protected]", Email = "[email protected]" };
var createResult = userManager.Create(user, "123456");
if (!createResult.Succeeded)
{
throw new Exception(string.Join("; ", createResult.Errors));
}
userManager.AddToRole(user.Id, "admin");
context.SaveChanges();
}
}
}
示例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",
ImgUrl = "user2-160x160.jpg",
Description = "High Level",
SinceDate = new DateTime(2016, 1, 1)
};
IdUserResult = userMgr.Create(appUser, "1qaz2wsxE");
var user = userMgr.FindByName("administrator");
if (!userMgr.IsInRole(user.Id, "administrator"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByName("administrator").Id, "administrator");
}
}
示例10: Up
public override void Up()
{
ApplicationDbContext context = new ApplicationDbContext();
var adminUser = new ApplicationUser()
{
Id = Guid.NewGuid().ToString(),
EmailConfirmed = false,
PhoneNumberConfirmed = false,
TwoFactorEnabled = false,
LockoutEnabled = false,
AccessFailedCount = 0,
Email = "[email protected]",
UserName = "[email protected]"
};
if (!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Admin" };
manager.Create(role);
}
if (!context.Users.Any(u => u.UserName == "TheGaffer"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
manager.Create(adminUser, "Seisen1!");
manager.AddToRole(adminUser.Id, "Admin");
}
}
示例11: 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");
}
}
示例12: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
var roles = new RoleStore<IdentityRole>();
var rolesManager = new RoleManager<IdentityRole>(roles);
ddlRol.DataSource = rolesManager.Roles.ToList();
ddlRol.DataBind();
}
示例13: GetRoles
public IQueryable<IdentityRole> GetRoles()
{
var roleStore = new RoleStore<IdentityRole>();
var roles = new RoleManager<IdentityRole>(roleStore);
return roles.Roles;
}
示例14: AddUserAndRole
internal void AddUserAndRole()
{
// Access the application context and create result variable.
Models.ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdRoleResult;
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);
// Create a RoleManager object that is only allowed to contain IdentityRole objects.
// When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object.
var roleMgr = new RoleManager<IdentityRole>(roleStore);
// Then, you create the "canEdit" role if it doesn't already exist.
if (!roleMgr.RoleExists("canEdit"))
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });
// Create a UserManager object based on the UserStore objcet and the ApplicationDbContext objcet.
// 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 = "[email protected]",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Pa$$word1");
// If the new "canEdit" user was successfully created, add the "canEdit" user to the "canEdit" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canEdit"))
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit");
}
示例15: AddUserAndRole
internal void AddUserAndRole()
{
ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
if (!roleMgr.RoleExists("Admin"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "Admin" });
}
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "adminA123...");
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Admin"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit");
}
}