protected override void Seed(AppIdentityDbContext context) { var userManager = new AppUserManager(new UserStore <AppUser>(context)); var roleManager = new AppRoleManager(new RoleStore <AppRole>(context)); if (!roleManager.RoleExists(SuperAdminRoleName)) { roleManager.Create(new AppRole(SuperAdminRoleName)); } var user = userManager.FindByName(SuperAdminUserName); if (user == null) { userManager.Create(new AppUser { UserName = SuperAdminUserName, Email = SuperAdminEmail }, SuperAdminPassword); user = userManager.FindByName(SuperAdminUserName); } if (!userManager.IsInRole(user.Id, SuperAdminRoleName)) { userManager.AddToRole(user.Id, SuperAdminRoleName); } context.SaveChanges(); }
protected override void Seed(AppIdentityDbContext context) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); string roleName = "Administrators"; string userName = "******"; string password = "******"; string email = " [email protected] "; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new AppRole(roleName)); } AppUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } context.SaveChanges(); }
public void PerformInitialSetup(AppIdentityDbContext context) { // настройки конфигурации контекста будут указываться здесь AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); string roleName = "Administrators"; string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new AppRole(roleName)); } AppUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } }
public void PerformInitialSetup(Store context) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); string roleName = "Administrators"; string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new AppRole(roleName)); } AppUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } }
protected override void Seed(AppIdentityDbContext context) { //seed admin AppUserManager userMgr = new AppUserManager(new UserStore <User>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <Role>(context)); string roleName = "Admin"; string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new Role(roleName)); } User user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new User { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } base.Seed(context); }
public void PerformInitialSetup(AppIdentityDbContext context) { var userMgr = new AppUserManager(new UserStore <AppUser>(context)); var roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); const string roleName = "Administrators"; const string userName = "******"; const string password = "******"; const string email = "*****@*****.**"; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new AppRole(roleName)); } var user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } }
private void SetDefaultAccountAndRole(Practice.Domain.Concrete.AppIdentityDbContext context) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); string roleName = "Administrators"; string userName = ""; string password = ""; string email = "@example.com"; int CityID = 1; int CountryID = 1; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new AppRole(roleName)); } AppUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = email, CityID = CityID, CountryID = CountryID }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } context.SaveChanges(); }
protected override void Seed(Infrastructure.Database.AppIdentityDbContext context) { AppUserManager userManager = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleManager = new AppRoleManager(new RoleStore <AppRole>(context)); string roleName = "Administrators"; string name = "Admin"; string password = "******"; string email = "*****@*****.**"; if (!roleManager.RoleExists(roleName)) { roleManager.Create(new AppRole(roleName)); } AppUser user = userManager.FindByName(name); if (user == null) { userManager.Create(new AppUser { Email = email, UserName = name }, password); user = userManager.FindByName(user.UserName); } if (!userManager.IsInRole(user.Id, roleName)) { userManager.AddToRole(user.Id, user.UserName); } foreach (AppUser dbUser in userManager.Users) { dbUser.City = Cities.PARIS; dbUser.Country = Countries.FRANCE; } context.SaveChanges(); }
private void CreateUsers(AppDbContext context) { var userManager = new AppUserManager(context); if (userManager.FindByName("admin") == null) { userManager.Create(new AppUser { UserName = "******", TimeZone = TimeZoneInfo.Utc.Id, CreatedOn = DateTimeOffset.UtcNow, Email = "*****@*****.**" }, "P@ssw0rd"); } if (userManager.FindByName("anonymous") == null) { userManager.Create(new AppUser { UserName = "******", TimeZone = TimeZoneInfo.Utc.Id, CreatedOn = DateTimeOffset.UtcNow, Email = "*****@*****.**", EmailConfirmed = false }); } }
public void PerformInitialSetup(AppIdentityDbContext context) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); string roleName1 = "Admin"; string roleName2 = "User"; string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleMgr.RoleExists(roleName1) && !roleMgr.RoleExists(roleName2)) { roleMgr.Create(new AppRole(roleName1)); roleMgr.Create(new AppRole(roleName2)); } AppUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName1) && !userMgr.IsInRole(user.Id, roleName2)) { userMgr.AddToRole(user.Id, roleName1); userMgr.AddToRole(user.Id, roleName2); } }
protected override void Seed(Ewu.WebUI.Infrastructure.Identity.AppIdentityDbContext context) { // This method will be called after migrating to the latest version. // 此方法将在迁移到最新版本时调用 // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. // 你可以使用DbSet<T>.AddOrUpdate()辅助器方法来避免创建重复的种子数据 //为新的属性值添加默认值 //获取用户和角色的管理器 AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); #region 默认用户信息 string roleName = "Admin"; string userName = "******"; string password = "******"; string eamil = "*****@*****.**"; #endregion //当前角色名不存在 if (!roleMgr.RoleExists(roleName)) { //根据默认角色名新建 roleMgr.Create(new AppRole(roleName)); } //根据用户名查找用户对象 AppUser user = userMgr.FindByName(userName); //不存在 if (user == null) { //根据用户默认信息创建 userMgr.Create(new AppUser { UserName = userName, Email = eamil }, password); user = userMgr.FindByName(userName); } //当前用户不存在默认的角色时 if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } //添加属性的默认值 foreach (AppUser dbUser in userMgr.Users) { dbUser.Gender = "保密"; dbUser.Signature = "Ta什么也没留下。"; dbUser.RealName = dbUser.UserName + "(用户名临时替代)"; dbUser.Age = 0; dbUser.IDCardNO = "未知"; dbUser.IDCardImageData = null; dbUser.IDCardImageMimeType = null; } context.SaveChanges(); }
protected override void Seed(Users.Infrastructure.AppIdentityDbContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); string roleName = "Administrators"; string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new AppRole(roleName)); } AppUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } foreach (AppUser dbUser in userMgr.Users) { dbUser.City = Cities.MOSCOW; } foreach (AppUser dbUser in userMgr.Users) { if (dbUser.Country == Countries.NONE) { dbUser.SetCountryFromCity(dbUser.City); } } context.SaveChanges(); }
protected override void Seed(PhotoAlbumContext context) { AppUserManager userManager = new AppUserManager(new UserStore <ApplicationUser, ApplicationRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>(context)); AppRoleManager roleManager = new AppRoleManager(new CustomRoleStore(context)); if (!roleManager.RoleExists("Admins")) { roleManager.Create(new ApplicationRole() { Name = "Administrators" }); } if (!roleManager.RoleExists("Users")) { roleManager.Create(new ApplicationRole() { Name = "Users" }); } var clientProfile = new ClientProfile() { Description = "Administrator's account", Photos = new List <Photo>(), }; context.ClientProfiles.Add(clientProfile); var user = userManager.FindByName("qwerty"); if (user == null) { userManager.Create(new ApplicationUser() { UserName = "******", Email = "*****@*****.**", ClientProfileId = clientProfile.Id, }, "qwerty123"); user = userManager.FindByName("qwerty"); } ; if (!userManager.IsInRole(user.Id, RoleName.Admin)) { userManager.AddToRole(user.Id, RoleName.Admin); } if (!userManager.IsInRole(user.Id, RoleName.User)) { userManager.AddToRole(user.Id, RoleName.User); } context.SaveChanges(); }
protected override void Seed(EasyLearningDB context) { context.Database.Log = s => Debug.WriteLine(s); // Create Admin User AppUserManager usermanager = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager rolemanager = new AppRoleManager(new RoleStore <AppRole>(context)); if (!rolemanager.RoleExists(Roles.Admin)) { rolemanager.Create(new AppRole(Roles.Admin)); } if (!rolemanager.RoleExists(Roles.Lecturer)) { rolemanager.Create(new AppRole(Roles.Lecturer)); } if (!rolemanager.RoleExists(Roles.Students)) { rolemanager.Create(new AppRole(Roles.Students)); } if (!rolemanager.RoleExists(Roles.Study)) { rolemanager.Create(new AppRole(Roles.Study)); } AppUser user = usermanager.FindByName(Owner.UserName); if (user == null) { usermanager.Create(new AppUser { DateOfBirth = Owner.DateOfBirth, Email = Owner.Email, FirstName = Owner.FirstName, Gender = Owner.Gender, LastName = Owner.LastName, EmailConfirmed = true, MiddleName = Owner.MiddleName, State = Owner.state, UserName = Owner.UserName, }, Owner.Password); user = usermanager.FindByName(Owner.UserName); } if (!usermanager.IsInRole(user.Id, Roles.Admin)) { usermanager.AddToRole(user.Id, Roles.Admin); } if (!usermanager.IsInRole(user.Id, Roles.Study)) { usermanager.AddToRole(user.Id, Roles.Study); } context.SaveChanges(); }
protected override void Seed(Users.Infrastructure.AppIdentityDbContext context) { var userMgr = new AppUserManager(new UserStore <AppUser>(context)); var roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); var roleName = "Administrators"; var userName = "******"; var password = "******"; var email = "*****@*****.**"; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new AppRole(roleName)); } var user = userMgr.FindByName(userName); if (user == null) { var result = userMgr.Create(new AppUser { UserName = userName, Email = email }, password); if (result.Succeeded) { user = userMgr.FindByName(userName); } else { var errorSB = new StringBuilder(); foreach (var error in result.Errors) { errorSB.AppendLine(error); } throw new Exception(errorSB.ToString()); } } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } foreach (var dbUser in userMgr.Users) { if (dbUser.Country == Countries.NONE) { dbUser.SetCountryFromCity(dbUser.City); } } context.SaveChanges(); }
public void PerformInitialSetup(AppIdentityDbContext context) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); string AdminRoleName = "Администратор"; string TeacherRoleName = "Преподаватель"; string StudentRoleName = "Ученик"; string ParentRoleName = "Родитель"; string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleMgr.RoleExists(AdminRoleName)) { roleMgr.Create(new AppRole(AdminRoleName)); } if (!roleMgr.RoleExists(TeacherRoleName)) { roleMgr.Create(new AppRole(TeacherRoleName)); } if (!roleMgr.RoleExists(StudentRoleName)) { roleMgr.Create(new AppRole(StudentRoleName)); } if (!roleMgr.RoleExists(ParentRoleName)) { roleMgr.Create(new AppRole(ParentRoleName)); } AppUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, AdminRoleName)) { userMgr.AddToRole(user.Id, AdminRoleName); } }
protected override void Seed(ChatMe.DataAccess.EF.ChatMeContext context) { var roleManager = new AppRoleManager(new RoleStore <Role>(context)); var userManager = new AppUserManager(new UserStore <User>(context)); if (!roleManager.RoleExists("admin")) { roleManager.Create(new Role { Name = "admin" }); } if (!roleManager.RoleExists("user")) { roleManager.Create(new Role { Name = "user" }); } if (userManager.FindByName("admin") == null) { var admin = new User { UserName = "******", Email = "*****@*****.**", UserInfo = new UserInfo { RegistrationDate = DateTime.Now } }; var adminPassword = "******"; userManager.Create(admin, adminPassword); userManager.AddToRole(admin.Id, "admin"); } }
public Result <int> ForgotPassword(ForgotPasswordViewModel model, ControllerBase controllerBase) { Result <int> result = new Result <int>(); Users user = AppUserManager.FindByName(model.UserName); if (user != null && user.Id > 0 && user.IsActive) { ForgotPasswordViewModel info = new ForgotPasswordViewModel(); string code = AppUserManager.GeneratePasswordResetToken(user.Id); var callbackUrl = Common.GetUrlHelper().Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Current.Request.Url.Scheme); info.Name = user.Name; info.Url = callbackUrl; info.UserName = user.UserName; info.Email = user.Email; string defaultPath = "~/Views/Templates/Default/ForgotPassword.cshtml"; string emailBody = Common.RenderRazorViewToString(defaultPath, info, controllerBase); AppUserManager.SendEmail(user.Id, "Reset Password", emailBody); user.IsPasswordResetRequested = true; AppUserManager.Update(user); } else { result.success = false; result.AddError("User does not exist in system"); } return(result); }
public async Task <IHttpActionResult> UpdateUser(UpdateUserBindingModel updateUserModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var u = AppUserManager.FindByName(updateUserModel.Username); if (updateUserModel.CustomerInfoId > 0) { u.CustomerInfoId = updateUserModel.CustomerInfoId; } else if (updateUserModel.PhoneNumber != null) { u.PhoneNumber = updateUserModel.PhoneNumber; } IdentityResult updateUserResult = await AppUserManager.UpdateAsync(u); if (updateUserResult.Succeeded) { Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = u.Id })); return(Created(locationHeader, TheModelFactory.Create(u))); } else { return(GetErrorResult(updateUserResult)); } }
private void InitUser() { var userStore = new UserStore <User>(new ApplicationContext()); var userManager = new AppUserManager(userStore); var role = new IdentityRole { Name = "Admin" }; var roleStore = new RoleStore <IdentityRole>(new ApplicationContext()); var roleManager = new RoleManager <IdentityRole>(roleStore); roleManager.Create(role); var user = new User { UserName = "******", Email = "*****@*****.**" }; var admin = new User { UserName = "******", Email = "*****@*****.**" }; userManager.Create(user, "Qwerty123"); userManager.Create(admin, "Ricardo123"); userManager.AddToRole(userManager.FindByName("ricardo").Id, "Admin"); }
private void InitUser() { var userStore = new UserStore <User>(new ApplicationContext()); var userManager = new AppUserManager(userStore); var role = new IdentityRole { Name = "Admin" }; var roleStore = new RoleStore <IdentityRole>(new ApplicationContext()); var roleManager = new RoleManager <IdentityRole>(roleStore); roleManager.Create(role); var user = new User { UserName = "******", Email = "*****@*****.**" }; var admin = new User { UserName = "******", Email = "*****@*****.**" }; userManager.Create(user, "Qwerty1"); userManager.Create(admin, "Qwerty1"); userManager.AddToRole(userManager.FindByName("sa").Id, "Admin"); }
public IHttpActionResult SubmitChatByMobile(CommentModel model) { try { var user = AppUserManager.FindByName(model.Mobile); if (user != null) { var res = _groupManager.SubmitChat(user.Id, model.Mobile, model.Comment); return(Json(_responseProvider.GenerateResponse(new List <string> { res }, "SubmitChatByMobile"))); } else { _responseProvider.SetBusinessMessage(new MessageResponse() { Message = getResource.getMessage("UserNotExist"), Type = ResponseTypes.Error }); return(Json(_responseProvider.GenerateBadRequestResponse())); } } catch (Exception e) { _logProvider.Log(Tag, "SubmitChatByMobile", e.Message); } return(Json(_responseProvider.GenerateUnknownErrorResponse())); }
protected override void Seed(AppIdentityDbContext context) { const string adminRoleName = IdentityDbInit.AdminRoleName; var userManager = new AppUserManager(new UserStore <AppUser>(context)); var roleManager = new AppRoleManager(new RoleStore <AppRole>(context)); const string userName = "******"; const string password = "******"; const string email = "*****@*****.**"; if (!roleManager.RoleExists(adminRoleName)) { roleManager.Create(new AppRole(adminRoleName)); } var user = userManager.FindByName(userName); if (user == null) { userManager.Create(new AppUser { UserName = userName, Email = email }, password); user = userManager.FindByName(userName); } if (!userManager.IsInRole(user.Id, adminRoleName)) { userManager.AddToRole(user.Id, adminRoleName); } foreach (var dbUser in userManager.Users) { if (dbUser.City == default(Cities)) { dbUser.City = Cities.Paris; } if (dbUser.Country == default(Countries)) { dbUser.SetCountryFromCity(dbUser.City); } } context.SaveChanges(); }
protected override void Seed(TestIdentity.Infrastructure.AppIdentityDbContext context) { // This method will be called after migrating to the latest version. AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); string roleName = "Administrators"; string userName = "******"; string password = "******"; string e_mail = "*****@*****.**"; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new AppRole(roleName)); } AppUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = e_mail }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } foreach (AppUser usr in userMgr.Users) { usr.City = Cities.MOSCOW; } foreach (AppUser usr in userMgr.Users) { if (usr.Country == Countries.NONE) { usr.SetCountryGromCity(usr.City); } } context.SaveChanges(); }
protected override void Seed(Users.Infrastructure.AppIdentityDbContext context) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); string roleName = "Administrators"; string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new AppRole(roleName)); } AppUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } foreach (AppUser dbUser in userMgr.Users) { dbUser.City = Cities.PARIS; } foreach (AppUser dbUser in userMgr.Users) { if (dbUser.Country == Countries.NONE) { dbUser.SetCountryFromCity(dbUser.City); } } context.SaveChanges(); }
public void PerformInitialSetup(BlogDbContext context) { AppUserManager userMgr = new AppUserManager(new UserStore <User>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <Domain.Dto.RoleDto>(context)); string roleName = "Administrators"; string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleMgr.RoleExists(roleName)) { roleMgr.Create(new Domain.Dto.RoleDto() { Name = roleName }); } User user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new User() { UserName = userName, Email = email, CreationTime = DateTime.Now }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, roleName)) { userMgr.AddToRole(user.Id, roleName); } //foreach (User mUser in userMgr.Users) // mUser.City = City.Paris; //context.SaveChanges(); foreach (User mUser in userMgr.Users) { if (mUser.Country == Country.None) { mUser.SetCountryByCity(mUser.City); } } }
public static void InitializeIdentityForEF(AppDbContext context) { var currentContext = HttpContext.Current; var ctx = currentContext.GetOwinContext(); AppUserManager userManager = ctx.Get <AppUserManager>(); AppRoleManager roleManager = ctx.Get <AppRoleManager>(); string roleName = "Administrator"; string firstName = "ufuk"; string lastName = "aytas"; string email = "*****@*****.**"; string password = "******"; var role = roleManager.FindByName(roleName); if (role == null) { role = new Role { Name = roleName }; roleManager.Create(role); } User user = userManager.FindByName(email); if (user == null) { user = new User { UserName = email, Email = email, EmailConfirmed = true, FirstName = firstName, LastName = lastName }; userManager.Create(user, password); userManager.SetLockoutEnabled(user.Id, false); } // Add user admin to Role Admin if not already added var rolesForUser = userManager.GetRoles(user.Id); if (!rolesForUser.Contains(roleName)) { userManager.AddToRole(user.Id, roleName); } }
protected override void Seed(BlogContext context) { var userManager = new AppUserManager(new UserStore <User>(context)); var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); userManager.PasswordValidator = new PasswordValidator { RequiredLength = 5, RequireNonLetterOrDigit = false, RequireDigit = false, RequireLowercase = false, RequireUppercase = false }; var roles = new List <IdentityRole> { new IdentityRole { Name = "admin" }, new IdentityRole { Name = "user" } }; foreach (var role in roles) { var existingRole = roleManager.FindByName(role.Name); if (existingRole == null) { roleManager.Create(role); } } var admin = new User { UserName = "******", Login = "******", EmailConfirmed = true, Id = Guid.NewGuid().ToString() }; string adminPassword = "******"; var adminUser = userManager.FindByName(admin.UserName); if (adminUser == null) { var result = userManager.Create(admin, adminPassword); userManager.SetLockoutEnabled(admin.Id, false); if (result.Succeeded) { userManager.AddToRole(admin.Id, roles[0].Name); } } base.Seed(context); }
private AppUser UserCreate(AppUserManager userManager, UserDto userDto) { var user = userManager.FindByName(userDto.Name); if (user == null) { userManager.Create(new AppUser { UserName = userDto.Name, Email = userDto.Email } , userDto.Password); user = userManager.FindByName(userDto.Name); } return(user); }
public void PerformInitialSetup(AppIdentityDbContext context) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(context)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <AppRole>(context)); string userName = "******"; string password = "******"; string email = "*****@*****.**"; if (!roleMgr.RoleExists(RoleName.Admin)) { roleMgr.Create(new AppRole(RoleName.Admin)); } if (!roleMgr.RoleExists(RoleName.User)) { roleMgr.Create(new AppRole(RoleName.User)); } AppUser user = userMgr.FindByName(userName); if (user == null) { userMgr.Create(new AppUser { UserName = userName, Email = email }, password); user = userMgr.FindByName(userName); } if (!userMgr.IsInRole(user.Id, RoleName.Admin)) { userMgr.AddToRole(user.Id, RoleName.Admin); } if (!userMgr.IsInRole(user.Id, RoleName.User)) { userMgr.AddToRole(user.Id, RoleName.User); } context.SaveChanges(); }