public string Create(AGSUserEntity user) { // creat the Identity User with the provided password EFApplicationUser appUser = new EFApplicationUser(); MapAGSUserEntityToEFApplicationUser(user, appUser); appUser.Id = CommonConstant.GenerateId(); // set the default password for the newly created user _ = _userManager.CreateAsync(appUser).Result; _ = _userManager.AddClaimsAsync(appUser, new Claim[] { new Claim(JwtClaimTypes.Name, user.Username), new Claim(JwtClaimTypes.Email, user.Email) }).Result; // update the associated groups if (user.GroupIds != null) { foreach (var groupId in user.GroupIds) { Console.WriteLine($"Adding group:{groupId}"); this.AddUserToGroup(appUser, groupId); } } return(appUser.Id); }
public void InitializeApplicationData() { // add all function claims into Database var ags_identity_constant_type = typeof(CommonConstant); var constant_fields = ags_identity_constant_type.GetFields(); foreach (var constant_field in constant_fields) { if (constant_field.Name.EndsWith("ClaimConstant")) { var claimValue = (string)(constant_field.GetValue(null)); _applicationDbContext.FunctionClaims.Add(new EFFunctionClaim() { Id = CommonConstant.GenerateId(), Name = claimValue }); } } // create admin user var userName = AGSAdminName; var email = _configuration["default_user_email"]; var userPassword = _configuration["default_user_password"]; var user = new EFApplicationUser { Id = CommonConstant.GenerateId(), UserName = userName, NormalizedEmail = email, NormalizedUserName = userName, Email = email, First_Name = "Tim", Last_Name = "Ng", Title = "Developer", SecurityStamp = CommonConstant.GenerateId(), // need to add this !!! }; _ = _userManager.CreateAsync(user, userPassword).Result; var group1 = new EFApplicationRole { Id = CommonConstant.GenerateId(), Name = "Group_1_Test", NormalizedName = "Group_1_Test", ConcurrencyStamp = CommonConstant.GenerateId() }; var group2 = new EFApplicationRole { Id = CommonConstant.GenerateId(), Name = "Group_2_Test", NormalizedName = "Group_2_Test", ConcurrencyStamp = CommonConstant.GenerateId() }; _ = _roleManager.CreateAsync(group1).Result; _ = _roleManager.CreateAsync(group2).Result; _applicationDbContext.SaveChanges(); }
public void AddUserToGroup(EFApplicationUser selectedUser, string groupId) { var selectedRole = _roleManager.FindByIdAsync(groupId).Result; if (selectedRole != null && selectedUser != null) { _ = _userManager.AddToRoleAsync(selectedUser, selectedRole.Name).Result; } }
public void MapAGSUserEntityToEFApplicationUser(AGSUserEntity userEntity, EFApplicationUser efApplicationUser) { efApplicationUser.Id = userEntity.Id; efApplicationUser.UserName = userEntity.Username; efApplicationUser.NormalizedUserName = userEntity.Username; efApplicationUser.Email = userEntity.Email; efApplicationUser.NormalizedEmail = userEntity.Email; efApplicationUser.SecurityStamp = userEntity.Id; efApplicationUser.First_Name = userEntity.First_Name; efApplicationUser.Last_Name = userEntity.Last_Name; efApplicationUser.Title = userEntity.Title; }
public AGSUserEntity GetAGSUserEntityFromEFApplicationUser(EFApplicationUser user) { var result = new AGSUserEntity() { Id = user.Id, Email = user.Email, Username = user.UserName, First_Name = user.First_Name, Last_Name = user.Last_Name, Title = user.Title, GroupIds = new List <string>() }; // get the associated groups var groupIds = this.GetGroupIdsByUser(user.Id); if (groupIds != null) { result.GroupIds = groupIds; } return(result); }