Browse Source

Update IdentityServerDataSeedContributor.

pull/4578/head
maliming 6 years ago
parent
commit
b2beaa2b0c
  1. 2
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/IdentityServer/IdentityServerDataSeedContributor.cs
  2. 54
      templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/IdentityServer/IdentityServerDataSeedContributor.cs

2
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/IdentityServer/IdentityServerDataSeedContributor.cs

@ -182,7 +182,7 @@ namespace MyCompanyName.MyProjectName.IdentityServer
AccessTokenLifetime = 31536000, //365 days AccessTokenLifetime = 31536000, //365 days
AuthorizationCodeLifetime = 300, AuthorizationCodeLifetime = 300,
IdentityTokenLifetime = 300, IdentityTokenLifetime = 300,
RequireConsent = true, RequireConsent = false,
RequirePkce = false, RequirePkce = false,
FrontChannelLogoutUri = frontChannelLogoutUri FrontChannelLogoutUri = frontChannelLogoutUri
}, },

54
templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/IdentityServer/IdentityServerDataSeedContributor.cs

@ -1,7 +1,8 @@
using Microsoft.Extensions.Configuration; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using IdentityServer4.Models;
using Microsoft.Extensions.Configuration;
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;
@ -11,12 +12,16 @@ using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.IdentityResources; using Volo.Abp.IdentityServer.IdentityResources;
using Volo.Abp.PermissionManagement; using Volo.Abp.PermissionManagement;
using Volo.Abp.Uow; using Volo.Abp.Uow;
using ApiResource = Volo.Abp.IdentityServer.ApiScopes.ApiResource;
using ApiScope = Volo.Abp.IdentityServer.ApiScopes.ApiScope;
using Client = Volo.Abp.IdentityServer.Clients.Client;
namespace MyCompanyName.MyProjectName.IdentityServer namespace MyCompanyName.MyProjectName.IdentityServer
{ {
public class IdentityServerDataSeedContributor : IDataSeedContributor, ITransientDependency public class IdentityServerDataSeedContributor : IDataSeedContributor, ITransientDependency
{ {
private readonly IApiResourceRepository _apiResourceRepository; private readonly IApiResourceRepository _apiResourceRepository;
private readonly IApiScopeRepository _apiScopeRepository;
private readonly IClientRepository _clientRepository; private readonly IClientRepository _clientRepository;
private readonly IIdentityResourceDataSeeder _identityResourceDataSeeder; private readonly IIdentityResourceDataSeeder _identityResourceDataSeeder;
private readonly IGuidGenerator _guidGenerator; private readonly IGuidGenerator _guidGenerator;
@ -26,6 +31,7 @@ namespace MyCompanyName.MyProjectName.IdentityServer
public IdentityServerDataSeedContributor( public IdentityServerDataSeedContributor(
IClientRepository clientRepository, IClientRepository clientRepository,
IApiResourceRepository apiResourceRepository, IApiResourceRepository apiResourceRepository,
IApiScopeRepository apiScopeRepository,
IIdentityResourceDataSeeder identityResourceDataSeeder, IIdentityResourceDataSeeder identityResourceDataSeeder,
IGuidGenerator guidGenerator, IGuidGenerator guidGenerator,
IPermissionDataSeeder permissionDataSeeder, IPermissionDataSeeder permissionDataSeeder,
@ -33,6 +39,7 @@ namespace MyCompanyName.MyProjectName.IdentityServer
{ {
_clientRepository = clientRepository; _clientRepository = clientRepository;
_apiResourceRepository = apiResourceRepository; _apiResourceRepository = apiResourceRepository;
_apiScopeRepository = apiScopeRepository;
_identityResourceDataSeeder = identityResourceDataSeeder; _identityResourceDataSeeder = identityResourceDataSeeder;
_guidGenerator = guidGenerator; _guidGenerator = guidGenerator;
_permissionDataSeeder = permissionDataSeeder; _permissionDataSeeder = permissionDataSeeder;
@ -44,6 +51,7 @@ namespace MyCompanyName.MyProjectName.IdentityServer
{ {
await _identityResourceDataSeeder.CreateStandardResourcesAsync(); await _identityResourceDataSeeder.CreateStandardResourcesAsync();
await CreateApiResourcesAsync(); await CreateApiResourcesAsync();
await CreateApiScopeAsync();
await CreateClientsAsync(); await CreateClientsAsync();
} }
@ -88,10 +96,17 @@ namespace MyCompanyName.MyProjectName.IdentityServer
return await _apiResourceRepository.UpdateAsync(apiResource); return await _apiResourceRepository.UpdateAsync(apiResource);
} }
private async Task CreateClientsAsync() private async Task CreateApiScopeAsync()
{ {
const string commonSecret = "E5Xd4yMqjP5kjWFKrYgySBju6JVfCzMyFp7n2QmMrME="; var apiScope = await _apiScopeRepository.GetByNameAsync("MyProjectName");
if (apiScope == null)
{
await _apiScopeRepository.InsertAsync(new ApiScope(_guidGenerator.Create(), "MyProjectName", "MyProjectName API"), autoSave: true);
}
}
private async Task CreateClientsAsync()
{
var commonScopes = new[] var commonScopes = new[]
{ {
"email", "email",
@ -100,6 +115,7 @@ namespace MyCompanyName.MyProjectName.IdentityServer
"role", "role",
"phone", "phone",
"address", "address",
"MyProjectName" "MyProjectName"
}; };
@ -110,25 +126,30 @@ namespace MyCompanyName.MyProjectName.IdentityServer
if (!webClientId.IsNullOrWhiteSpace()) if (!webClientId.IsNullOrWhiteSpace())
{ {
var webClientRootUrl = configurationSection["MyProjectName_Web:RootUrl"].EnsureEndsWith('/'); var webClientRootUrl = configurationSection["MyProjectName_Web:RootUrl"].EnsureEndsWith('/');
/* MyProjectName_Web client is only needed if you created a tiered
* solution. Otherwise, you can delete this client. */
await CreateClientAsync( await CreateClientAsync(
webClientId, name: webClientId,
commonScopes, scopes: commonScopes,
new[] { "hybrid" }, grantTypes: new[] {"hybrid"},
commonSecret, secret: (configurationSection["MyProjectName_Web:ClientSecret"] ?? "1q2w3e*").Sha256(),
redirectUri: $"{webClientRootUrl}signin-oidc", redirectUri: $"{webClientRootUrl}signin-oidc",
postLogoutRedirectUri: $"{webClientRootUrl}signout-callback-oidc" postLogoutRedirectUri: $"{webClientRootUrl}signout-callback-oidc",
frontChannelLogoutUri: $"{webClientRootUrl}Account/FrontChannelLogout"
); );
} }
//Console Test Client //Console Test Client
var consoleClientId = configurationSection["MyProjectName_ConsoleTestApp:ClientId"]; var consoleClientId = configurationSection["MyProjectName_App:ClientId"];
if (!consoleClientId.IsNullOrWhiteSpace()) if (!consoleClientId.IsNullOrWhiteSpace())
{ {
await CreateClientAsync( await CreateClientAsync(
consoleClientId, name: consoleClientId,
commonScopes, scopes: commonScopes,
new[] { "password", "client_credentials" }, grantTypes: new[] {"password", "client_credentials"},
commonSecret secret: (configurationSection["MyProjectName_App:ClientSecret"] ?? "1q2w3e*").Sha256()
); );
} }
} }
@ -140,6 +161,7 @@ namespace MyCompanyName.MyProjectName.IdentityServer
string secret, string secret,
string redirectUri = null, string redirectUri = null,
string postLogoutRedirectUri = null, string postLogoutRedirectUri = null,
string frontChannelLogoutUri = null,
IEnumerable<string> permissions = null) IEnumerable<string> permissions = null)
{ {
var client = await _clientRepository.FindByCliendIdAsync(name); var client = await _clientRepository.FindByCliendIdAsync(name);
@ -160,7 +182,9 @@ namespace MyCompanyName.MyProjectName.IdentityServer
AccessTokenLifetime = 31536000, //365 days AccessTokenLifetime = 31536000, //365 days
AuthorizationCodeLifetime = 300, AuthorizationCodeLifetime = 300,
IdentityTokenLifetime = 300, IdentityTokenLifetime = 300,
RequireConsent = false RequireConsent = false,
RequirePkce = false,
FrontChannelLogoutUri = frontChannelLogoutUri
}, },
autoSave: true autoSave: true
); );

Loading…
Cancel
Save