本文整理汇总了C#中RoleManager.CreateAsync方法的典型用法代码示例。如果您正苦于以下问题:C# RoleManager.CreateAsync方法的具体用法?C# RoleManager.CreateAsync怎么用?C# RoleManager.CreateAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RoleManager
的用法示例。
在下文中一共展示了RoleManager.CreateAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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());
}
}
示例2: 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);
}
}
}
示例3: CreateRole
public static async Task<bool> CreateRole(string name)
{
var roleManager = new RoleManager<IdentityRole>(
new RoleStore<IdentityRole>(new PlatformUserIdentityDbContext()));
var idResult = await roleManager.CreateAsync(new IdentityRole(name));
return idResult.Succeeded;
}
示例4: ClaimsIdentityTest
public async Task ClaimsIdentityTest()
{
var db = UnitTestHelper.CreateDefaultDb();
var manager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(db));
var role = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
var user = new IdentityUser("Hao");
UnitTestHelper.IsSuccess(await manager.CreateAsync(user));
UnitTestHelper.IsSuccess(await role.CreateAsync(new IdentityRole("Admin")));
UnitTestHelper.IsSuccess(await role.CreateAsync(new IdentityRole("Local")));
UnitTestHelper.IsSuccess(await manager.AddToRoleAsync(user.Id, "Admin"));
UnitTestHelper.IsSuccess(await manager.AddToRoleAsync(user.Id, "Local"));
Claim[] userClaims =
{
new Claim("Whatever", "Value"),
new Claim("Whatever2", "Value2")
};
foreach (var c in userClaims)
{
UnitTestHelper.IsSuccess(await manager.AddClaimAsync(user.Id, c));
}
var identity = await manager.CreateIdentityAsync(user, "test");
var claimsFactory = manager.ClaimsIdentityFactory as ClaimsIdentityFactory<IdentityUser, string>;
Assert.NotNull(claimsFactory);
var claims = identity.Claims;
Assert.NotNull(claims);
Assert.True(
claims.Any(c => c.Type == claimsFactory.UserNameClaimType && c.Value == user.UserName));
Assert.True(claims.Any(c => c.Type == claimsFactory.UserIdClaimType && c.Value == user.Id));
Assert.True(claims.Any(c => c.Type == claimsFactory.RoleClaimType && c.Value == "Admin"));
Assert.True(claims.Any(c => c.Type == claimsFactory.RoleClaimType && c.Value == "Local"));
Assert.True(
claims.Any(
c =>
c.Type == ClaimsIdentityFactory<IdentityUser>.IdentityProviderClaimType &&
c.Value == ClaimsIdentityFactory<IdentityUser>.DefaultIdentityProviderClaimValue));
foreach (var cl in userClaims)
{
Assert.True(claims.Any(c => c.Type == cl.Type && c.Value == cl.Value));
}
}
示例5: AddRoleToStore
private async void AddRoleToStore(RoleManager<IdentityRole> manager)
{
var roles = manager.Roles.ToList();
if (!roles.Any(r => r.Name.Equals("Admin", StringComparison.OrdinalIgnoreCase)))
{
IdentityRole role = new IdentityRole("Admin");
await manager.CreateAsync(role);
}
roles = manager.Roles.ToList();
if (!roles.Any(r => r.Name.Equals("Builder", StringComparison.OrdinalIgnoreCase)))
{
IdentityRole role = new IdentityRole("Builder");
await manager.CreateAsync(role);
}
roles = manager.Roles.ToList();
if (!roles.Any(r => r.Name.Equals("Player", StringComparison.OrdinalIgnoreCase)))
{
IdentityRole role = new IdentityRole("Player");
await manager.CreateAsync(role);
}
}
示例6: 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)
});
}
}
}
示例7: Create
public async Task<ActionResult> Create(string name)
{
//var context = new ApplicationDbContext();
var roleStore = new RoleStore<IdentityRole>(db);
var roleManager = new RoleManager<IdentityRole>(roleStore);
IdentityResult result = await roleManager.CreateAsync(new IdentityRole(name));
if (result.Succeeded)
{
//await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index");
}
AddErrors(result);
return View(name);
}
示例8: Create
public ActionResult Create([Bind(Include = "Name")] RoleViewModel roleViewModel)
{
if (ModelState.IsValid)
{
var role = roleViewModel.ToDomainModel();
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
var roleresult = roleManager.CreateAsync(role);
if (roleresult.Result != IdentityResult.Success)
{
ModelState.AddModelError("", roleresult.Result.Errors.First());
return View(roleViewModel);
}
return RedirectToAction("Index");
}
return View(roleViewModel);
}
示例9: Seed
private async void Seed()
{
const string roleName = "administrator";
const string userName = "[email protected]";
var userContext = new ApplicationDbContext();
var roleStore = new RoleStore<IdentityRole>(userContext);
var roleManager = new RoleManager<IdentityRole>(roleStore);
if (roleManager.RoleExists(roleName)) return;
await roleManager.CreateAsync(new IdentityRole(roleName));
var userStore = new UserStore<ApplicationUser>(userContext);
var userManager = new UserManager<ApplicationUser>(userStore);
var admin = new ApplicationUser() {UserName = userName};
await userManager.CreateAsync(admin, "Koekoek!IsNummer1");
await userManager.AddToRoleAsync(admin.Id, roleName);
var pandContext = new ExclimmoContext();
pandContext.SeedPanden();
}
示例10: InitializeRoles
private async Task InitializeRoles()
{
if (_context.Roles.Any()) return;
var roleStore = new RoleStore<Role, LiverpoolContext, int>(_context);
var roleManager = new RoleManager<Role>(roleStore, null, null, null, null, new HttpContextAccessor());
var roles = new List<Role>()
{
new Role() //1
{
Name = RolesEnum.Simple.ToString(),
},
new Role()
{
Name = RolesEnum.NewsFull.ToString(),
},
new Role()//3
{
Name = RolesEnum.NewsStart.ToString(),
},
new Role()
{
Name = RolesEnum.BlogFull.ToString(),
},
new Role()//5
{
Name = RolesEnum.BlogStart.ToString(),
},
new Role()
{
Name = RolesEnum.UserFull.ToString(),
},
new Role()//7
{
Name = RolesEnum.UserStart.ToString(),
},
new Role()
{
Name = RolesEnum.AdminFull.ToString(),
},
new Role()//9
{
Name = RolesEnum.AdminStart.ToString(),
},
new Role()
{
Name = RolesEnum.ModeratorFull.ToString(),
},
new Role()//11
{
Name = RolesEnum.ModeratorStart.ToString(),
},
new Role()//12
{
Name = RolesEnum.Intern.ToString(),
},
};
// roles.ForEach(x => _context.RoleGroups.Add(x));
foreach (var role in roles)
{
await roleManager.CreateAsync(role);
}
// roles.ForEach(x => new Task(() => roleManager.CreateAsync(x)));
//roleManager.Create(new Role { Name = RolesEnum.User.ToString() });
await _context.SaveChangesAsync();
}
示例11: InitializeRoleAdmin
private async static void InitializeRoleAdmin(ApplicationDbContext context, RoleManager<IdentityRole> roleManager)
{
IdentityRole role = new IdentityRole { Name = "admin" };
await roleManager.CreateAsync(role);
context.SaveChanges();
}
示例12: AddRoles
private static void AddRoles(RoleManager<ApplicationRole> roleManager)
{
Task.FromResult(roleManager.CreateAsync(new ApplicationRole { Name = "Admin" }).Result);
Task.FromResult(roleManager.CreateAsync(new ApplicationRole { Name = "Esthetician" }).Result);
Task.FromResult(roleManager.CreateAsync(new ApplicationRole { Name = "Client" }).Result);
}
示例13: 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");
}
示例14: Register
public async Task<ActionResult> Register(RegisterViewModel model, HttpPostedFileBase upload)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Name = model.Name, Gender = model.Gender, DateofBirth = model.DateofBirth };
var result = await UserManager.CreateAsync(user, model.Password);
//File UPLOAD
if (upload != null && upload.ContentLength > 0)
{
var avatar = new File
{
FileName = System.IO.Path.GetFileName(upload.FileName),
FileType = FileType.Avatar,
ContentType = upload.ContentType
};
using (var reader = new System.IO.BinaryReader(upload.InputStream))
{
avatar.Content = reader.ReadBytes(upload.ContentLength);
}
user.Files = new List<File> { avatar };
}
if (result.Succeeded)
{
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
await roleManager.CreateAsync(new IdentityRole { Name = "Normal"});
var currentUser = UserManager.FindByName(user.UserName);
var roleresult = UserManager.AddToRole(currentUser.Id, "Normal");
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
示例15: 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