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.
56 lines
2.1 KiB
56 lines
2.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Volo.Abp;
|
|
namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.TyrpAgent.Authenticaitons;
|
|
|
|
public class TokenService : ITokenService
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public TokenService(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
public virtual async Task<BaererToken> GetTokenAsync()
|
|
{
|
|
const string routeString = "connect/token";
|
|
|
|
var baseUrl = _configuration["AuthServer:Authority"];
|
|
var client_id = _configuration["Authentication:client_id"];
|
|
var client_secret = _configuration["Authentication:client_secret"];
|
|
var grant_type = _configuration["Authentication:grant_type"];
|
|
var username = _configuration["Authentication:username"];
|
|
var password = _configuration["Authentication:password"];
|
|
var client = new HttpClient();
|
|
client.BaseAddress = new Uri(baseUrl);
|
|
client.DefaultRequestHeaders.Accept.Clear();
|
|
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
var content = new FormUrlEncodedContent(new[]
|
|
{
|
|
new KeyValuePair<string, string>("client_id",client_id),
|
|
new KeyValuePair<string, string>("client_secret",client_secret),
|
|
new KeyValuePair<string, string>("grant_type",grant_type),
|
|
new KeyValuePair<string, string>("username",username),
|
|
new KeyValuePair<string, string>("password",password),
|
|
});
|
|
|
|
var response = await client.PostAsync(routeString, content).ConfigureAwait(false);
|
|
Console.WriteLine(response.RequestMessage.RequestUri);
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var str = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
|
throw new UserFriendlyException(str);
|
|
}
|
|
|
|
var dto = await response.Content.ReadFromJsonAsync<BaererToken>().ConfigureAwait(false);
|
|
return dto;
|
|
|
|
}
|
|
}
|
|
|