public IActionResult Register(Register obj) { if (ModelState.IsValid) { if (!roleManager.RoleExistsAsync("Admin").Result) { ApplicationIdentityRole role = new ApplicationIdentityRole(); role.Name = "Admin"; IdentityResult roleResult = roleManager.CreateAsync(role).Result; } ApplicationIdentityUser user = new ApplicationIdentityUser(); user.UserName = obj.UserName; user.Email = obj.Email; user.FirstName = obj.FirstName; user.LastName = obj.LastName; IdentityResult result = userManager.CreateAsync(user, obj.Password).Result; if (result.Succeeded) { userManager.AddToRoleAsync(user, "Admin").Wait(); return(RedirectToAction("Login", "Security")); } else { ModelState.AddModelError("", "User details were invalid"); } } return(View(obj)); }
public static ApplicationIdentityRole ToIdentityRole(this ApplicationRole applicationRole) { if (applicationRole == null) { return(null); } var identityRole = new ApplicationIdentityRole(); return(identityRole.CopyApplicationRoleProperties(applicationRole)); }
private async Task <DetailsUseCase.UserDetails.UserRole> BuildRole(ApplicationIdentityRole role, ApplicationUser user) { return(new DetailsUseCase.UserDetails.UserRole() { Name = role.Name, Description = role.Description, CanRevoke = (await _authorizer.Authorize(new RevokeRoleUseCase.Command() { UserID = user.Id, RoleName = role.Name })).Succeeded }); }
public IActionResult Register(RegisterViewModel obj) { if (ModelState.IsValid) { ApplicationIdentityUser user = new ApplicationIdentityUser { UserName = obj.UserName, Email = obj.Email, Name = obj.Name, Surname = obj.Surname, PhoneNumber = obj.PhoneNumber, }; IdentityResult result = userManager.CreateAsync(user, obj.Password).Result; //Create a unsecure user profile for this demo var unsecureUser = new UserProfile { FirstName = obj.Name, LastName = obj.Surname, Email = obj.Email, Password = obj.Password, IsAdmin = false }; _appDdContext.UserProfiles.Add(unsecureUser); _appDdContext.SaveChanges(); if (result.Succeeded) { if (!roleManager.RoleExistsAsync("NormalUser").Result) { ApplicationIdentityRole role = new ApplicationIdentityRole(); role.Name = "NormalUser"; role.Description = "Perform normal operations."; IdentityResult roleResult = roleManager. CreateAsync(role).Result; if (!roleResult.Succeeded) { ModelState.AddModelError("", "Error while creating role!"); return(View(obj)); } } userManager.AddToRoleAsync(user, "NormalUser").Wait(); return(RedirectToAction("Login", "Account")); } } return(View(obj)); }
//Create [email protected] with password=Admin@123456 in the Admin role public void InitializeIdentityForEF(AspnetIdentityWithOnionContext db) { // This is only for testing purpose const string name = "*****@*****.**"; const string password = "******"; const string roleName = "Admin"; var applicationRoleManager = IdentityFactory.CreateRoleManager(db); var applicationUserManager = IdentityFactory.CreateUserManager(db); //Create Role Admin if it does not exist var role = applicationRoleManager.FindByName(roleName); if (role == null) { role = new ApplicationIdentityRole { Name = roleName }; applicationRoleManager.Create(role); } var user = applicationUserManager.FindByName(name); if (user == null) { user = new ApplicationIdentityUser { UserName = name, Email = name }; applicationUserManager.Create(user, password); applicationUserManager.SetLockoutEnabled(user.Id, false); } // Add user admin to Role Admin if not already added var rolesForUser = applicationUserManager.GetRoles(user.Id); if (!rolesForUser.Contains(role.Name)) { applicationUserManager.AddToRole(user.Id, role.Name); } var context = new AspnetIdentityWithOnionContext("name=AppContext", new DebugLogger()); var image = new Image { Path = "http://lorempixel.com/400/200/" }; context.Set <Image>().Add(image); for (var i = 0; i < 100; i++) { context.Set <Product>().Add(new Product { Name = "My Product", Description = "My Product", Image = image }); } context.SaveChanges(); }
public async Task <IActionResult> AddRole([FromBody] RoleAddViewModel roleAddViewModel) { var role = new ApplicationIdentityRole { Id = roleAddViewModel.RoleID, Name = roleAddViewModel.RoleName }; var result = await _roleManager.CreateAsync(role); if (result.Succeeded) { return(Json("SUCCEED")); } return(Json("角色添加失败!")); }
public static ApplicationIdentityRole CopyApplicationRoleProperties(this ApplicationIdentityRole identityRole, ApplicationRole applicationRole) { if (identityRole == null) { return(null); } if (applicationRole == null) { return(null); } identityRole.Name = applicationRole.Name; identityRole.Id = applicationRole.Id; foreach (var userRole in applicationRole.Users) { identityRole.Users.Add(userRole.ToIdentityUserRole()); } return(identityRole); }
protected override void Seed(LuaBijouxContext context) { const string username = "******"; const string password = "******"; const string roleName = "Administrator"; var applicationRoleManager = IdentityFactory.CreateRoleManager(context); var applicationUserManager = IdentityFactory.CreateUserManager(context); //Create Role Admin if it does not exist var role = applicationRoleManager.FindByName(roleName); if (role == null) { role = new ApplicationIdentityRole { Name = roleName }; applicationRoleManager.Create(role); } var user = applicationUserManager.FindByName(username); if (user == null) { user = new ApplicationIdentityUser { UserName = username, Email = username }; applicationUserManager.Create(user, password); applicationUserManager.SetLockoutEnabled(user.Id, false); } // Add user admin to Role Admin if not already added var rolesForUser = applicationUserManager.GetRoles(user.Id); if (!rolesForUser.Contains(role.Name)) { applicationUserManager.AddToRole(user.Id, role.Name); } // var context = new LuaBijouxContext("name=AppContext", new DebugLogger()); context.SaveChanges(); }
private static ApplicationIdentityRole CopyApplicationRoleProperties(this ApplicationIdentityRole identityRole, ApplicationRole applicationRole) { if (applicationRole == null) { return(null); } if (identityRole == null) { return(null); } identityRole.Id = applicationRole.Id; identityRole.Name = applicationRole.Name; identityRole.Description = applicationRole.Description; identityRole.IsSystemConfig = applicationRole.IsSystemConfig; identityRole.InsertedOnUtc = applicationRole.InsertedOnUtc; foreach (var userRole in applicationRole.Users) { identityRole.Users.Add(userRole.ToApplicationIdentityUserRole()); } return(identityRole); }
public void InitializeIdentityForEf(ApplicationDbContext db) { // This is only for testing purpose const string name = "*****@*****.**"; const string password = "******"; const string roleName = "Admin"; var applicationRoleManager = IdentityFactory.CreateRoleManager(db); var applicationUserManager = IdentityFactory.CreateUserManager(db); //Create Role Admin if it does not exist var role = applicationRoleManager.FindByName(roleName); if (role == null) { role = new ApplicationIdentityRole { Name = roleName }; applicationRoleManager.Create(role); } var user = applicationUserManager.FindByName(name); if (user == null) { user = new ApplicationIdentityUser { UserName = name, Email = name }; applicationUserManager.Create(user, password); applicationUserManager.SetLockoutEnabled(user.Id, false); } // Add user admin to Role Admin if not already added var rolesForUser = applicationUserManager.GetRoles(user.Id); if (!rolesForUser.Contains(role.Name)) { applicationUserManager.AddToRole(user.Id, role.Name); } db.SaveChanges(); }
//Create [email protected] with password=Admin@123456 in the Admin role public void InitializeIdentityForEF(AspnetIdentityWithOnionContext db) { // This is only for testing purpose const string name = "*****@*****.**"; const string password = "******"; string[] roleName = new string[] { "Admin", "Anonim", "Manager", "User" }; var applicationRoleManager = IdentityFactory.CreateRoleManager(db); var applicationUserManager = IdentityFactory.CreateUserManager(db); //Create Role Admin if it does not exist ApplicationIdentityRole role; foreach (var item in roleName) { role = new ApplicationIdentityRole { Name = item }; applicationRoleManager.Create(role); role = null; } //var role = applicationRoleManager.FindByName(roleName); //if (role == null) //{ // role = new ApplicationIdentityRole { Name = roleName }; // applicationRoleManager.Create(role); //} var user = applicationUserManager.FindByName(name); if (user == null) { user = new ApplicationIdentityUser { UserName = name, Email = name }; applicationUserManager.Create(user, password); applicationUserManager.SetLockoutEnabled(user.Id, false); } // Add user admin to Role Admin if not already added var rolesForUser = applicationUserManager.GetRoles(user.Id); foreach (var item in roleName) { if (!rolesForUser.Contains(item)) { applicationUserManager.AddToRole(user.Id, item); } } var context = new AspnetIdentityWithOnionContext("name=AppContext", new DebugLogger()); //var image = new Image { Path = "http://lorempixel.com/400/200/" }; //context.Set<Image>().Add(image); //context.Set<Category>().Add(new Category { Name = "Cameras", IsActive = true, Path = "6a81942d-d825-4c18-97ca-4a5a906c68ad.jpg" }); //context.Set<Category>().Add(new Category { Name = "BagsCase", IsActive = true, Path = "49307dd0-7ada-4e12-b1e9-e5040269288f.jpg" }); //context.Set<Category>().Add(new Category { Name = "Accessuare", IsActive = true, Path = "1c68860a-3b9a-4278-90ce-0b0237ec4898.jpg" }); //context.Set<SubCategory>().Add(new SubCategory { Name = "SLR Cameras", }) //for (var i = 0; i < 10; i++) //{ // context.Set<Product>().Add(new Product { Name = "My Product", Price = 35}); //} //context.Set<Category>().Add(new Category { Name = "ForExample", IsActive = true }); //context.SaveChanges(); }
public static ApplicationRole CopyApplicationIdentityRoleProperties(this ApplicationRole applicationRole, ApplicationIdentityRole identityRole) { if (identityRole == null) { return(null); } if (applicationRole == null) { return(null); } applicationRole.Id = identityRole.Id; applicationRole.Name = identityRole.Name; applicationRole.Description = identityRole.Description; applicationRole.IsSystemConfig = identityRole.IsSystemConfig; applicationRole.InsertedOnUtc = identityRole.InsertedOnUtc; foreach (var userRole in identityRole.Users) { applicationRole.Users.Add(userRole.ToApplicationUserRole()); } return(applicationRole); }
public async Task <IActionResult> AddUser(AddUserViewModel addUserViewModel) { addUserViewModel.CreateTime = DateTime.Now.ToLocalTime(); var user = await _userManager.FindByNameAsync(addUserViewModel.UserName); if (user == null) { var result = await _userManager.CreateAsync(new ApplicationIdentityUser { UserName = addUserViewModel.UserName, CreateTime = addUserViewModel.CreateTime, Sex = addUserViewModel.Sex, Email = addUserViewModel.Email, NickName = addUserViewModel.NickName, IsInUsing = addUserViewModel.IsInUsing, RoleName = addUserViewModel.RoleName, UserDescription = addUserViewModel.UserDescription }, addUserViewModel.Password); if (result.Succeeded) { user = await _userManager.FindByNameAsync(addUserViewModel.UserName); if (user == null) { return(Json("失败")); } var role = await _roleManager.FindByIdAsync(addUserViewModel.RoleId.ToString()); if (role == null) { role = new ApplicationIdentityRole { Id = addUserViewModel.RoleId.ToString(), Name = addUserViewModel.RoleName }; var roleresult = await _roleManager.CreateAsync(role); if (roleresult.Succeeded) { role = await _roleManager.FindByIdAsync(role.Id); if (role != null) { var addToRoleResult = await _userManager.AddToRoleAsync(user, role.Name); if (addToRoleResult.Succeeded) { return(Json("成功")); } } } return(Json("失败")); } var addToRoleResult1 = await _userManager.AddToRoleAsync(user, role.Name); if (addToRoleResult1.Succeeded) { return(Json("成功")); } } } else { user.UserName = addUserViewModel.UserName; user.Email = addUserViewModel.Email; user.NickName = addUserViewModel.NickName; user.IsInUsing = addUserViewModel.IsInUsing; user.RoleName = addUserViewModel.RoleName; user.Sex = addUserViewModel.Sex; user.UserDescription = addUserViewModel.UserDescription; var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { if (addUserViewModel.RoleId == 0) { return(Json("成功")); } var userOldRoles = await _userManager.GetRolesAsync(user); if (userOldRoles.Count > 0) { var deleteFromRoleResult = await _userManager.RemoveFromRolesAsync(user, userOldRoles); if (!deleteFromRoleResult.Succeeded) { return(Json("失败")); } } var role = await _roleManager.FindByIdAsync(addUserViewModel.RoleId.ToString()); if (role == null) { role = new ApplicationIdentityRole { Id = addUserViewModel.RoleId.ToString(), Name = addUserViewModel.RoleName }; var roleResult = await _roleManager.CreateAsync(role); if (!roleResult.Succeeded) { return(Json("失败")); } var addToRoleResult = await _userManager.AddToRoleAsync(user, role.Name); if (addToRoleResult.Succeeded) { return(Json("成功")); } } else { var addToRoleResult = await _userManager.AddToRoleAsync(user, role.Name); if (addToRoleResult.Succeeded) { return(Json("成功")); } } } } return(Json("失败")); }