1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
public class Cookie
{
public delegate void AutorisationEventHandler(bool autorise);
public static event AutorisationEventHandler AutorisationEvent;
private static string Login;
private static string Password;
private static bool init = true;
public static string GetCookie(string key)
{
string[] cookies = HtmlPage.Document.Cookies.Split(';');
foreach (string cookie in cookies)
{
string[] keyValue = cookie.Split('=');
if (keyValue.Length == 2)
{
if (keyValue[0].ToString().Trim() == key)
return keyValue[1];
}
}
return null;
}
public static void SetCookie(string key, string value)
{
// Expire in 350 days
DateTime expireDate = DateTime.Now + TimeSpan.FromDays(350);
string newCookie = key + "=" + value + ";expires=" + expireDate.ToString("R");
HtmlPage.Document.SetProperty("cookie", newCookie);
}
public static void SetIsAut()
{
VerifierAutorisation(GetCookie("login"), GetCookie("password"));
}
public static void VerifierAutorisation(string login, string mdp)
{
Login = login;
Password = mdp;
if (Login != null && Password != null)
Service.VerifierLoginMdp(Login, Password);
else
OnAutorisationEvent(false);
if (init)
{
Service.AutorisationEvent += new Service.AutorisationEventHandler(Service_AutorisationEvent);
init = false;
}
}
protected static void OnAutorisationEvent(bool autorise)
{
if (AutorisationEvent != null)
{
AutorisationEvent(autorise);
}
}
private static void Service_AutorisationEvent(bool autorise)
{
Global.isAuth = autorise;
if (autorise)
{
Cookie.SetCookie("login", Login);
Cookie.SetCookie("password", Password);
}
OnAutorisationEvent(autorise);
}
} |