Created
January 17, 2015 22:12
-
-
Save trailmax/a69004170220b639880c to your computer and use it in GitHub Desktop.
IoC debugging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<ActionResult> DoSeed() | |
{ | |
return await InitializeIdentityForEF(db) | |
} | |
private async Task InitializeIdentityForEF(ApplicationDbContext db) | |
{ | |
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); | |
var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>(); | |
const string name = "XXX"; | |
const string password = "XXX"; | |
const string roleName = "XXX"; | |
//Create Role Admin if it does not exist | |
var role = await roleManager.FindByNameAsync(roleName); | |
if (role == null) | |
{ | |
role = new IdentityRole(roleName); | |
var roleresult = await roleManager.CreateAsync(role); | |
} | |
var user = await userManager.FindByNameAsync(name); | |
if (user == null) | |
{ | |
user = new ApplicationUser { UserName = name, Email = name }; | |
var result = await userManager.CreateAsync(user, password); | |
var result1 = await userManager.SetLockoutEnabledAsync(user.Id, false); | |
} | |
// Add user admin to Role Admin if not already added | |
var rolesForUser = async userManager.GetRolesAsync(user.Id); | |
if (!rolesForUser.Contains(role.Name)) | |
{ | |
var result2 = await userManager.AddToRoleAsync(user.Id, role.Name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment