diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.Domain/IdentityServer/IdentityServerDataSeeder.cs b/templates/mvc/src/MyCompanyName.MyProjectName.Domain/IdentityServer/IdentityServerDataSeedContributor.cs similarity index 76% rename from templates/mvc/src/MyCompanyName.MyProjectName.Domain/IdentityServer/IdentityServerDataSeeder.cs rename to templates/mvc/src/MyCompanyName.MyProjectName.Domain/IdentityServer/IdentityServerDataSeedContributor.cs index ae56d838e3..48d82dae90 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.Domain/IdentityServer/IdentityServerDataSeeder.cs +++ b/templates/mvc/src/MyCompanyName.MyProjectName.Domain/IdentityServer/IdentityServerDataSeedContributor.cs @@ -1,6 +1,8 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Configuration; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.Guids; @@ -12,26 +14,29 @@ using Volo.Abp.Uow; namespace MyCompanyName.MyProjectName.IdentityServer { - public class IdentityServerDataSeeder : IDataSeedContributor, ITransientDependency + public class IdentityServerDataSeedContributor : IDataSeedContributor, ITransientDependency { private readonly IApiResourceRepository _apiResourceRepository; private readonly IClientRepository _clientRepository; private readonly IIdentityResourceDataSeeder _identityResourceDataSeeder; private readonly IGuidGenerator _guidGenerator; private readonly IPermissionDataSeeder _permissionDataSeeder; + private readonly IConfigurationAccessor _configurationAccessor; - public IdentityServerDataSeeder( + public IdentityServerDataSeedContributor( IClientRepository clientRepository, IApiResourceRepository apiResourceRepository, IIdentityResourceDataSeeder identityResourceDataSeeder, IGuidGenerator guidGenerator, - IPermissionDataSeeder permissionDataSeeder) + IPermissionDataSeeder permissionDataSeeder, + IConfigurationAccessor configurationAccessor) { _clientRepository = clientRepository; _apiResourceRepository = apiResourceRepository; _identityResourceDataSeeder = identityResourceDataSeeder; _guidGenerator = guidGenerator; _permissionDataSeeder = permissionDataSeeder; + _configurationAccessor = configurationAccessor; } [UnitOfWork] @@ -98,23 +103,38 @@ namespace MyCompanyName.MyProjectName.IdentityServer "MyProjectName" }; - /* MyProjectName_Web client is only needed if you created a tiered - * solution. Otherwise, delete this client. */ - await CreateClientAsync( - "MyProjectName_Web", - commonScopes, - new[] { "hybrid" }, - commonSecret, - redirectUri: "https://localhost:44314/signin-oidc", - postLogoutRedirectUri: "https://localhost:44314/signout-callback-oidc" - ); - - await CreateClientAsync( - "MyCompanyName_ConsoleTestApp", - commonScopes, - new[] { "password", "client_credentials" }, - commonSecret - ); + var configurationSection = _configurationAccessor.Configuration.GetSection("IdentityServer:Clients"); + + //Web Client + var webClientId = configurationSection["MyProjectName_Web:ClientId"]; + if (!webClientId.IsNullOrWhiteSpace()) + { + 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( + webClientId, + commonScopes, + new[] { "hybrid" }, + commonSecret, + redirectUri: $"{webClientRootUrl}signin-oidc", + postLogoutRedirectUri: $"{webClientRootUrl}signout-callback-oidc" + ); + } + + //Console Test Client + var consoleClientId = configurationSection["MyProjectName_ConsoleTestApp:ClientId"]; + if (!consoleClientId.IsNullOrWhiteSpace()) + { + await CreateClientAsync( + consoleClientId, + commonScopes, + new[] { "password", "client_credentials" }, + commonSecret + ); + } } private async Task CreateClientAsync( diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs b/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs index 2f90983e42..f8a9f8dd1e 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs +++ b/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs @@ -33,7 +33,7 @@ namespace MyCompanyName.MyProjectName var hostingEnvironment = context.Services.GetHostingEnvironment(); ConfigureConventionalControllers(); - ConfigureAuthentication(context); + ConfigureAuthentication(context, configuration); ConfigureSwagger(context); ConfigureLocalization(); ConfigureVirtualFileSystem(context); @@ -64,12 +64,12 @@ namespace MyCompanyName.MyProjectName }); } - private void ConfigureAuthentication(ServiceConfigurationContext context) + private void ConfigureAuthentication(ServiceConfigurationContext context, IConfigurationRoot configuration) { context.Services.AddAuthentication("Bearer") .AddIdentityServerAuthentication(options => { - options.Authority = "https://localhost:44348"; + options.Authority = configuration["AuthServer:Authority"]; options.RequireHttpsMetadata = true; options.ApiName = "MyProjectName"; }); diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/appsettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/appsettings.json index 73ad9e7229..a4df214f53 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/appsettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/appsettings.json @@ -4,5 +4,8 @@ }, "Redis": { "Configuration": "127.0.0.1" + }, + "AuthServer": { + "Authority": "https://localhost:44348" } } diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs b/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs index 8a99d626d0..6f9efea814 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs +++ b/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs @@ -77,7 +77,7 @@ namespace MyCompanyName.MyProjectName Configure(options => { - options.Applications["MVC"].RootUrl = "https://localhost:44348/"; + options.Applications["MVC"].RootUrl = configuration["AppSelfUrl"]; }); Configure(options => diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/appsettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/appsettings.json index 73ad9e7229..92314c1d36 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/appsettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/appsettings.json @@ -1,8 +1,20 @@ { + "AppSelfUrl": "https://localhost:44348", "ConnectionStrings": { "Default": "Server=localhost;Database=MyProjectName;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Redis": { "Configuration": "127.0.0.1" + }, + "IdentityServer": { + "Clients": { + "MyProjectName_Web": { + "ClientId": "MyProjectName_Web", + "RootUrl": "https://localhost:44314/" + }, + "MyProjectName_ConsoleTestApp": { + "ClientId": "MyProjectName_ConsoleTestApp" + } + } } } diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebModule.cs b/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebModule.cs index 4ce92707bc..294605afd7 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebModule.cs +++ b/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebModule.cs @@ -69,7 +69,7 @@ namespace MyCompanyName.MyProjectName var configuration = context.Services.GetConfiguration(); ConfigureUrls(configuration); - ConfigureAuthentication(context); + ConfigureAuthentication(context, configuration); ConfigureAutoMapper(); ConfigureVirtualFileSystem(hostingEnvironment); ConfigureNavigationServices(); @@ -90,7 +90,7 @@ namespace MyCompanyName.MyProjectName Configure(options => { options.IsEnabled = MultiTenancyConsts.IsMultiTenancyEnabled; }); } - private void ConfigureAuthentication(ServiceConfigurationContext context) + private void ConfigureAuthentication(ServiceConfigurationContext context, IConfigurationRoot configuration) { context.Services.AddAuthentication(options => { @@ -104,12 +104,12 @@ namespace MyCompanyName.MyProjectName }) .AddOpenIdConnect("oidc", options => { - options.Authority = "https://localhost:44348"; + options.Authority = configuration["AuthServer:Authority"]; options.RequireHttpsMetadata = true; options.ResponseType = OpenIdConnectResponseType.CodeIdToken; - options.ClientId = "MyProjectName_Web"; - options.ClientSecret = "1q2w3e*"; + options.ClientId = configuration["AuthServer:ClientId"]; + options.ClientSecret = configuration["AuthServer:ClientSecret"]; options.SaveTokens = true; options.GetClaimsFromUserInfoEndpoint = true; diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/appsettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/appsettings.json index c53419cc49..f0d2e40d57 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/appsettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/appsettings.json @@ -7,5 +7,10 @@ }, "Redis": { "Configuration": "127.0.0.1" + }, + "AuthServer": { + "Authority": "https://localhost:44348", + "ClientId": "MyProjectName_Web", + "ClientSecret": "1q2w3e*" } } diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.Web/appsettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.Web/appsettings.json index e8ff5924ce..5b48dc3d38 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.Web/appsettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.Web/appsettings.json @@ -5,5 +5,12 @@ }, "AuthServer": { "Authority": "https://localhost:44361" + }, + "IdentityServer": { + "Clients": { + "MyProjectName_ConsoleTestApp": { + "ClientId": "MyProjectName_ConsoleTestApp" + } + } } } \ No newline at end of file diff --git a/templates/mvc/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/appsettings.json b/templates/mvc/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/appsettings.json index 130ba0f17b..e183f6ba53 100644 --- a/templates/mvc/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/appsettings.json +++ b/templates/mvc/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/appsettings.json @@ -7,7 +7,7 @@ "IdentityClients": { "Default": { "GrantType": "password", - "ClientId": "MyCompanyName_ConsoleTestApp", + "ClientId": "MyProjectName_ConsoleTestApp", "ClientSecret": "1q2w3e*", "UserName": "admin", "UserPassword": "1q2w3E*",