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.
 
 
 
 
 
 

69 lines
2.3 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.Pda.Authenticaitons;
/// <summary>
///
/// </summary>
public class TokenService : ITokenService
{
private readonly IConfiguration _configuration;
/// <summary>
///
/// </summary>
/// <param name="configuration"></param>
public TokenService(IConfiguration configuration)
{
_configuration = configuration;
}
/// <summary>
///
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
public virtual async Task<BaererToken> GetTokenAsync(string username, string password)
{
const string routeString = "connect/token";
var baseUrl = _configuration["IdentityClients:Default:Authority"];
var client_id = _configuration["IdentityClients:Default:ClientId"];
var client_secret = _configuration["IdentityClients:Default:ClientSecret"];
var grant_type = "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;
}
}