Browse Source

[fix]优化TokenService,改用ClientCredentials

master
贾荣国 3 years ago
parent
commit
295b5b06b5
  1. 2
      WebApiService/src/Win_in.Sfs.Scp.WebApi.DbMigrator/appsettings.json
  2. 11
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/RemoteAuthenticationOptions.cs
  3. 18
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/TokenService.cs
  4. 1
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Startup.cs
  5. 7
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.Production.json
  6. 7
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.json

2
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": {

11
WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/RemoteAutherication/RemoteAuthenticationOptions.cs

@ -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; }
}
}

18
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<RemoteAuthenticationOptions> options)
public TokenService(IConfiguration configuration)
{
_configuration = configuration;
_options = options.Value;
}
public async Task<BaererToken> 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<string, string>("client_id",_options.client_id),
new KeyValuePair<string, string>("client_secret",_options.client_secret),
new KeyValuePair<string, string>("grant_type",_options.grant_type),
new KeyValuePair<string, string>("username",_options.username),
new KeyValuePair<string, string>("password",_options.password),
new KeyValuePair<string, string>("client_id",clientId),
new KeyValuePair<string, string>("client_secret",clientSecret),
new KeyValuePair<string, string>("grant_type",grantType),
});
var response = await client.PostAsync(routeString, content);

1
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<BasicAuthenticationOptions>(configuration.GetSection("BasicAuthentication"));
services.Configure<RemoteAuthenticationOptions>(configuration.GetSection("RemoteAuthentication"));
}
private static void ConfigureAuthentication(IServiceCollection services,IConfiguration configuration)

7
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*"
}
}

7
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*"
}
}
Loading…
Cancel
Save