public async Task <ApiEditRoleResponse> EditRole(ApiRole inputRole) { IdentityRole role = await _roleManager.FindByIdAsync(inputRole.Id); if (role == null) { throw new HttpStatusCodeException((int)HttpStatusCode.InternalServerError, "Role doesn't exist!"); } role.Name = inputRole.Name; IdentityResult result = await _roleManager.UpdateAsync(role); if (!result.Succeeded) { throw new HttpStatusCodeException((int)HttpStatusCode.InternalServerError, result.Errors.First().Description); } return(new ApiEditRoleResponse { Succeeded = true, Role = new ApiRole { Id = role.Id, Name = role.Name } }); }
public static async Task EnsureSeedData(IServiceProvider serviceProvider) { using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()) { scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate(); { var context = scope.ServiceProvider.GetService <ConfigurationDbContext>(); context.Database.Migrate(); EnsureSeedData(context).Wait(); } { var context = scope.ServiceProvider.GetRequiredService <ApiDbContext>(); context.Database.Migrate(); #region Ensure Users var userMgr = scope.ServiceProvider.GetRequiredService <UserManager <ApiUser> >(); var alice = await userMgr.FindByNameAsync("alice"); if (alice == null) { alice = new ApiUser { UserName = "******" }; var result = await userMgr.CreateAsync(alice, "Pass123$"); if (!result.Succeeded) { throw new Exception(result.Errors.First().Description); } result = await userMgr.AddClaimsAsync(alice, new Claim[] { new Claim(JwtClaimTypes.Name, "Alice Smith"), new Claim(JwtClaimTypes.GivenName, "Alice"), new Claim(JwtClaimTypes.FamilyName, "Smith"), new Claim(JwtClaimTypes.Email, "*****@*****.**"), new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean), new Claim(JwtClaimTypes.WebSite, "http://alice.com"), new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServerConstants.ClaimValueTypes.Json) }); if (!result.Succeeded) { throw new Exception(result.Errors.First().Description); } Console.WriteLine("alice created"); } else { Console.WriteLine("alice already exists"); } var bob = await userMgr.FindByNameAsync("bob"); if (bob == null) { bob = new ApiUser { UserName = "******" }; var result = await userMgr.CreateAsync(bob, "Pass123$"); if (!result.Succeeded) { throw new Exception(result.Errors.First().Description); } result = await userMgr.AddClaimsAsync(bob, new Claim[] { new Claim(JwtClaimTypes.Name, "Bob Smith"), new Claim(JwtClaimTypes.GivenName, "Bob"), new Claim(JwtClaimTypes.FamilyName, "Smith"), new Claim(JwtClaimTypes.Email, "*****@*****.**"), new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean), new Claim(JwtClaimTypes.WebSite, "http://bob.com"), new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServerConstants.ClaimValueTypes.Json), new Claim("location", "somewhere") }); if (!result.Succeeded) { throw new Exception(result.Errors.First().Description); } Console.WriteLine("bob created"); } else { Console.WriteLine("bob already exists"); } #endregion #region Ensure Roles var roleManager = scope.ServiceProvider.GetRequiredService <RoleManager <ApiRole> >(); var roleExists = await roleManager.RoleExistsAsync("superuser"); if (!roleExists) { var role = new ApiRole { Name = "superuser" }; var result = await roleManager.CreateAsync(role); if (result.Succeeded) { await userMgr.AddToRoleAsync(alice, role.Name); } } #endregion } } }