Browse Source

[fix]Xml.Host访问WebApi.Host还是需要增加验证

master
贾荣国 3 years ago
parent
commit
19113599e0
  1. 12
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/.config/dotnet-tools.json
  2. 24
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/HttpClientInvokers/HttpClientInvoker.cs
  3. 5
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/HttpClientInvokers/IHttpClientInvoker.cs
  4. 6
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Startup.cs
  5. 9
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.Production.json
  6. 9
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.json

12
WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/.config/dotnet-tools.json

@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "6.0.1",
"commands": [
"dotnet-ef"
]
}
}
}

24
WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/HttpClientInvokers/HttpClientInvoker.cs

@ -13,18 +13,18 @@ namespace Win_in.Sfs.Scp.WebApi.XmlHost
public class HttpClientInvoker<TInput, TOutput> : IHttpClientInvoker<TInput, TOutput>
{
// private readonly HttpAuthorizationHandler _httpAuthorizationHandler;
private readonly HttpAuthorizationHandler _httpAuthorizationHandler;
private readonly string _baseUrl;
public HttpClientInvoker(IConfiguration configuration
// , HttpAuthorizationHandler httpAuthorizationHandler
, HttpAuthorizationHandler httpAuthorizationHandler
)
{
// _httpAuthorizationHandler = httpAuthorizationHandler;
_httpAuthorizationHandler = httpAuthorizationHandler;
_baseUrl = configuration["RemoteServices:Default:BaseUrl"];
}
public async Task<TOutput?> InvokeGetAsync(string routeString)
public async Task<TOutput> InvokeGetAsync(string routeString)
{
var client = CreateClient();
var response = await client.GetAsync(routeString);
@ -33,11 +33,13 @@ namespace Win_in.Sfs.Scp.WebApi.XmlHost
return dto;
}
public async Task<TOutput?> InvokePostAsync(TInput input, string routeString)
public async Task<TOutput> InvokePostAsync(TInput input, string routeString)
{
var client = CreateClient();
var response = await client.PostAsJsonAsync(routeString, input);
response.EnsureSuccessStatusCode();
var str =await response.Content.ReadAsStringAsync();
var dto = await response.Content.ReadFromJsonAsync<TOutput>();
return dto;
}
@ -48,13 +50,13 @@ namespace Win_in.Sfs.Scp.WebApi.XmlHost
client.BaseAddress = new Uri(_baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//XmlHost也使用Jwt认证,并连到同一个IDS4 server,不需要再手工添加鉴权信息了
// if (_httpAuthorizationHandler.IsLoggedIn())
// {
// client.DefaultRequestHeaders.Authorization =
// new AuthenticationHeaderValue("Bearer", _httpAuthorizationHandler.GetCurrentBearer());
// }
if (_httpAuthorizationHandler.IsLoggedIn())
{
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", _httpAuthorizationHandler.GetCurrentBearer());
}
return client;
}

5
WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/HttpClientInvokers/IHttpClientInvoker.cs

@ -5,7 +5,8 @@ namespace Win_in.Sfs.Scp.WebApi.XmlHost
{
public interface IHttpClientInvoker<in TInput, TOutput>
{
Task<TOutput?> InvokeGetAsync(string routeString);
Task<TOutput?> InvokePostAsync(TInput input, string routeString);
Task<TOutput> InvokeGetAsync(string routeString);
Task<TOutput> InvokePostAsync(TInput input, string routeString);
}
}

6
WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Startup.cs

@ -38,14 +38,14 @@ namespace Win_in.Sfs.Scp.WebApi.XmlHost
private static void DependencyInjection(IServiceCollection services)
{
services.AddSingleton(typeof(IHttpClientInvoker<,>), typeof(HttpClientInvoker<,>));
// services.AddSingleton<ITokenService, TokenService>();
// services.AddSingleton<HttpAuthorizationHandler>();
services.AddSingleton<ITokenService, TokenService>();
services.AddSingleton<HttpAuthorizationHandler>();
}
private static void ConfigureOptions(IServiceCollection services, IConfiguration configuration)
{
// services.Configure<BasicAuthenticationOptions>(configuration.GetSection("BasicAuthentication"));
// services.Configure<RemoteAuthenticationOptions>(configuration.GetSection("RemoteAuthentication"));
services.Configure<RemoteAuthenticationOptions>(configuration.GetSection("RemoteAuthentication"));
}
private static void ConfigureAuthentication(IServiceCollection services,IConfiguration configuration)

9
WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.Production.json

@ -16,7 +16,14 @@
"AuthServer": {
"Authority": "https://scp.iacchina.net:9988",
"RequireHttpsMetadata": "false",
"SwaggerClientId": "WebApi_Swagger",
"SwaggerClientId": "WebApi_App",
"SwaggerClientSecret": "1q2w3e*"
},
"RemoteAuthentication": {
"client_id": "WebApi_App",
"client_secret": "1q2w3e*",
"grant_type": "password",
"username": "admin",
"password": "1q2w3E*"
}
}

9
WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/appsettings.json

@ -16,7 +16,14 @@
"AuthServer": {
"Authority": "https://localhost:9988",
"RequireHttpsMetadata": "false",
"SwaggerClientId": "WebApi_Swagger",
"SwaggerClientId": "WebApi_App",
"SwaggerClientSecret": "1q2w3e*"
},
"RemoteAuthentication": {
"client_id": "WebApi_App",
"client_secret": "1q2w3e*",
"grant_type": "password",
"username": "admin",
"password": "1q2w3E*"
}
}
Loading…
Cancel
Save