From 295b5b06b59a0e63270c32c99e411a017be95487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=BE=E8=8D=A3=E5=9B=BD?= Date: Fri, 28 Jan 2022 14:22:28 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=E4=BC=98=E5=8C=96TokenService,=E6=94=B9?= =?UTF-8?q?=E7=94=A8ClientCredentials?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../appsettings.json | 2 +- .../RemoteAuthenticationOptions.cs | 11 ----------- .../RemoteAutherication/TokenService.cs | 18 +++++++++--------- .../Win_in.Sfs.Scp.WebApi.Xml.Host/Startup.cs | 1 - .../appsettings.Production.json | 7 ------- .../appsettings.json | 7 ------- 6 files changed, 10 insertions(+), 36 deletions(-) delete mode 100644 WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/RemoteAuthenticationOptions.cs diff --git a/WebApiService/src/Win_in.Sfs.Scp.WebApi.DbMigrator/appsettings.json b/WebApiService/src/Win_in.Sfs.Scp.WebApi.DbMigrator/appsettings.json index 681470e..8f67deb 100644 --- a/WebApiService/src/Win_in.Sfs.Scp.WebApi.DbMigrator/appsettings.json +++ b/WebApiService/src/Win_in.Sfs.Scp.WebApi.DbMigrator/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "Default": "Server=127.0.0.1;Database=Scp_WebApi;User ID=sa;Password=Microsoft2008;connection timeout=600;", + "Default": "Server=127.0.0.1;Database=Scp_WebApi;User ID=sa;Password=Microsoft2008;connection timeout=600;" }, "IdentityServer": { "Clients": { diff --git a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/RemoteAuthenticationOptions.cs b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/RemoteAuthenticationOptions.cs deleted file mode 100644 index 9e82d6a..0000000 --- a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/RemoteAuthenticationOptions.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Win_in.Sfs.Scp.WebApi.XmlHost -{ - public class RemoteAuthenticationOptions - { - public string client_id { get; set; } - public string client_secret { get; set; } - public string grant_type { get; set; } - public string username { get; set; } - public string password { get; set; } - } -} \ No newline at end of file diff --git a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/TokenService.cs b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/TokenService.cs index 56c468c..596cf59 100644 --- a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/TokenService.cs +++ b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/TokenService.cs @@ -4,6 +4,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Threading.Tasks; +using IdentityModel; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Options; @@ -12,17 +13,18 @@ namespace Win_in.Sfs.Scp.WebApi.XmlHost public class TokenService : ITokenService { private readonly IConfiguration _configuration; - private readonly RemoteAuthenticationOptions _options; - public TokenService(IConfiguration configuration, IOptions options) + public TokenService(IConfiguration configuration) { _configuration = configuration; - _options = options.Value; } public async Task GetToken() { - var baseUrl = _configuration["RemoteServices:Default:BaseUrl"]; + var baseUrl = _configuration["AuthServer:Authority"]; + var clientId = _configuration["AuthServer:SwaggerClientId"]; + var clientSecret = _configuration["AuthServer:SwaggerClientSecret"]; + var grantType = OidcConstants.GrantTypes.ClientCredentials; const string routeString = "connect/token"; var client = new HttpClient(); client.BaseAddress = new Uri(baseUrl); @@ -31,11 +33,9 @@ namespace Win_in.Sfs.Scp.WebApi.XmlHost var content = new FormUrlEncodedContent(new[] { - new KeyValuePair("client_id",_options.client_id), - new KeyValuePair("client_secret",_options.client_secret), - new KeyValuePair("grant_type",_options.grant_type), - new KeyValuePair("username",_options.username), - new KeyValuePair("password",_options.password), + new KeyValuePair("client_id",clientId), + new KeyValuePair("client_secret",clientSecret), + new KeyValuePair("grant_type",grantType), }); var response = await client.PostAsync(routeString, content); diff --git a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Startup.cs b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Startup.cs index 71681d1..176e491 100644 --- a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Startup.cs +++ b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Startup.cs @@ -45,7 +45,6 @@ namespace Win_in.Sfs.Scp.WebApi.XmlHost private static void ConfigureOptions(IServiceCollection services, IConfiguration configuration) { // services.Configure(configuration.GetSection("BasicAuthentication")); - services.Configure(configuration.GetSection("RemoteAuthentication")); } private static void ConfigureAuthentication(IServiceCollection services,IConfiguration configuration) diff --git a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.Production.json b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.Production.json index a4bebac..47e8adc 100644 --- a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.Production.json +++ b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.Production.json @@ -18,12 +18,5 @@ "RequireHttpsMetadata": "false", "SwaggerClientId": "WebApi_App", "SwaggerClientSecret": "1q2w3e*" - }, - "RemoteAuthentication": { - "client_id": "WebApi_App", - "client_secret": "1q2w3e*", - "grant_type": "password", - "username": "admin", - "password": "1q2w3E*" } } \ No newline at end of file diff --git a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.json b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.json index cd2a42a..b78bc03 100644 --- a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.json +++ b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.json @@ -18,12 +18,5 @@ "RequireHttpsMetadata": "false", "SwaggerClientId": "WebApi_App", "SwaggerClientSecret": "1q2w3e*" - }, - "RemoteAuthentication": { - "client_id": "WebApi_App", - "client_secret": "1q2w3e*", - "grant_type": "password", - "username": "admin", - "password": "1q2w3E*" } } \ No newline at end of file