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.EosAgent.Authenticaitons; public class TokenService : ITokenService { private readonly IConfiguration _configuration; public TokenService(IConfiguration configuration) { _configuration = configuration; } public virtual async Task 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("client_id",client_id), new KeyValuePair("client_secret",client_secret), new KeyValuePair("grant_type",grant_type), new KeyValuePair("username",username), new KeyValuePair("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().ConfigureAwait(false); return dto; } }