171 lines
5.8 KiB
C#
171 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Runtime.Caching;
|
|
using System.Threading.Tasks;
|
|
using HtmlAgilityPack;
|
|
using Kreta.Client.Kir.Interfaces;
|
|
using Kreta.Client.Kir.Models;
|
|
using Kreta.Client.Kir2Service;
|
|
using Kreta.Core;
|
|
using Kreta.Resources;
|
|
|
|
namespace Kreta.Client.Kir
|
|
{
|
|
public class KirAuthentication : IKirAuthentication
|
|
{
|
|
private static HttpClient HttpClient = new HttpClient();
|
|
|
|
#region Privát osztály változók
|
|
|
|
private string FelhasznaloNevCacheKey { get; set; }
|
|
|
|
private string JelszoCacheKey { get; set; }
|
|
|
|
private string IntezmenyAzonosito { get; }
|
|
|
|
private string FelhasznaloNev
|
|
{
|
|
get => Cache.Get(FelhasznaloNevCacheKey)?.ToString();
|
|
set
|
|
{
|
|
if (FelhasznaloNev == null)
|
|
{
|
|
Cache.Add(FelhasznaloNevCacheKey, value, new CacheItemPolicy { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(5)) });
|
|
}
|
|
}
|
|
}
|
|
|
|
private string Jelszo
|
|
{
|
|
get => Cache.Get(JelszoCacheKey)?.ToString();
|
|
set
|
|
{
|
|
if (Jelszo == null)
|
|
{
|
|
Cache.Add(JelszoCacheKey, value, new CacheItemPolicy { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(5)) });
|
|
}
|
|
}
|
|
}
|
|
|
|
private string LoginUrl { get; }
|
|
|
|
#endregion Privát osztály változók
|
|
|
|
public KirAuthentication(KirLogin loginModel, string felhasznaloNevCacheKey, string jelszoCacheKey)
|
|
{
|
|
LoginUrl = loginModel.LoginUrl;
|
|
IntezmenyAzonosito = loginModel.IntezmenyAzonosito;
|
|
|
|
FelhasznaloNevCacheKey = felhasznaloNevCacheKey;
|
|
JelszoCacheKey = jelszoCacheKey;
|
|
|
|
if (string.IsNullOrWhiteSpace(FelhasznaloNev) && string.IsNullOrWhiteSpace(Jelszo))
|
|
{
|
|
FelhasznaloNev = loginModel.FelhasznaloNev;
|
|
Jelszo = loginModel.Jelszo;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(LoginUrl))
|
|
{
|
|
throw new ArgumentException(ErrorResource.KirLoginUrlNincsMegadvaAzAppSettingsben);
|
|
}
|
|
}
|
|
|
|
public KIR2AuthHeaderType GetAuthHeader(string kirToken)
|
|
{
|
|
return new KIR2AuthHeaderType
|
|
{
|
|
Intezmeny = IntezmenyAzonosito,
|
|
Felhasznalo = new FelhasznaloType
|
|
{
|
|
Item = new IntezmenyiFelhasznaloType
|
|
{
|
|
FelhasznaloNev = FelhasznaloNev,
|
|
KIRToken = kirToken
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#region Get KIR token
|
|
|
|
public string GetKirToken()
|
|
{
|
|
try
|
|
{
|
|
string requestVerificationToken = GetRequestVerificationToken();
|
|
|
|
string loginResponse = AsyncUtil.RunSync(() => GetLoginResponse(requestVerificationToken, IntezmenyAzonosito, FelhasznaloNev, Jelszo));
|
|
|
|
return GetToken(loginResponse);
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
if (ex.Status == WebExceptionStatus.ProtocolError)
|
|
{
|
|
if (ex.Response is HttpWebResponse response)
|
|
{
|
|
var statusCode = (int)response.StatusCode;
|
|
if (statusCode != (int)HttpStatusCode.OK)
|
|
{
|
|
throw new Exception(ErrorResource.ASzolgaltatasNemElerhetoProbaljaKesobbEsetlegHaAHibaHuzamosabbIdeigFennallJelezzeAzIlletekeseknek, ex);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new Exception(ErrorResource.IsmeretlenHibaTortentAKirOldalanKerjukKisereljeMegAzImportalastKesobb, ex);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private string GetRequestVerificationToken()
|
|
{
|
|
string loginPage = AsyncUtil.RunSync(() => HttpClient.GetStringAsync(LoginUrl));
|
|
|
|
var htmlDocument = new HtmlDocument();
|
|
htmlDocument.LoadHtml(loginPage);
|
|
|
|
HtmlNode requestVerificationTokenNode = htmlDocument.DocumentNode.SelectSingleNode("//input[@name='__RequestVerificationToken']");
|
|
|
|
return requestVerificationTokenNode?.Attributes["value"].Value;
|
|
}
|
|
|
|
private async Task<string> GetLoginResponse(string requestVerificationToken, string intezmenyAzonosito, string felhasznaloNev, string jelszo)
|
|
{
|
|
HttpResponseMessage result = await HttpClient.PostAsync(LoginUrl,
|
|
new FormUrlEncodedContent(
|
|
new Dictionary<string, string>
|
|
{
|
|
{ "__RequestVerificationToken", requestVerificationToken },
|
|
{ "UserDto.UserType", "3" },
|
|
{ "UserDto.FenntId", string.Empty },
|
|
{ "UserDto.TKAzonosito", string.Empty },
|
|
{ "UserDto.OMAzonosito", intezmenyAzonosito },
|
|
{ "UserDto.UserName", felhasznaloNev },
|
|
{ "UserDto.Password", jelszo },
|
|
{ "ContinueUrl", string.Empty },
|
|
{ "RedirectPartnerId", string.Empty }
|
|
}
|
|
));
|
|
|
|
return await result.Content.ReadAsStringAsync();
|
|
}
|
|
|
|
private string GetToken(string loginResponse)
|
|
{
|
|
var htmlDocument = new HtmlDocument();
|
|
htmlDocument.LoadHtml(loginResponse);
|
|
|
|
HtmlNode tokenNode = htmlDocument.GetElementbyId("token");
|
|
|
|
return tokenNode?.InnerText.Trim();
|
|
}
|
|
|
|
#endregion Get KIR token
|
|
}
|
|
}
|