remove 移除user项目
This commit is contained in:
38
Src/Dlhis.Sys/Identity/Controllers/OAuth2Controller.cs
Normal file
38
Src/Dlhis.Sys/Identity/Controllers/OAuth2Controller.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Text;
|
||||
using Dlhis.Sys.Identity.Request;
|
||||
using Kurisu.AspNetCore.Authentication.Options;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Dlhis.Sys.Identity.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// OAuth2认证
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class OAuth2Controller : ControllerBase
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IOptions<IdentityServerOptions> _identityServerOption;
|
||||
|
||||
public OAuth2Controller(IHttpClientFactory httpClientFactory, IOptions<IdentityServerOptions> identityServerOption)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_identityServerOption = identityServerOption;
|
||||
}
|
||||
|
||||
[HttpPost("clientgrant")]
|
||||
public async Task<object> ClientGrant(ClientGrantRequest request)
|
||||
{
|
||||
var authority = _identityServerOption.Value.Authority;
|
||||
|
||||
var httpClient = _httpClientFactory.CreateClient();
|
||||
|
||||
var content = new StringContent($"grant_type=client_credentials&client_id={request.ClientId}&client_secret={request.ClientSecret}&scope={request.Scope}", Encoding.UTF8, "application/x-www-form-urlencoded");
|
||||
var response = await httpClient.PostAsync($"{authority}/connect/token", content);
|
||||
var result = await response.Content.ReadAsStringAsync();
|
||||
return JObject.Parse(result);
|
||||
}
|
||||
}
|
||||
10
Src/Dlhis.Sys/Identity/Request/ClientGrantRequest.cs
Normal file
10
Src/Dlhis.Sys/Identity/Request/ClientGrantRequest.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Dlhis.Sys.Identity.Request;
|
||||
|
||||
public class ClientGrantRequest
|
||||
{
|
||||
public string ClientId { get; set; }
|
||||
|
||||
public string ClientSecret { get; set; }
|
||||
|
||||
public string Scope { get; set; } = "yikatong";
|
||||
}
|
||||
27
Src/Dlhis.Sys/Packs/OAuth2HttpClientPack.cs
Normal file
27
Src/Dlhis.Sys/Packs/OAuth2HttpClientPack.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Kurisu.AspNetCore.Startup;
|
||||
|
||||
namespace Dlhis.Sys.Packs;
|
||||
|
||||
public class OAuth2HttpClientPack : BaseAppPack
|
||||
{
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddHttpClient("oauth2", (sp, httpClient) =>
|
||||
{
|
||||
if (sp.GetService<IWebHostEnvironment>().IsProduction())
|
||||
{
|
||||
httpClient.DefaultRequestHeaders.Add("X-Requested-Internal", "internal");
|
||||
}
|
||||
})
|
||||
.ConfigurePrimaryHttpMessageHandler(() =>
|
||||
{
|
||||
var handler = new HttpClientHandler
|
||||
{
|
||||
ClientCertificateOptions = ClientCertificateOption.Manual,
|
||||
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
|
||||
};
|
||||
|
||||
return handler;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user