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.
65 lines
1.8 KiB
65 lines
1.8 KiB
2 years ago
|
using System.ComponentModel.DataAnnotations;
|
||
|
using System.Net.Http;
|
||
|
using System.Threading.Tasks;
|
||
|
using IdentityModel.Client;
|
||
|
using Microsoft.AspNetCore.Authorization;
|
||
|
using Microsoft.AspNetCore.Mvc;
|
||
|
using Microsoft.Extensions.Configuration;
|
||
|
using Volo.Abp.Application.Services;
|
||
|
|
||
|
namespace Win_in.Sfs.Auth.Tokens;
|
||
|
|
||
|
[Route($"api/token")]
|
||
|
[Authorize]
|
||
|
public class TokenService : ApplicationService
|
||
|
{
|
||
|
private readonly IHttpClientFactory _httpClientFactory;
|
||
|
private readonly IConfiguration _configuration;
|
||
|
|
||
|
public TokenService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
|
||
|
{
|
||
|
this._httpClientFactory = httpClientFactory;
|
||
|
this._configuration = configuration;
|
||
|
}
|
||
|
|
||
|
[HttpPost]
|
||
|
[AllowAnonymous]
|
||
|
public async Task<TokenResponse> CreateAsync(LoginModel model)
|
||
|
{
|
||
|
var address = _configuration["AuthServer:Authority"];
|
||
|
var discovery = await _httpClientFactory.CreateClient().GetDiscoveryDocumentAsync(address).ConfigureAwait(false);
|
||
|
var clientId = _configuration["AuthServer:ClientId"];
|
||
|
var clientSecret = _configuration["AuthServer:ClientSecret"];
|
||
|
|
||
|
var result = await _httpClientFactory.CreateClient().RequestPasswordTokenAsync(new PasswordTokenRequest
|
||
|
{
|
||
|
Address = discovery.TokenEndpoint,
|
||
|
GrantType = "password",
|
||
|
ClientId = clientId,
|
||
|
ClientSecret = clientSecret,
|
||
|
UserName = model.UserName,
|
||
|
Password = model.Password
|
||
|
}).ConfigureAwait(false);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
[HttpGet]
|
||
|
public string Test()
|
||
|
{
|
||
|
return "Test";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[Display]
|
||
|
public class LoginModel
|
||
|
{
|
||
|
[Display]
|
||
|
[Required]
|
||
|
public string UserName { get; set; }
|
||
|
|
||
|
[Display]
|
||
|
[Required]
|
||
|
public string Password { get; set; }
|
||
|
}
|