本文整理汇总了C#中RoleManager.FindByNameAsync方法的典型用法代码示例。如果您正苦于以下问题:C# RoleManager.FindByNameAsync方法的具体用法?C# RoleManager.FindByNameAsync怎么用?C# RoleManager.FindByNameAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RoleManager
的用法示例。
在下文中一共展示了RoleManager.FindByNameAsync方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EditMembers
public async Task<ActionResult> EditMembers(string id)
{
try
{
using (var ctx = new ApplicationDbContext())
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(ctx));
IdentityRole role = await roleManager.FindByNameAsync(id);
if (role == null)
{
// TODO: show error.
}
var roleMemberIds = role.Users.Select(m => m.UserId);
var roleMembers = from u in ctx.Users
where roleMemberIds.Contains(u.Id)
select u;
var availableUsers = await ctx.Users.Where(u => !roleMemberIds.Contains(u.Id)).ToArrayAsync();
var model = new EditRoleMembersViewModel()
{
AvailableUsers = availableUsers.Select(MapDbUserToUserViewModel).ToArray(),
CurrentMembers = roleMembers.Select(MapDbUserToUserViewModel).ToArray(),
RoleName = role.Name,
RoleId = role.Id
};
return View(model);
}
}
catch (Exception e)
{
// TODO
return View(new EditRoleMembersViewModel());
}
}
示例2: Add
public async Task<ActionResult> Add(UserViewModel model)
{
if (ModelState.IsValid)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(_unitOfWork.DataContext));
var user = new ApplicationUser()
{
UserName = model.UserName,
Email = model.Email,
EmailConfirmed = true,
FullName = model.FullName
};
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_unitOfWork.DataContext));
var role = await roleManager.FindByNameAsync("roleAdmin");
var result = await manager.CreateAsync(user, model.PasswordHash);
if (result.Succeeded && role != null)
{
manager.AddToRole(user.Id, role.Name);
}
else
{
throw new NullReferenceException();
}
return RedirectToAction("List");
}
return HttpNotFound();
}
示例3: CreateRole
public async Task<ActionResult> CreateRole(string roleName)
{
var roleStore = new RoleStore<IdentityRole>();
var roleManager = new RoleManager<IdentityRole>(roleStore);
var role = await roleManager.FindByNameAsync(roleName);
if (role != null)
{
return View();
}
role = new IdentityRole { Name = roleName };
await roleManager.CreateAsync(role);
return RedirectToAction("index", "home");
}
示例4: AddUserToRole
public async Task<ActionResult> AddUserToRole(string roleName)
{
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var roleStore = new RoleStore<IdentityRole>();
var roleManager = new RoleManager<IdentityRole>(roleStore);
var role = await roleManager.FindByNameAsync(roleName);
if (role == null)
{
return View();
}
await userManager.AddToRoleAsync(User.Identity.GetUserId(), roleName);
return RedirectToAction("index", "home");
}
示例5: Seed
private async Task Seed(IUnitOfWork uow , UserManager<ApplicationUser> usrMan, RoleManager<IdentityRole> _RoleManager)
{
var email = "[email protected]";
var RoleName = "CustomerAdmin";
var role = await _RoleManager.FindByNameAsync(RoleName);
if (role == null)
{
role = new IdentityRole { Name = RoleName };
await _RoleManager.CreateAsync(role);
}
RoleName = "superadmin";
role = await _RoleManager.FindByNameAsync( RoleName );
if ( role == null)
{
role = new IdentityRole { Name = "SuperAdmin" };
await _RoleManager.CreateAsync(role);
}
var appUser = await usrMan.FindByEmailAsync(email);
if (appUser != null)
{
var result = await usrMan.DeleteAsync(appUser);
}
else
{
appUser = new ApplicationUser() { UserName = email, Email = email };
var result2 = await usrMan.CreateAsync(appUser, "Shafiro2,");
if (result2.Succeeded)
{
await usrMan.AddToRoleAsync(appUser, RoleName );
var hrmUser = new User()
{
UserID = "7310209296",
FirstName = "Gabriel",
LastName = "Tekin",
UserName = "gabtek",
UserCode = "tekgab",
PhoneNumber = "0702385537"
};
if (uow.UserRepository.GetEagerLoad( w => w.UserID.Equals( hrmUser.UserID, StringComparison.OrdinalIgnoreCase )).FirstOrDefault() == null )
{
uow.UserRepository.Insert(hrmUser);
uow.Save();
}
}
else
{
var code = result2.Errors.FirstOrDefault().Code;
}
}
} // method seed
示例6: Register
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.UserName };
user.Introduction = "";
user.BattleTag = "";
user.ImageUrl = "http://s3.amazonaws.com/37assets/svn/765-default-avatar.png";
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
if (roleManager.FindByNameAsync("user") == null)
{
var nRole = new IdentityRole("user");
roleManager.Create(nRole);
}
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
userManager.AddToRole(user.Id, "user");
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}