http://benfoster.io/blog/asp-net-identity-role-claims
Следуя приведенному выше уроку, я создал собственный метод настройки ниже:
public async void Setup()
{
var companyAdminRole = await roleManager.FindByNameAsync("CompanyAdmin");
if (companyAdminRole == null)
{
companyAdminRole = new IdentityRole("CompanyAdmin");
await roleManager.CreateAsync(companyAdminRole);
await roleManager.AddClaimAsync(companyAdminRole, new Claim("Permission", "company.view"));
await roleManager.AddClaimAsync(companyAdminRole, new Claim("Permission", "company.create"));
await roleManager.AddClaimAsync(companyAdminRole, new Claim("Permission", "company.update"));
await roleManager.AddClaimAsync(companyAdminRole, new Claim("Permission", "company.analitycs.view"));
await roleManager.AddClaimAsync(companyAdminRole, new Claim("Permission", "company.analitycs.download"));
}
var adminRole = await roleManager.FindByNameAsync("CompanyViewer");
if (adminRole == null)
{
adminRole = new IdentityRole("CompanyAdmin");
await roleManager.CreateAsync(adminRole);
await roleManager.AddClaimAsync(adminRole, new Claim("Permission", "company.view"));
await roleManager.AddClaimAsync(adminRole, new Claim("Permission", "company.create"));
await roleManager.AddClaimAsync(adminRole, new Claim("Permission", "company.update"));
await roleManager.AddClaimAsync(adminRole, new Claim("Permission", "company.analitycs.view"));
await roleManager.AddClaimAsync(adminRole, new Claim("Permission", "company.analitycs.download"));
}
var SystemAdminRole = await roleManager.FindByNameAsync("SystemAdmin");
if (SystemAdminRole == null)
{
SystemAdminRole = new IdentityRole("SystemAdmin");
await roleManager.CreateAsync(SystemAdminRole);
await roleManager.AddClaimAsync(SystemAdminRole, new Claim("Permission", "company.view.all"));
await roleManager.AddClaimAsync(SystemAdminRole, new Claim("Permission", "company.analitycs.view.all"));
await roleManager.AddClaimAsync(SystemAdminRole, new Claim("Permission", "company.analitycs.manage.all"));
}
ApplicationUser user = await userManager.FindByEmailAsync("system@administrator.com");
if (user == null)
{
user = new ApplicationUser
{
FirstName = "System",
LastName = "Administrator",
UserName = "system@administrator.com",
Email = "system@administrator.com",
CreationDate = DateTime.Now
};
var result = await userManager.CreateAsync(user, "Test@123");
}
if (!await userManager.IsInRoleAsync(user, SystemAdminRole.Name))
{
await userManager.AddToRoleAsync(user, SystemAdminRole.Name);
}
}
Дело в том, что я не хочу показывать этот метод настройки на контроллере для внешнего вызова системы.
Где лучшее место, где мне следует вызывать мой метод настройки?