149 lines
4.9 KiB
C#
149 lines
4.9 KiB
C#
using CPRNIMS.Infrastructure.Models.Account;
|
|
using CPRNIMS.Infrastructure.Models.Common;
|
|
using CPRNIMS.Infrastructure.ViewModel.Account;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CPRNIMS.Infrastructure.Helper
|
|
{
|
|
public class TokenHelper
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly IConfiguration _configuration;
|
|
public TokenHelper(HttpClient httpClient, IConfiguration configuration)
|
|
{
|
|
_httpClient = httpClient;
|
|
_configuration = configuration;
|
|
}
|
|
public async Task<string> GetRoleAsync(string username, string password, string token)
|
|
{
|
|
var loginModel = new LoginModel
|
|
{
|
|
Username = username,
|
|
Password = password
|
|
};
|
|
try
|
|
{
|
|
var httpClient = new HttpClient(new HttpClientHandler
|
|
{
|
|
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
|
|
})
|
|
{
|
|
BaseAddress = new Uri(_configuration["CommonEndpoints:ApiDefaultHeaders:BaseUrl"]),
|
|
DefaultRequestHeaders = {
|
|
Authorization = new AuthenticationHeaderValue("Bearer", token)}
|
|
};
|
|
var response = await httpClient.PostAsJsonAsync(_configuration["Account:Claims"], loginModel);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var tokenResponse = await response.Content.ReadAsStringAsync();
|
|
|
|
return tokenResponse;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<LoginVM> LoginAsync(LoginVM loginModel)
|
|
{
|
|
var loginResponse = new ResponseObject();
|
|
try
|
|
{
|
|
var httpClient = new HttpClient(new HttpClientHandler
|
|
{
|
|
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
|
|
})
|
|
{
|
|
BaseAddress = new Uri(_configuration["CommonEndpoints:ApiDefaultHeaders:BaseUrl"]),
|
|
};
|
|
|
|
// Send a POST request to the /login endpoint
|
|
var response = await httpClient.PostAsJsonAsync(_configuration["Account:Login"], loginModel);
|
|
|
|
// Deserialize the JSON response
|
|
loginResponse = JsonSerializer.Deserialize<ResponseObject>(await response.Content.ReadAsStringAsync());
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
|
|
// Access the message property
|
|
loginModel.Message = loginResponse.message;
|
|
loginModel.Status = loginResponse.statusResponse;
|
|
return loginModel;
|
|
}
|
|
else
|
|
{
|
|
// Access the message property
|
|
loginModel.Message = loginResponse.message;
|
|
loginModel.Status = loginResponse.statusResponse;
|
|
return loginModel;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
loginModel.Message = ex.ToString();
|
|
loginModel.Status = "Invalid";
|
|
return loginModel;
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<string> GetJwtTokenAsync(User loginModel)
|
|
{
|
|
var httpClient = new HttpClient(new HttpClientHandler
|
|
{
|
|
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
|
|
})
|
|
{
|
|
BaseAddress = new Uri(_configuration["CommonEndpoints:ApiDefaultHeaders:BaseUrl"]),
|
|
};
|
|
|
|
var response = await httpClient.PostAsJsonAsync(_configuration["Account:Auth"], loginModel);
|
|
|
|
try
|
|
{
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var tokenResponse = await response.Content.ReadAsStringAsync();
|
|
|
|
var tokenObj = JsonSerializer.Deserialize<Dictionary<string, string>>(tokenResponse);
|
|
|
|
if (tokenObj.TryGetValue("token", out var token))
|
|
{
|
|
return token;
|
|
}
|
|
|
|
return tokenResponse;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|