本文整理汇总了C#中IdentityRole类的典型用法代码示例。如果您正苦于以下问题:C# IdentityRole类的具体用法?C# IdentityRole怎么用?C# IdentityRole使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IdentityRole类属于命名空间,在下文中一共展示了IdentityRole类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RegisterUser
public async Task<IdentityResult> RegisterUser(UserModel userModel, string roleName)
{
IdentityUser user = new IdentityUser
{
UserName = userModel.UserName,
Email = userModel.UserName
};
var result = await _userManager.CreateAsync(user, userModel.Password);
if (result == IdentityResult.Success)
{
var role = await _roleManager.FindByNameAsync(roleName);
if (role == null)
{
role = new IdentityRole(roleName);
var roleresult = await _roleManager.CreateAsync(role);
}
var userFromDb = await _userManager.FindByNameAsync(user.UserName);
if (userFromDb != null)
{
await _userManager.AddToRoleAsync(userFromDb.Id, roleName);
}
}
return result;
}
示例2: Insert
/// <summary>
/// Inserts a new Role in the Roles table
/// </summary>
/// <param name="roleName">The role's name</param>
/// <returns></returns>
public int Insert(IdentityRole role)
{
using (var db = _database.Open())
{
return (int)db.Insert<IdentityRole>(role);
}
}
示例3: IdentityRoleSet_Id
public void IdentityRoleSet_Id()
{
var role = new IdentityRole();
role.Id = Guid.NewGuid().ToString();
role.Id = null;
}
示例4: Page_Load
protected void Page_Load(object sender, EventArgs e) {
Feedback.Visible = false;
Guid roleId;
if (!Request.QueryString["id"].TryParseGuid(out roleId)) {
ShowError(Feedback, "The parameter for id is not in the correct format!");
SaveButton.Visible = false;
FormFields.Visible = false;
return;
}
_roleManager = IdentityRoleManager.GetManager();
_role = _roleManager.FindById(roleId);
if (_role == null) {
ShowError(Feedback, "No role was found for the given id!");
SaveButton.Visible = false;
FormFields.Visible = false;
return;
}
SaveButton.Click += SaveButton_Click;
if (!IsPostBack) {
RoleId.Value = roleId.ToString();
RoleName.Text = _role.Name;
if (Request.QueryString["message"] == "created") {
ShowMessage(Feedback, "Role has been created!");
}
}
RenderUsersInRole();
}
示例5: Delete
public JsonResult Delete(IdentityRole role)
{
_context.Entry(role).State = EntityState.Deleted;
_context.SaveChanges();
return Json(_context.Roles.ToList(), JsonRequestBehavior.AllowGet);
}
示例6: Create
public JsonResult Create(IdentityRole role)
{
_context.Roles.Add(role);
_context.SaveChanges();
return Json(_context.Roles.ToList(), JsonRequestBehavior.AllowGet);
}
示例7: RegisterCustomerAsync
public async Task<IdentityResult> RegisterCustomerAsync(UserModel userModel, AppRole appRole = AppRole.customer)
{
var addToRoleResult = new IdentityResult();
// Add generate username to the model
IdentityUser user = new IdentityUser
{
UserName = userModel.UserName,
Email = userModel.EmailAddress,
EmailConfirmed = true,
PhoneNumber = userModel.PhoneNumber,
PhoneNumberConfirmed = true,
};
var result = await _userManager.CreateAsync(user, userModel.Password);
//Role-user combination is managed as Contact-Role combination in ContactRole table
//Create Role Admin if it does not exist
var _role = await _roleManager.FindByNameAsync(appRole.ToString());
if (_role == null)
{
_role = new IdentityRole(appRole.ToString());
var roleresult = await _roleManager.CreateAsync(_role);
}
var rolesForUser = await _userManager.GetRolesAsync(user.Id);
if (!rolesForUser.Contains(_role.Name))
{
addToRoleResult = await _userManager.AddToRoleAsync(user.Id, _role.Name);
}
return addToRoleResult;
}
示例8: RoleViewModel
public RoleViewModel(IdentityRole role)
{
this.Id = role.Id;
this.Name = role.Name;
this.ConcurrencyStamp = role.ConcurrencyStamp;
//this.Description = role.Description;
}
示例9: GetRolesForUserShouldRetrieveCorrectRoles
public void GetRolesForUserShouldRetrieveCorrectRoles()
{
var role1 = new IdentityRole(GetNewRandomName());
var role2 = new IdentityRole(GetNewRandomName());
_roleManager.Create<IdentityRole>(role1);
_roleManager.Create<IdentityRole>(role2);
var user1 = new IdentityUser(GetNewRandomName());
var user2 = new IdentityUser(GetNewRandomName());
var user3 = new IdentityUser(GetNewRandomName());
_userManager.Create(user1, _password);
_userManager.Create(user2, _password);
_userManager.Create(user3, _password);
_userManager.AddToRole(user1.Id, role1.Name);
_userManager.AddToRole(user1.Id, role2.Name);
_userManager.AddToRole(user2.Id, role1.Name);
_userManager.AddToRole(user3.Id, role2.Name);
var user1RoleNames = _userManager.GetRoles(user1.Id);
var user2RoleNames = _userManager.GetRoles(user2.Id);
var user3RoleNames = _userManager.GetRoles(user3.Id);
Assert.AreEqual(2, user1RoleNames.Count);
Assert.AreEqual(1, user2RoleNames.Count);
Assert.AreEqual(1, user3RoleNames.Count);
Assert.IsTrue(user1RoleNames.Any(r => r == role1.Name));
Assert.IsTrue(user1RoleNames.Any(r => r == role2.Name));
Assert.IsTrue(user2RoleNames.Any(r => r == role1.Name));
Assert.IsTrue(user3RoleNames.Any(r => r == role2.Name));
}
示例10: Add
public ActionResult Add(IdentityRole model)
{
try
{
db.Roles.Add(model);
db.SaveChanges();
var Projectdb = new ProjectMvcDbContext();
foreach (var p in Projectdb.WebActions)
{
var perm = new Permission
{
Allow = false,
RoleId = model.Id,
WebActionId = p.Id
};
Projectdb.Permissions.Add(perm);
}
Projectdb.SaveChanges();
return RedirectToAction("Index");
}
catch (Exception Ex)
{
ModelState.AddModelError("", Ex.Message);
ViewBag.Role = db.Roles;
return View("Index", model);
}
}
示例11: SeedAdmin
internal static void SeedAdmin(HotelSystemDbContext context)
{
const string adminEmail = "[email protected]";
const string adminPassword = "adminadmin";
if (context.Users.Any(u => u.Email == adminEmail))
{
return;
}
var admin = new User()
{
Email = adminEmail,
UserName = adminEmail,
PasswordHash = new PasswordHasher().HashPassword(adminPassword),
FirstName = "Admin",
LastName = "Admin",
BirthDate = new DateTime(1950, 2, 20),
PhoneNumber = "0888888888888",
SecurityStamp = Guid.NewGuid().ToString()
};
var adminRole = new IdentityRole { Name = GlobalConstants.AdministratorRole, Id = Guid.NewGuid().ToString() };
context.Roles.AddOrUpdate(adminRole);
admin.Roles.Add(new IdentityUserRole { RoleId = adminRole.Id, UserId = admin.Id });
context.Users.Add(admin);
context.SaveChanges();
}
示例12: Create
public ActionResult Create(IdentityRole Role)
{
context.Roles.Add(Role);
context.SaveChanges();
return RedirectToAction("Index");
}
示例13: Insert
public JsonData Insert(IdentityRole entity, string userId)
{
try
{
using (var db = new DataContext())
{
if (entity == null) throw new ArgumentNullException("The new" + " record is null");
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
//Create Roles if they do not exist
if (!roleManager.RoleExists(entity.Name))
{
roleManager.Create(new IdentityRole(entity.Name));
}
db.SaveChanges();
return DataHelpers.ReturnJsonData(entity, true, "Saved successfully", 1);
}
}
catch (Exception e)
{
return DataHelpers.ExceptionProcessor(e);
}
}
示例14: CreateAdminUser
private async Task CreateAdminUser()
{
var username = "admin";//ConfigurationManager.AppSettings["DefaultAdminUsername"];
var password = "password123";//ConfigurationManager.AppSettings["DefaultAdminPassword"];
using (var context = new ApplicationDbContext())
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var role = new IdentityRole(RoleName);
var result = await roleManager.RoleExistsAsync(RoleName);
if (!result)
{
await roleManager.CreateAsync(role);
}
var user = await userManager.FindByNameAsync(username);
if (user == null)
{
user = new ApplicationUser { UserName = username, Email = "[email protected]", First = "Big", Last="Admin Person" };
await userManager.CreateAsync(user, password);
await userManager.AddToRoleAsync(user.Id, RoleName);
}
}
}
示例15: AddRoleToUser
public void AddRoleToUser(string userName, string roleName)
{
var role = this.roles.All()
.FirstOrDefault(r => r.Name == roleName);
if (role == null)
{
role = new IdentityRole
{
Name = roleName
};
this.roles.Add(role);
this.roles.SaveChanges();
}
var user = this.users.All().FirstOrDefault(u => u.UserName == userName);
var userRole = this.userRoles.All()
.FirstOrDefault(r => r.UserId == user.Id && r.RoleId == role.Id);
if (userRole == null)
{
user.Roles.Add(new IdentityUserRole
{
UserId = user.Id,
RoleId = role.Id
});
this.users.Update(user);
this.users.SaveChanges();
}
}