本文整理汇总了C#中RoleManager.RoleExists方法的典型用法代码示例。如果您正苦于以下问题:C# RoleManager.RoleExists方法的具体用法?C# RoleManager.RoleExists怎么用?C# RoleManager.RoleExists使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RoleManager
的用法示例。
在下文中一共展示了RoleManager.RoleExists方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
public async Task<ActionResult> Create(DoctorViewModel DoctorViewModel)
{
if (ModelState.IsValid)
{
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var user = new ApplicationUser() { UserName = DoctorViewModel.Email };
var result = await UserManager.CreateAsync(user, DoctorViewModel.Password);
string roleName = "Doctor";
IdentityResult roleResult;
if (!RoleManager.RoleExists(roleName))
{
roleResult = RoleManager.Create(new IdentityRole(roleName));
}
try
{
var findUser = UserManager.FindByName(DoctorViewModel.Email);
UserManager.AddToRole(findUser.Id, "Doctor");
context.SaveChanges();
}
catch
{
throw;
}
Doctor_Detail doctor = MapDoctor(DoctorViewModel);
db.Doctor_Details.Add(doctor);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(DoctorViewModel);
}
示例2: 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");
}
}
示例3: SeedRolesAndUsers
private void SeedRolesAndUsers(ETCCanadaContext context)
{
RoleManager<ApplicationRole> roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(context));
if (!roleManager.RoleExists("Administrator"))
{
roleManager.Create(new ApplicationRole("Administrator"));
}
if (!roleManager.RoleExists("User"))
{
roleManager.Create(new ApplicationRole("User"));
}
List<ApplicationUser> users = new List<ApplicationUser>();
ApplicationUser user1 = new ApplicationUser { UserName = "[email protected]", Email = "[email protected]", FirstName = "Administrator", LastName = "Administrator", IsApproved = true };
ApplicationUser user2 = new ApplicationUser { UserName = "[email protected]", Email = "[email protected]", FirstName = "User", LastName = "User", IsApproved = true };
users.Add(user1);
users.Add(user2);
SeedDefaultUser(context, user1, "Administrator");
SeedDefaultUser(context, user2, "User");
//http://stackoverflow.com/questions/20470623/asp-net-mvc-asp-net-identity-seeding-roles-and-users
//http://stackoverflow.com/questions/19280527/mvc5-seed-users-and-roles
//http://stackoverflow.com/questions/19745286/asp-net-identity-db-seed
}
示例4: Users
public ActionResult Users()
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
if (!roleManager.RoleExists("user"))
{
var nRole = new IdentityRole("user");
roleManager.Create(nRole);
}
if (!roleManager.RoleExists("admin"))
{
var nRole = new IdentityRole("admin");
roleManager.Create(nRole);
}
if (!roleManager.RoleExists("moderator"))
{
var nRole = new IdentityRole("moderator");
roleManager.Create(nRole);
}
if (!roleManager.RoleExists("journalist"))
{
var nRole = new IdentityRole("journalist");
roleManager.Create(nRole);
}
var vm = new AdminUsersViewModel();
vm.Users = db.Users.OrderBy(x => x.UserName).ToList();
vm.Roles = db.Roles.ToList();
return View(vm);
}
示例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: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("BodyTag");
body.Attributes.Add("class", "login");
RoleStore<IdentityRole> roleStore = new RoleStore<IdentityRole>();
RoleManager<IdentityRole> roleMgr = new RoleManager<IdentityRole>(roleStore);
if (User.Identity.IsAuthenticated)
{
Response.Redirect("~/MainPage.aspx");
}
//creating roles//
if (!roleMgr.RoleExists("Admin"))
{
IdentityResult roleResult = roleMgr.Create(new IdentityRole("Admin"));
}
if (!roleMgr.RoleExists("Manager"))
{
IdentityResult roleResult = roleMgr.Create(new IdentityRole("Manager"));
}
if (!roleMgr.RoleExists("SalesAssoc"))
{
IdentityResult roleResult = roleMgr.Create(new IdentityRole("SalesAssoc"));
}
if (!roleMgr.RoleExists("Designer"))
{
IdentityResult roleResult = roleMgr.Create(new IdentityRole("Designer"));
}
if (!roleMgr.RoleExists("Laborers"))
{
IdentityResult roleResult = roleMgr.Create(new IdentityRole("Laborers"));
}
}
示例7: CreateUserAndRole
internal void CreateUserAndRole()
{
using (var context = new WebDeveloperDbContext())
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var userManager = new UserManager<WebDeveloperUser>(new UserStore<WebDeveloperUser>(context));
// In Startup iam creating first Admin Role and creating a default Admin User
if (!roleManager.RoleExists("Admin"))
{
// first we create Admin rool
var role = new IdentityRole();
role.Name = "Admin";
roleManager.Create(role);
//Here we create a Admin super user who will maintain the website
var user = new WebDeveloperUser
{
UserName = "[email protected]",
Email = "[email protected]"
};
string userPassword = "Passw0rd2016";
var userCreation = userManager.Create(user, userPassword);
//Add default User to Role Admin
if (userCreation.Succeeded)
userManager.AddToRole(user.Id, "Admin");
}
// creating Creating Manager role
if (!roleManager.RoleExists("Manager"))
{
var role = new IdentityRole
{
Name = "Manager"
};
roleManager.Create(role);
}
// creating Creating Employee role
if (!roleManager.RoleExists("Employee"))
{
var role = new IdentityRole
{
Name = "Employee"
};
roleManager.Create(role);
}
}
}
示例8: SetupRolesAndUsers
private static void SetupRolesAndUsers(DbContext context)
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
// add roles
if (!roleManager.RoleExists(Role.Guest.ToString()))
roleManager.Create(new IdentityRole(Role.Guest.ToString()));
if (!roleManager.RoleExists(Role.Supplier.ToString()))
roleManager.Create(new IdentityRole(Role.Supplier.ToString()));
if (!roleManager.RoleExists(Role.Deactivated.ToString()))
roleManager.Create(new IdentityRole(Role.Deactivated.ToString()));
if (!roleManager.RoleExists(Role.User.ToString()))
roleManager.Create(new IdentityRole(Role.User.ToString()));
var adminRole = roleManager.FindByName(Role.Admin.ToString());
if (adminRole == null)
{
adminRole = new IdentityRole(Role.Admin.ToString());
roleManager.Create(adminRole);
}
#if DEBUG
//add admin user
var admin = userManager.Find(Admin_User, Admin_Pass);
if (admin == null)
{
admin = new ApplicationUser
{
UserName = Admin_User,
Email = Admin_Mail,
EmailConfirmed = true
};
var result = userManager.Create(admin, Admin_Pass);
// TODO: verify returned IdentityResult
userManager.AddToRole(admin.Id, Role.Admin.ToString());
result = userManager.SetLockoutEnabled(admin.Id, false);
}
var rolesForUser = userManager.GetRoles(admin.Id);
if (!rolesForUser.Contains(adminRole.Name))
{
var result = userManager.AddToRole(admin.Id, adminRole.Name);
}
//add normal user
if (userManager.Find("[email protected]", "1q2w3e4r") == null)
{
var user = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]",
EmailConfirmed = true
};
userManager.Create(user, "1q2w3e4r");
// TODO: verify returned IdentityResult
userManager.AddToRole(user.Id, Role.User.ToString());
}
#endif
}
示例9: AddUserAndRole
internal void AddUserAndRole()
{
// Access the application context and create result variables.
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("Admin"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "Admin" });
}
//create the "canBuy" role if it doesn't already exist.
if (!roleMgr.RoleExists("Normal"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "Normal" });
}
// 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 = "Michael",
Email = "[email protected]",
PhoneNumber = "0172215759"
};
IdUserResult = userMgr.Create(appUser, "aaa111A!");
// If the new "canEdit" user was successfully created,
// add the "canEdit" user to the "canEdit" role.
AddToAdminUserRole(appUser);
AddToNormalUserRole(appUser);
//if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canEdit"))
//{
// IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit");
//}
//if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canBuy"))
//{
// IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canBuy");
//}
}
示例10: SeedRoles
public static void SeedRoles(ApplicationDbContext db)
{
var store = new RoleStore<IdentityRole>(db);
var manager = new RoleManager<IdentityRole>(store);
if (!manager.RoleExists(Roles.USER))
{
manager.Create(new IdentityRole() { Name = Roles.USER });
}
if (!manager.RoleExists(Roles.ADMIN))
{
manager.Create(new IdentityRole() { Name = Roles.ADMIN });
}
}
示例11: Seed
public static void Seed(ApplicationDbContext context)
{
UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(context);
UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);
RoleStore<Role> roleStore = new RoleStore<Role>(context);
RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);
if (!roleManager.RoleExists("Admin"))
roleManager.Create(new Role { Name = "Admin" });
if (!roleManager.RoleExists("User"))
roleManager.Create(new Role { Name = "User" });
IdentityResult result = null;
ApplicationUser user1 = userManager.FindByName("[email protected]");
if (user1 == null)
{
user1 = new ApplicationUser { Email = "[email protected]", UserName = "[email protected]" };
}
result = userManager.Create(user1, "asdfasdf");
if (!result.Succeeded)
{
string error = result.Errors.FirstOrDefault();
throw new Exception(error);
}
userManager.AddToRole(user1.Id, "Admin");
user1 = userManager.FindByName("[email protected]");
ApplicationUser user2 = userManager.FindByName("[email protected]");
if (user2 == null)
{
user2 = new ApplicationUser { Email = "[email protected]", UserName = "[email protected]" };
}
result = userManager.Create(user2, "asdfasfd");
if (!result.Succeeded)
{
string error = result.Errors.FirstOrDefault();
throw new Exception(error);
}
userManager.AddToRole(user2.Id, "User");
user2 = userManager.FindByName("[email protected]");
}
示例12: AddUserAndRole
internal void AddUserAndRole()
{
// Access the application context and create result variables.
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("administrator"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "administrator" });
}
// Then, you create the "canEdit" role if it doesn't already exist.
if (!roleMgr.RoleExists("manager"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "manager" });
}
// Then, you create the "canEdit" role if it doesn't already exist.
if (!roleMgr.RoleExists("member"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "member" });
}
// 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 = "[email protected]",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "pass1234");
// 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");
}
}
示例13: SeedDbUsers
public static void SeedDbUsers(ApplicationDbContext context, List<Image> userImages)
{
Users = new List<ApplicationUser>();
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
if (!roleManager.RoleExists("Admin"))
{
roleManager.Create(new IdentityRole("Admin"));
}
var admin = new ApplicationUser
{
UserName = "[email protected]",
PasswordHash = new PasswordHasher().HashPassword("admin"),
SecurityStamp = Guid.NewGuid().ToString("D"),
FirstName = "Admin",
LastName = "Adminski",
Email = "[email protected]"
};
Admin = admin;
userManager.Create(admin);
userManager.AddToRole(admin.Id, "Admin");
if (!roleManager.RoleExists("User"))
{
roleManager.Create(new IdentityRole("User"));
}
for (int i = 0; i < 20; i++)
{
var user = new ApplicationUser
{
UserName = "user" + i + "@artist.com",
PasswordHash = new PasswordHasher().HashPassword("user" + i),
SecurityStamp = Guid.NewGuid().ToString("D"),
FirstName = "FirstName" + i,
LastName = "LastName" + i,
Email = "user" + i + "@artist.com",
Images = userImages[0]
};
userManager.Create(user);
userManager.AddToRole(admin.Id, "User");
Users.Add(user);
}
}
示例14: Application_Start
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// Tạo role sẵn
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
List<string> roleNameList = new List<string> { "Admin"
, "Designer"
, "Mod"
, "Uploader"
, "Subteam"
, "Subber"
, "VIP"
, "Member" };
foreach (string roleName in roleNameList)
{
if (!roleManager.RoleExists(roleName))
{
var newRole = new IdentityRole();
newRole.Name = roleName;
roleManager.Create(newRole);
}
}
}
示例15: Create
public ActionResult Create(ClientViewModel client)
{
if (!ModelState.IsValid) return View();
//Register user and SingIn
var accountController = new AccountController {ControllerContext = this.ControllerContext};
var user = accountController.RegisterAccount(new RegisterViewModel() { Email = client.Email, Password = client.Password });
accountController.SignInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
//Add user to client role
var userStore = new UserStore<ApplicationUser>(_context);
var userManager = new UserManager<ApplicationUser>(userStore);
var roleStore = new RoleStore<IdentityRole>(_context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
if (!roleManager.RoleExists("Client"))
roleManager.Create(new IdentityRole("Client"));
userManager.AddToRole(user.Id, "Client");
//Register client
if (string.IsNullOrWhiteSpace(user.Id)) return View();
_context.Clients.Add(new Client()
{
Id = user.Id,
Name = client.Name,
Age = client.Age
});
_context.SaveChanges();
return RedirectToAction("Index", "Home");
}