You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.3 KiB
100 lines
3.3 KiB
using System.ComponentModel.DataAnnotations;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Threading.Tasks;
|
|
using IdentityModel.Client;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Volo.Abp.Application.Services;
|
|
|
|
namespace Win_in.Sfs.Auth.Tokens;
|
|
|
|
[Route($"api")]
|
|
[Authorize]
|
|
public class TokenService : ApplicationService
|
|
{
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
private readonly ILogger<TokenService> _logger;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public TokenService(IHttpContextAccessor httpContextAccessor, IHttpClientFactory httpClientFactory, IConfiguration configuration, ILogger<TokenService> logger)
|
|
{
|
|
this._httpContextAccessor = httpContextAccessor;
|
|
this._httpClientFactory = httpClientFactory;
|
|
this._configuration = configuration;
|
|
this._logger = logger;
|
|
}
|
|
|
|
[HttpPost("token")]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> CreateAsync(LoginModel model)
|
|
{
|
|
var address = _configuration["AuthServer:Authority"];
|
|
var clientId = _configuration["AuthServer:ClientId"];
|
|
var clientSecret = _configuration["AuthServer:ClientSecret"];
|
|
|
|
var result = await _httpClientFactory.CreateClient().RequestPasswordTokenAsync(new PasswordTokenRequest
|
|
{
|
|
Address = $"{address.TrimEnd('/')}/connect/token",
|
|
GrantType = "password",
|
|
ClientId = clientId,
|
|
ClientSecret = clientSecret,
|
|
UserName = model.UserName,
|
|
Password = model.Password
|
|
}).ConfigureAwait(false);
|
|
|
|
return new JsonResult(new
|
|
{
|
|
result.TokenType,
|
|
result.AccessToken,
|
|
result.ExpiresIn,
|
|
result.RefreshToken,
|
|
result.Scope,
|
|
result.HttpStatusCode,
|
|
result.Error,
|
|
result.HttpErrorReason,
|
|
result.ErrorDescription,
|
|
result.ErrorType,
|
|
result.Exception?.Message,
|
|
Exception = result.Exception?.ToString()
|
|
});
|
|
}
|
|
|
|
[HttpGet("token/application-configuration")]
|
|
public async Task<IActionResult> ApplicationConfiguration()
|
|
{
|
|
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")]
|
|
[AllowAnonymous]
|
|
public string Test()
|
|
{
|
|
return "test";
|
|
}
|
|
}
|
|
|
|
[Display]
|
|
public class LoginModel
|
|
{
|
|
[Display]
|
|
[Required]
|
|
public string UserName { get; set; }
|
|
|
|
[Display]
|
|
[Required]
|
|
public string Password { get; set; }
|
|
}
|
|
|