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.
 
 
 

51 lines
1.8 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 Microsoft.Extensions.Options;
namespace Win_in.Sfs.Scp.WebApi.XmlHost
{
public class TokenService : ITokenService
{
private readonly IConfiguration _configuration;
private readonly RemoteAuthenticationOptions _options;
public TokenService(IConfiguration configuration, IOptions<RemoteAuthenticationOptions> options)
{
_configuration = configuration;
_options = options.Value;
}
public async Task<BaererToken> GetToken()
{
var baseUrl = _configuration["RemoteServices:Default:BaseUrl"];
const string routeString = "connect/token";
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",_options.client_id),
new KeyValuePair<string, string>("client_secret",_options.client_secret),
new KeyValuePair<string, string>("grant_type",_options.grant_type),
new KeyValuePair<string, string>("username",_options.username),
new KeyValuePair<string, string>("password",_options.password),
});
var response = await client.PostAsync(routeString, content);
response.EnsureSuccessStatusCode();
var dto = await response.Content.ReadFromJsonAsync<BaererToken>();
return dto;
}
}
}