3 changed files with 1066 additions and 329 deletions
@ -1,329 +1,329 @@ |
|||||
using LINGYUN.Abp.IdentityServer; |
using LINGYUN.Abp.IdentityServer; |
||||
using Microsoft.Extensions.Configuration; |
using Microsoft.Extensions.Configuration; |
||||
using System; |
using System; |
||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
using System.IO; |
using System.IO; |
||||
using System.Linq; |
using System.Linq; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using Volo.Abp.Authorization.Permissions; |
using Volo.Abp.Authorization.Permissions; |
||||
using Volo.Abp.Data; |
using Volo.Abp.Data; |
||||
using Volo.Abp.DependencyInjection; |
using Volo.Abp.DependencyInjection; |
||||
using Volo.Abp.Guids; |
using Volo.Abp.Guids; |
||||
using Volo.Abp.IdentityServer.ApiResources; |
using Volo.Abp.IdentityServer.ApiResources; |
||||
using Volo.Abp.IdentityServer.ApiScopes; |
using Volo.Abp.IdentityServer.ApiScopes; |
||||
using Volo.Abp.IdentityServer.Clients; |
using Volo.Abp.IdentityServer.Clients; |
||||
using Volo.Abp.IdentityServer.IdentityResources; |
using Volo.Abp.IdentityServer.IdentityResources; |
||||
using Volo.Abp.MultiTenancy; |
using Volo.Abp.MultiTenancy; |
||||
using Volo.Abp.PermissionManagement; |
using Volo.Abp.PermissionManagement; |
||||
using Volo.Abp.Uow; |
using Volo.Abp.Uow; |
||||
|
|
||||
namespace AuthServer.DataSeeder |
namespace AuthServer.DataSeeder |
||||
{ |
{ |
||||
public class IdentityServerDataSeedContributor : IDataSeedContributor, ITransientDependency |
public class IdentityServerDataSeedContributor : IDataSeedContributor, ITransientDependency |
||||
{ |
{ |
||||
private readonly IApiResourceRepository _apiResourceRepository; |
private readonly IApiResourceRepository _apiResourceRepository; |
||||
private readonly IApiScopeRepository _apiScopeRepository; |
private readonly IApiScopeRepository _apiScopeRepository; |
||||
private readonly IClientRepository _clientRepository; |
private readonly IClientRepository _clientRepository; |
||||
private readonly IIdentityResourceDataSeeder _identityResourceDataSeeder; |
private readonly IIdentityResourceDataSeeder _identityResourceDataSeeder; |
||||
private readonly IWeChatResourceDataSeeder _weChatResourceDataSeeder; |
private readonly IWeChatResourceDataSeeder _weChatResourceDataSeeder; |
||||
private readonly IGuidGenerator _guidGenerator; |
private readonly IGuidGenerator _guidGenerator; |
||||
private readonly IPermissionDataSeeder _permissionDataSeeder; |
private readonly IPermissionDataSeeder _permissionDataSeeder; |
||||
private readonly IConfiguration _configuration; |
private readonly IConfiguration _configuration; |
||||
private readonly ICurrentTenant _currentTenant; |
private readonly ICurrentTenant _currentTenant; |
||||
|
|
||||
public IdentityServerDataSeedContributor( |
public IdentityServerDataSeedContributor( |
||||
IClientRepository clientRepository, |
IClientRepository clientRepository, |
||||
IApiScopeRepository apiScopeRepository, |
IApiScopeRepository apiScopeRepository, |
||||
IPermissionDataSeeder permissionDataSeeder, |
IPermissionDataSeeder permissionDataSeeder, |
||||
IApiResourceRepository apiResourceRepository, |
IApiResourceRepository apiResourceRepository, |
||||
IWeChatResourceDataSeeder weChatResourceDataSeeder, |
IWeChatResourceDataSeeder weChatResourceDataSeeder, |
||||
IIdentityResourceDataSeeder identityResourceDataSeeder, |
IIdentityResourceDataSeeder identityResourceDataSeeder, |
||||
IGuidGenerator guidGenerator, |
IGuidGenerator guidGenerator, |
||||
ICurrentTenant currentTenant) |
ICurrentTenant currentTenant) |
||||
{ |
{ |
||||
_currentTenant = currentTenant; |
_currentTenant = currentTenant; |
||||
_clientRepository = clientRepository; |
_clientRepository = clientRepository; |
||||
_permissionDataSeeder = permissionDataSeeder; |
_permissionDataSeeder = permissionDataSeeder; |
||||
_apiScopeRepository = apiScopeRepository; |
_apiScopeRepository = apiScopeRepository; |
||||
_apiResourceRepository = apiResourceRepository; |
_apiResourceRepository = apiResourceRepository; |
||||
_weChatResourceDataSeeder = weChatResourceDataSeeder; |
_weChatResourceDataSeeder = weChatResourceDataSeeder; |
||||
_identityResourceDataSeeder = identityResourceDataSeeder; |
_identityResourceDataSeeder = identityResourceDataSeeder; |
||||
_guidGenerator = guidGenerator; |
_guidGenerator = guidGenerator; |
||||
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"; |
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"; |
||||
var configuration = new ConfigurationBuilder() |
var configuration = new ConfigurationBuilder() |
||||
.SetBasePath(Directory.GetCurrentDirectory()) |
.SetBasePath(Directory.GetCurrentDirectory()) |
||||
.AddJsonFile($"appsettings.{env}.json", optional: false, reloadOnChange: true) |
.AddJsonFile($"appsettings.{env}.json", optional: false, reloadOnChange: true) |
||||
.AddEnvironmentVariables() |
.AddEnvironmentVariables() |
||||
.Build(); |
.Build(); |
||||
_configuration = configuration; |
_configuration = configuration; |
||||
} |
} |
||||
|
|
||||
[UnitOfWork] |
[UnitOfWork] |
||||
public virtual async Task SeedAsync(DataSeedContext context) |
public virtual async Task SeedAsync(DataSeedContext context) |
||||
{ |
{ |
||||
using (_currentTenant.Change(context?.TenantId)) |
using (_currentTenant.Change(context?.TenantId)) |
||||
{ |
{ |
||||
await _identityResourceDataSeeder.CreateStandardResourcesAsync(); |
await _identityResourceDataSeeder.CreateStandardResourcesAsync(); |
||||
await CreateWeChatClaimTypeAsync(); |
await CreateWeChatClaimTypeAsync(); |
||||
await CreateApiResourcesAsync(); |
await CreateApiResourcesAsync(); |
||||
await CreateApiScopesAsync(); |
await CreateApiScopesAsync(); |
||||
await CreateClientsAsync(); |
await CreateClientsAsync(); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
private async Task CreateWeChatClaimTypeAsync() |
private async Task CreateWeChatClaimTypeAsync() |
||||
{ |
{ |
||||
await _weChatResourceDataSeeder.CreateStandardResourcesAsync(); |
await _weChatResourceDataSeeder.CreateStandardResourcesAsync(); |
||||
} |
} |
||||
|
|
||||
private async Task CreateApiScopesAsync() |
private async Task CreateApiScopesAsync() |
||||
{ |
{ |
||||
await CreateApiScopeAsync("lingyun-abp-application"); |
await CreateApiScopeAsync("lingyun-abp-application"); |
||||
} |
} |
||||
|
|
||||
private async Task CreateApiResourcesAsync() |
private async Task CreateApiResourcesAsync() |
||||
{ |
{ |
||||
var commonApiUserClaims = new[] |
var commonApiUserClaims = new[] |
||||
{ |
{ |
||||
"email", |
"email", |
||||
"email_verified", |
"email_verified", |
||||
"name", |
"name", |
||||
"phone_number", |
"phone_number", |
||||
"phone_number_verified", |
"phone_number_verified", |
||||
"role" |
"role" |
||||
}; |
}; |
||||
|
|
||||
await CreateApiResourceAsync("lingyun-abp-application", commonApiUserClaims); |
await CreateApiResourceAsync("lingyun-abp-application", commonApiUserClaims); |
||||
} |
} |
||||
|
|
||||
private async Task<ApiResource> CreateApiResourceAsync(string name, IEnumerable<string> claims, IEnumerable<string> secrets = null) |
private async Task<ApiResource> CreateApiResourceAsync(string name, IEnumerable<string> claims, IEnumerable<string> secrets = null) |
||||
{ |
{ |
||||
var apiResource = await _apiResourceRepository.FindByNameAsync(name); |
var apiResource = await _apiResourceRepository.FindByNameAsync(name); |
||||
if (apiResource == null) |
if (apiResource == null) |
||||
{ |
{ |
||||
apiResource = await _apiResourceRepository.InsertAsync( |
apiResource = await _apiResourceRepository.InsertAsync( |
||||
new ApiResource( |
new ApiResource( |
||||
_guidGenerator.Create(), |
_guidGenerator.Create(), |
||||
name, |
name, |
||||
name + " API" |
name + " API" |
||||
), |
), |
||||
autoSave: true |
autoSave: true |
||||
); |
); |
||||
} |
} |
||||
|
|
||||
foreach (var claim in claims) |
foreach (var claim in claims) |
||||
{ |
{ |
||||
if (apiResource.FindClaim(claim) == null) |
if (apiResource.FindClaim(claim) == null) |
||||
{ |
{ |
||||
apiResource.AddUserClaim(claim); |
apiResource.AddUserClaim(claim); |
||||
} |
} |
||||
} |
} |
||||
if (secrets != null) |
if (secrets != null) |
||||
{ |
{ |
||||
foreach (var secret in secrets) |
foreach (var secret in secrets) |
||||
{ |
{ |
||||
if (apiResource.FindSecret(secret) == null) |
if (apiResource.FindSecret(secret) == null) |
||||
{ |
{ |
||||
apiResource.AddSecret(secret); |
apiResource.AddSecret(secret); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
return await _apiResourceRepository.UpdateAsync(apiResource); |
return await _apiResourceRepository.UpdateAsync(apiResource); |
||||
} |
} |
||||
|
|
||||
private async Task<ApiScope> CreateApiScopeAsync(string name) |
private async Task<ApiScope> CreateApiScopeAsync(string name) |
||||
{ |
{ |
||||
var apiScope = await _apiScopeRepository.GetByNameAsync(name); |
var apiScope = await _apiScopeRepository.GetByNameAsync(name); |
||||
if (apiScope == null) |
if (apiScope == null) |
||||
{ |
{ |
||||
apiScope = await _apiScopeRepository.InsertAsync( |
apiScope = await _apiScopeRepository.InsertAsync( |
||||
new ApiScope( |
new ApiScope( |
||||
_guidGenerator.Create(), |
_guidGenerator.Create(), |
||||
name, |
name, |
||||
name + " API" |
name + " API" |
||||
), |
), |
||||
autoSave: true |
autoSave: true |
||||
); |
); |
||||
} |
} |
||||
|
|
||||
return apiScope; |
return apiScope; |
||||
} |
} |
||||
|
|
||||
private async Task CreateClientsAsync() |
private async Task CreateClientsAsync() |
||||
{ |
{ |
||||
|
|
||||
string commonSecret = IdentityServer4.Models.HashExtensions.Sha256("1q2w3e*"); |
string commonSecret = IdentityServer4.Models.HashExtensions.Sha256("1q2w3e*"); |
||||
|
|
||||
var commonScopes = new[] |
var commonScopes = new[] |
||||
{ |
{ |
||||
"email", |
"email", |
||||
"openid", |
"openid", |
||||
"profile", |
"profile", |
||||
"role", |
"role", |
||||
"phone", |
"phone", |
||||
"address", |
"address", |
||||
"offline_access" // 加上刷新,
|
"offline_access" // 加上刷新,
|
||||
|
|
||||
}; |
}; |
||||
|
|
||||
var configurationSection = _configuration.GetSection("IdentityServer:Clients"); |
var configurationSection = _configuration.GetSection("IdentityServer:Clients"); |
||||
|
|
||||
//Web Client
|
//Web Client
|
||||
var webClientId = configurationSection["AuthManagement:ClientId"]; |
var webClientId = configurationSection["AuthManagement:ClientId"]; |
||||
if (!webClientId.IsNullOrWhiteSpace()) |
if (!webClientId.IsNullOrWhiteSpace()) |
||||
{ |
{ |
||||
var webClientRootUrl = configurationSection["AuthManagement:RootUrl"].EnsureEndsWith('/'); |
var webClientRootUrl = configurationSection["AuthManagement:RootUrl"].EnsureEndsWith('/'); |
||||
await CreateClientAsync( |
await CreateClientAsync( |
||||
webClientId, |
webClientId, |
||||
commonScopes.Union(new[] { "lingyun-abp-application" }), |
commonScopes.Union(new[] { "lingyun-abp-application" }), |
||||
new[] { "hybrid" }, |
new[] { "hybrid" }, |
||||
commonSecret, |
commonSecret, |
||||
redirectUri: $"{webClientRootUrl}signin-oidc", |
redirectUri: $"{webClientRootUrl}signin-oidc", |
||||
postLogoutRedirectUri: $"{webClientRootUrl}signout-callback-oidc", |
postLogoutRedirectUri: $"{webClientRootUrl}signout-callback-oidc", |
||||
corsOrigins: configurationSection["CorsOrigins"] |
corsOrigins: configurationSection["CorsOrigins"] |
||||
); |
); |
||||
} |
} |
||||
|
|
||||
//Console Test Client
|
//Console Test Client
|
||||
var consoleClientId = configurationSection["AuthVueAdmin:ClientId"]; |
var consoleClientId = configurationSection["AuthVueAdmin:ClientId"]; |
||||
if (!consoleClientId.IsNullOrWhiteSpace()) |
if (!consoleClientId.IsNullOrWhiteSpace()) |
||||
{ |
{ |
||||
await CreateClientAsync( |
await CreateClientAsync( |
||||
consoleClientId, |
consoleClientId, |
||||
commonScopes.Union(new[] { "lingyun-abp-application" }), |
commonScopes.Union(new[] { "lingyun-abp-application" }), |
||||
new[] { "password", "client_credentials" }, |
new[] { "password", "client_credentials" }, |
||||
commonSecret |
commonSecret |
||||
); |
); |
||||
} |
} |
||||
|
|
||||
//ApiGateway
|
//ApiGateway
|
||||
var apigatewayClientId = configurationSection["AuthApiGateway:ClientId"]; |
var apigatewayClientId = configurationSection["AuthApiGateway:ClientId"]; |
||||
if (!apigatewayClientId.IsNullOrWhiteSpace()) |
if (!apigatewayClientId.IsNullOrWhiteSpace()) |
||||
{ |
{ |
||||
var apigatewayPermissions = new string[8] |
var apigatewayPermissions = new string[8] |
||||
{ |
{ |
||||
"ApiGateway.Global", "ApiGateway.Global.Export", |
"ApiGateway.Global", "ApiGateway.Global.Export", |
||||
"ApiGateway.Route", "ApiGateway.Route.Export", |
"ApiGateway.Route", "ApiGateway.Route.Export", |
||||
"ApiGateway.DynamicRoute", "ApiGateway.DynamicRoute.Export", |
"ApiGateway.DynamicRoute", "ApiGateway.DynamicRoute.Export", |
||||
"ApiGateway.AggregateRoute", "ApiGateway.AggregateRoute.Export", |
"ApiGateway.AggregateRoute", "ApiGateway.AggregateRoute.Export", |
||||
}; |
}; |
||||
await CreateClientAsync( |
await CreateClientAsync( |
||||
apigatewayClientId, |
apigatewayClientId, |
||||
commonScopes.Union(new[] { "lingyun-abp-application" }), |
commonScopes.Union(new[] { "lingyun-abp-application" }), |
||||
new[] { "client_credentials" }, |
new[] { "client_credentials" }, |
||||
commonSecret, |
commonSecret, |
||||
permissions: apigatewayPermissions |
permissions: apigatewayPermissions |
||||
); |
); |
||||
} |
} |
||||
|
|
||||
// InternalService 内部服务间通讯客户端,必要的话需要在前端指定它拥有所有权限,当前项目仅预置用户查询权限
|
// InternalService 内部服务间通讯客户端,必要的话需要在前端指定它拥有所有权限,当前项目仅预置用户查询权限
|
||||
var internalServiceClientId = configurationSection["InternalService:ClientId"]; |
var internalServiceClientId = configurationSection["InternalService:ClientId"]; |
||||
if (!internalServiceClientId.IsNullOrWhiteSpace()) |
if (!internalServiceClientId.IsNullOrWhiteSpace()) |
||||
{ |
{ |
||||
var internalServicePermissions = new string[2] |
var internalServicePermissions = new string[2] |
||||
{ |
{ |
||||
"AbpIdentity.UserLookup","AbpIdentity.Users" |
"AbpIdentity.UserLookup","AbpIdentity.Users" |
||||
}; |
}; |
||||
await CreateClientAsync( |
await CreateClientAsync( |
||||
internalServiceClientId, |
internalServiceClientId, |
||||
commonScopes.Union(new[] { "lingyun-abp-application" }), |
commonScopes.Union(new[] { "lingyun-abp-application" }), |
||||
new[] { "client_credentials" }, |
new[] { "client_credentials" }, |
||||
commonSecret, |
commonSecret, |
||||
permissions: internalServicePermissions |
permissions: internalServicePermissions |
||||
); |
); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
private async Task<Client> CreateClientAsync( |
private async Task<Client> CreateClientAsync( |
||||
string name, |
string name, |
||||
IEnumerable<string> scopes, |
IEnumerable<string> scopes, |
||||
IEnumerable<string> grantTypes, |
IEnumerable<string> grantTypes, |
||||
string secret, |
string secret, |
||||
string redirectUri = null, |
string redirectUri = null, |
||||
string postLogoutRedirectUri = null, |
string postLogoutRedirectUri = null, |
||||
IEnumerable<string> permissions = null, |
IEnumerable<string> permissions = null, |
||||
string corsOrigins = null) |
string corsOrigins = null) |
||||
{ |
{ |
||||
var client = await _clientRepository.FindByClientIdAsync(name); |
var client = await _clientRepository.FindByClientIdAsync(name); |
||||
if (client == null) |
if (client == null) |
||||
{ |
{ |
||||
client = await _clientRepository.InsertAsync( |
client = await _clientRepository.InsertAsync( |
||||
new Client( |
new Client( |
||||
_guidGenerator.Create(), |
_guidGenerator.Create(), |
||||
name |
name |
||||
) |
) |
||||
{ |
{ |
||||
ClientName = name, |
ClientName = name, |
||||
ProtocolType = "oidc", |
ProtocolType = "oidc", |
||||
Description = name, |
Description = name, |
||||
AlwaysIncludeUserClaimsInIdToken = true, |
AlwaysIncludeUserClaimsInIdToken = true, |
||||
AllowOfflineAccess = true, |
AllowOfflineAccess = true, |
||||
AbsoluteRefreshTokenLifetime = 10800, //3 hours
|
AbsoluteRefreshTokenLifetime = 10800, //3 hours
|
||||
AccessTokenLifetime = 7200, //2 hours
|
AccessTokenLifetime = 7200, //2 hours
|
||||
AuthorizationCodeLifetime = 300, |
AuthorizationCodeLifetime = 300, |
||||
IdentityTokenLifetime = 300, |
IdentityTokenLifetime = 300, |
||||
RequireConsent = false |
RequireConsent = false |
||||
}, |
}, |
||||
autoSave: true |
autoSave: true |
||||
); |
); |
||||
} |
} |
||||
|
|
||||
foreach (var scope in scopes) |
foreach (var scope in scopes) |
||||
{ |
{ |
||||
if (client.FindScope(scope) == null) |
if (client.FindScope(scope) == null) |
||||
{ |
{ |
||||
client.AddScope(scope); |
client.AddScope(scope); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
foreach (var grantType in grantTypes) |
foreach (var grantType in grantTypes) |
||||
{ |
{ |
||||
if (client.FindGrantType(grantType) == null) |
if (client.FindGrantType(grantType) == null) |
||||
{ |
{ |
||||
client.AddGrantType(grantType); |
client.AddGrantType(grantType); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
if (client.FindSecret(secret) == null) |
if (client.FindSecret(secret) == null) |
||||
{ |
{ |
||||
client.AddSecret(secret); |
client.AddSecret(secret); |
||||
} |
} |
||||
|
|
||||
if (redirectUri != null) |
if (redirectUri != null) |
||||
{ |
{ |
||||
if (client.FindRedirectUri(redirectUri) == null) |
if (client.FindRedirectUri(redirectUri) == null) |
||||
{ |
{ |
||||
client.AddRedirectUri(redirectUri); |
client.AddRedirectUri(redirectUri); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
if (postLogoutRedirectUri != null) |
if (postLogoutRedirectUri != null) |
||||
{ |
{ |
||||
if (client.FindPostLogoutRedirectUri(postLogoutRedirectUri) == null) |
if (client.FindPostLogoutRedirectUri(postLogoutRedirectUri) == null) |
||||
{ |
{ |
||||
client.AddPostLogoutRedirectUri(postLogoutRedirectUri); |
client.AddPostLogoutRedirectUri(postLogoutRedirectUri); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
if (corsOrigins != null) |
if (corsOrigins != null) |
||||
{ |
{ |
||||
var corsOriginsSplit = corsOrigins.Split(";"); |
var corsOriginsSplit = corsOrigins.Split(";"); |
||||
foreach (var corsOrigin in corsOriginsSplit) |
foreach (var corsOrigin in corsOriginsSplit) |
||||
{ |
{ |
||||
if (client.FindCorsOrigin(corsOrigin) == null) |
if (client.FindCorsOrigin(corsOrigin) == null) |
||||
{ |
{ |
||||
client.AddCorsOrigin(corsOrigin); |
client.AddCorsOrigin(corsOrigin); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
if(permissions != null) |
if(permissions != null) |
||||
{ |
{ |
||||
await _permissionDataSeeder.SeedAsync(ClientPermissionValueProvider.ProviderName, name, permissions); |
await _permissionDataSeeder.SeedAsync(ClientPermissionValueProvider.ProviderName, name, permissions); |
||||
} |
} |
||||
|
|
||||
return await _clientRepository.UpdateAsync(client); |
return await _clientRepository.UpdateAsync(client); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,113 @@ |
|||||
|
|
||||
|
using Microsoft.AspNetCore.Identity; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Guids; |
||||
|
using Volo.Abp.Identity; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
using Volo.Abp.Uow; |
||||
|
using IdentityRole = Volo.Abp.Identity.IdentityRole; |
||||
|
using IdentityUser = Volo.Abp.Identity.IdentityUser; |
||||
|
|
||||
|
namespace AuthServer.DataSeeder |
||||
|
{ |
||||
|
public class IdentityServerExtendUserDataSeedContributor : IDataSeedContributor, ITransientDependency |
||||
|
{ |
||||
|
public const string AdminEmailPropertyName = "AdminEmail"; |
||||
|
public const string AdminEmailDefaultValue = "vben@abp.io"; |
||||
|
public const string AdminPasswordPropertyName = "AdminPassword"; |
||||
|
public const string AdminPasswordDefaultValue = "1q2w3E*"; |
||||
|
public const string AdminRolePropertyName = "AdminRole"; |
||||
|
public const string AdminRoleDefaultValue = "vben-admin"; |
||||
|
|
||||
|
protected IGuidGenerator GuidGenerator { get; } |
||||
|
protected IIdentityRoleRepository RoleRepository { get; } |
||||
|
protected IIdentityUserRepository UserRepository { get; } |
||||
|
protected ILookupNormalizer LookupNormalizer { get; } |
||||
|
protected IdentityUserManager UserManager { get; } |
||||
|
protected IdentityRoleManager RoleManager { get; } |
||||
|
protected ICurrentTenant CurrentTenant { get; } |
||||
|
protected IOptions<IdentityOptions> IdentityOptions { get; } |
||||
|
|
||||
|
public IdentityServerExtendUserDataSeedContributor( |
||||
|
IGuidGenerator guidGenerator, |
||||
|
IIdentityRoleRepository roleRepository, |
||||
|
IIdentityUserRepository userRepository, |
||||
|
ILookupNormalizer lookupNormalizer, |
||||
|
IdentityUserManager userManager, |
||||
|
IdentityRoleManager roleManager, |
||||
|
ICurrentTenant currentTenant, |
||||
|
IOptions<IdentityOptions> identityOptions) |
||||
|
{ |
||||
|
GuidGenerator = guidGenerator; |
||||
|
RoleRepository = roleRepository; |
||||
|
UserRepository = userRepository; |
||||
|
LookupNormalizer = lookupNormalizer; |
||||
|
UserManager = userManager; |
||||
|
RoleManager = roleManager; |
||||
|
CurrentTenant = currentTenant; |
||||
|
IdentityOptions = identityOptions; |
||||
|
} |
||||
|
|
||||
|
[UnitOfWork] |
||||
|
public virtual async Task SeedAsync(DataSeedContext context) |
||||
|
{ |
||||
|
using (CurrentTenant.Change(context.TenantId)) |
||||
|
{ |
||||
|
await IdentityOptions.SetAsync(); |
||||
|
|
||||
|
var result = new IdentityDataSeedResult(); |
||||
|
//"admin" user
|
||||
|
const string adminUserName = "vben"; |
||||
|
var adminEmail = context?[AdminEmailPropertyName] as string ?? AdminEmailDefaultValue; |
||||
|
var adminPassword = context?[AdminPasswordPropertyName] as string ?? AdminPasswordDefaultValue; |
||||
|
|
||||
|
var adminUser = await UserRepository.FindByNormalizedUserNameAsync( |
||||
|
LookupNormalizer.NormalizeName(adminUserName) |
||||
|
); |
||||
|
|
||||
|
if (adminUser != null) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
adminUser = new IdentityUser( |
||||
|
GuidGenerator.Create(), |
||||
|
adminUserName, |
||||
|
adminEmail, |
||||
|
context.TenantId |
||||
|
) |
||||
|
{ |
||||
|
Name = adminUserName |
||||
|
}; |
||||
|
|
||||
|
(await UserManager.CreateAsync(adminUser, adminPassword, validatePassword: false)).CheckErrors(); |
||||
|
result.CreatedAdminUser = true; |
||||
|
|
||||
|
//"admin" role
|
||||
|
var adminRoleName = context?[AdminRolePropertyName] as string ?? AdminRoleDefaultValue; |
||||
|
var adminRole = |
||||
|
await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(adminRoleName)); |
||||
|
if (adminRole == null) |
||||
|
{ |
||||
|
adminRole = new IdentityRole( |
||||
|
GuidGenerator.Create(), |
||||
|
adminRoleName, |
||||
|
context.TenantId |
||||
|
) |
||||
|
{ |
||||
|
IsStatic = true, |
||||
|
IsPublic = true |
||||
|
}; |
||||
|
|
||||
|
(await RoleManager.CreateAsync(adminRole)).CheckErrors(); |
||||
|
result.CreatedAdminRole = true; |
||||
|
} |
||||
|
|
||||
|
(await UserManager.AddToRoleAsync(adminUser, adminRoleName)).CheckErrors(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue