|
@ -1,9 +1,10 @@ |
|
|
using System; |
|
|
|
|
|
using System.ComponentModel.DataAnnotations; |
|
|
using System.ComponentModel.DataAnnotations; |
|
|
using System.Net.Http; |
|
|
using System.Net.Http; |
|
|
|
|
|
using System.Net.Http.Headers; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
using IdentityModel.Client; |
|
|
using IdentityModel.Client; |
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
|
|
using Microsoft.AspNetCore.Http; |
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
using Microsoft.Extensions.Configuration; |
|
|
using Microsoft.Extensions.Configuration; |
|
|
using Microsoft.Extensions.Logging; |
|
|
using Microsoft.Extensions.Logging; |
|
@ -11,35 +12,31 @@ using Volo.Abp.Application.Services; |
|
|
|
|
|
|
|
|
namespace Win_in.Sfs.Auth.Tokens; |
|
|
namespace Win_in.Sfs.Auth.Tokens; |
|
|
|
|
|
|
|
|
[Route($"api/token")] |
|
|
[Route($"api")] |
|
|
|
|
|
[Authorize] |
|
|
public class TokenService : ApplicationService |
|
|
public class TokenService : ApplicationService |
|
|
{ |
|
|
{ |
|
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor; |
|
|
private readonly IHttpClientFactory _httpClientFactory; |
|
|
private readonly IHttpClientFactory _httpClientFactory; |
|
|
private readonly ILogger<TokenService> _logger; |
|
|
private readonly ILogger<TokenService> _logger; |
|
|
private readonly IConfiguration _configuration; |
|
|
private readonly IConfiguration _configuration; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TokenService(IHttpClientFactory httpClientFactory, IConfiguration configuration, ILogger<TokenService> logger) |
|
|
public TokenService(IHttpContextAccessor httpContextAccessor, IHttpClientFactory httpClientFactory, IConfiguration configuration, ILogger<TokenService> logger) |
|
|
{ |
|
|
{ |
|
|
|
|
|
this._httpContextAccessor = httpContextAccessor; |
|
|
this._httpClientFactory = httpClientFactory; |
|
|
this._httpClientFactory = httpClientFactory; |
|
|
this._configuration = configuration; |
|
|
this._configuration = configuration; |
|
|
this._logger = logger; |
|
|
this._logger = logger; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[HttpPost] |
|
|
[HttpPost("token")] |
|
|
[AllowAnonymous] |
|
|
[AllowAnonymous] |
|
|
public async Task<IActionResult> CreateAsync(LoginModel model) |
|
|
public async Task<IActionResult> CreateAsync(LoginModel model) |
|
|
{ |
|
|
{ |
|
|
var address = _configuration["AuthServer:Authority"]; |
|
|
var address = _configuration["AuthServer:Authority"]; |
|
|
var request = new DiscoveryDocumentRequest |
|
|
|
|
|
{ |
|
|
|
|
|
Address = address, |
|
|
|
|
|
Policy = new DiscoveryPolicy { RequireHttps = false } |
|
|
|
|
|
}; |
|
|
|
|
|
var discovery = await _httpClientFactory.CreateClient().GetDiscoveryDocumentAsync(request).ConfigureAwait(false); |
|
|
|
|
|
var clientId = _configuration["AuthServer:ClientId"]; |
|
|
var clientId = _configuration["AuthServer:ClientId"]; |
|
|
var clientSecret = _configuration["AuthServer:ClientSecret"]; |
|
|
var clientSecret = _configuration["AuthServer:ClientSecret"]; |
|
|
this._logger.LogInformation($"address:{address},TokenEndpoint:{discovery.TokenEndpoint},clientId:{clientId},clientSecret:{clientSecret}"); |
|
|
|
|
|
var result = await _httpClientFactory.CreateClient().RequestPasswordTokenAsync(new PasswordTokenRequest |
|
|
var result = await _httpClientFactory.CreateClient().RequestPasswordTokenAsync(new PasswordTokenRequest |
|
|
{ |
|
|
{ |
|
|
Address = $"{address.TrimEnd('/')}/connect/token", |
|
|
Address = $"{address.TrimEnd('/')}/connect/token", |
|
@ -49,11 +46,6 @@ public class TokenService : ApplicationService |
|
|
UserName = model.UserName, |
|
|
UserName = model.UserName, |
|
|
Password = model.Password |
|
|
Password = model.Password |
|
|
}).ConfigureAwait(false); |
|
|
}).ConfigureAwait(false); |
|
|
Console.WriteLine($"Result:${(result.IsError ? result.ErrorDescription : result.AccessToken)}"); |
|
|
|
|
|
//if (result.RefreshToken == null)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// throw new UserFriendlyException("用户名或密码错误");
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
return new JsonResult(new |
|
|
return new JsonResult(new |
|
|
{ |
|
|
{ |
|
@ -72,18 +64,26 @@ public class TokenService : ApplicationService |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[HttpGet("[action]")]
|
|
|
[HttpGet("token/application-configuration")] |
|
|
[AllowAnonymous] |
|
|
public async Task<IActionResult> ApplicationConfiguration() |
|
|
public string Test() |
|
|
|
|
|
{ |
|
|
{ |
|
|
return "Test"; |
|
|
var address = _configuration["AuthServer:Authority"]; |
|
|
|
|
|
var url = $"{address.TrimEnd('/')}/api/abp/application-configuration"; |
|
|
|
|
|
var httpClient = _httpClientFactory.CreateClient(); |
|
|
|
|
|
var token = this._httpContextAccessor.HttpContext.Request.Headers.Authorization.ToString(); |
|
|
|
|
|
httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(token); |
|
|
|
|
|
var response = await httpClient.GetAsync(url).ConfigureAwait(false); |
|
|
|
|
|
var result = new ContentResult(); |
|
|
|
|
|
result.ContentType = "application/json"; |
|
|
|
|
|
result.Content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); |
|
|
|
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[HttpGet("/token/test")] |
|
|
[HttpGet("token/test")] |
|
|
[AllowAnonymous] |
|
|
[AllowAnonymous] |
|
|
public string Test1() |
|
|
public string Test() |
|
|
{ |
|
|
{ |
|
|
return "Test"; |
|
|
return "test"; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|