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.
63 lines
2.2 KiB
63 lines
2.2 KiB
using IdentityModel.Client;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Volo.Abp.Account.Web.Pages.Account;
|
|
using Volo.Abp.Application.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace AuthServer.Host.Services.Token;
|
|
|
|
[Route("/api/[controller]/[action]")]
|
|
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()
|
|
});
|
|
}
|
|
}
|
|
|