本文整理汇总了C#中RoleManager.RoleExistsAsync方法的典型用法代码示例。如果您正苦于以下问题:C# RoleManager.RoleExistsAsync方法的具体用法?C# RoleManager.RoleExistsAsync怎么用?C# RoleManager.RoleExistsAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RoleManager
的用法示例。
在下文中一共展示了RoleManager.RoleExistsAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
}
}
示例2: CreateRole_OnClick
public void CreateRole_OnClick(object sender, EventArgs args)
{
string createRole = RoleTextBox.Text;
try
{
var rm = new RoleManager();
var task = rm.RoleExistsAsync(createRole);
task.Wait();
if (task.Result)
{
Msg.Text = "Role '" + Server.HtmlEncode(createRole) + "' already exists. Please specify a different role name.";
return;
}
rm.CreateAsync(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(createRole));
Msg.Text = "Role '" + Server.HtmlEncode(createRole) + "' created.";
BindRolestoGridView();
}
catch (Exception e)
{
Msg.Text = "Role '" + Server.HtmlEncode(createRole) + "' <u>not</u> created.";
Response.Write(e.ToString());
}
}
示例3: All
// GET: Users
public async Task<ActionResult> All(string role)
{
// All users
if(role == null)
{
ViewBag.Title = "All users";
return View(db.Users.ToList());
}
ViewBag.Title = String.Format("Users in '{0}'", role);
// Users in specific role
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
var result = new List<ApplicationUser>();
if(await roleManager.RoleExistsAsync(role))
{
foreach(var user in db.Users)
{
if(await userManager.IsInRoleAsync(user.Id, role))
{
result.Add(user);
}
}
return View(result);
}
else
{
return View();
}
}
示例4: RoleSync
public static void RoleSync(RoleManager<IdentityRole> roleManager)
{
foreach (var role in RoleNames.AllRoles)
{
var roleExist = roleManager.RoleExistsAsync(role);
if (!roleExist.Result)
{
roleManager.CreateAsync(new IdentityRole
{
Name = role,
NormalizedName = roleManager.NormalizeKey(role)
});
}
}
}
示例5: Register
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// Check that role exists
var db = new ApplicationDbContext();
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
if(!(await roleManager.RoleExistsAsync(model.Role)))
{
return BadRequest(string.Format("No such role: {0}", model.Role));
}
var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if(!result.Succeeded)
{
return GetErrorResult(result);
}
await UserManager.AddToRoleAsync(user.Id, model.Role);
return Ok();
}