protected override void Seed(ApplicationDbContext context) { ApplicationUserManager userMgr = new ApplicationUserManager(new UserStore <ApplicationUser>(context)); StoreRoleManager roleMgr = new StoreRoleManager(new RoleStore <StoreRole>(context)); string roleName = "Administrators"; string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new StoreRole(roleName)); } ApplicationUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new ApplicationUser { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } base.Seed(context); }
// GET api/values public async Task <IEnumerable <string> > Get() { // Именно тут я нафигачу 3 свои проверки ApplicationDbContext context = new ApplicationDbContext(); ApplicationUserManager userMgr = new ApplicationUserManager(new UserStore <ApplicationUser>(context)); StoreRoleManager roleMgr = new StoreRoleManager(new RoleStore <StoreRole>(context)); //var id = HttpContext.Current.User.Identity.GetUserId(); //userMgr.UpdateSecurityStamp(id); // create //string roleName = "Administrators"; //string userName = "******"; //string password = "******"; //string email = "*****@*****.**"; //roleMgr.Create(new StoreRole(roleName)); //userMgr.Create(new ApplicationUser //{ // UserName = userName, // Email = email //}, password); //ApplicationUser user = userMgr.FindByName(userName); //userMgr.AddToRole(user.Id, roleName); //// просто обновить что-то //ApplicationUser user = context.Users.First(x => x.UserName == "Admin"); //user.UserName = "******"; //userMgr.Update(user); //// сменить пароль - тупо, но работает //ApplicationUser user = context.Users.First(x => x.UserName == "Admin2"); //string pas = "******"; //ApplicationUser cUser = userMgr.FindById(user.Id); //await userMgr.RemovePasswordAsync(user.Id); //userMgr.AddPassword(user.Id, pas); return(new string[] { "value1", "value2" }); }