123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System;
- using System.Configuration;
- using System.Linq;
- using System.Security.Claims;
- using System.Text.RegularExpressions;
- using System.Threading;
- using iiie.Authentication.DBO;
- using Newtonsoft.Json;
-
- namespace iiie.Authentication.Business
- {
- public static class AuthProvider
- {
- /// <summary>
- /// The registered user dbo, or null if no user is logged
- /// </summary>
- public static UserGet UserDbo
- {
- get
- {
- var claim = ((ClaimsIdentity)Thread.CurrentPrincipal.Identity).Claims.FirstOrDefault(x => x != null && x.Type == "__userdbo__");
- if (claim == null)
- return null;
- return JsonConvert.DeserializeObject<UserGet>(claim.Value);
- }
- set
- {
- var claim = new Claim("__userdbo__", JsonConvert.SerializeObject(value));
- ((ClaimsIdentity)Thread.CurrentPrincipal.Identity).AddClaim(claim);
- }
- }
-
- /// <summary>
- /// Check if this is a valid email address
- /// </summary>
- /// <param name="email">The email to test</param>
- /// <returns>True if the email is valid, false otherwise</returns>
- public static bool IsValidEmail(string email)
- {
- try
- {
- return Regex.IsMatch(email,
- @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
- @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
- RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
- }
- catch (RegexMatchTimeoutException)
- {
- return false;
- }
- }
-
- /// <summary>
- /// Get a int from config file, or return the default value
- /// </summary>
- /// <param name="key">The config file key</param>
- /// <param name="def">The default value</param>
- /// <returns>The found int</returns>
- public static int GetInt(string key, int def)
- {
- var valueStr = GetString(key, def.ToString());
- int value;
- if (int.TryParse(valueStr, out value))
- return value;
- return def;
- }
-
- /// <summary>
- /// Get a string from config file, or return the default value
- /// </summary>
- /// <param name="key">The config file key</param>
- /// <param name="def">The default value</param>
- /// <returns>The found string</returns>
- public static string GetString(string key, string def)
- {
- var value = ConfigurationManager.AppSettings[key];
- if (value == null)
- return def;
- return value;
- }
-
- /// <summary>
- /// Const value to compute password hash
- /// </summary>
- public static int GetPasswordSaltByteSize()
- {
- return GetInt("PasswordSaltByteSize", 42);
- }
-
- /// <summary>
- /// Const value to compute password hash
- /// </summary>
- public static int GetPasswordHashByteSize()
- {
- return GetInt("PasswordHashByteSize", 42);
- }
-
- /// <summary>
- /// Const value to compute password hash
- /// </summary>
- public static int GetPasswordIterations()
- {
- return GetInt("PasswordIterations", 2048);
- }
-
- /// <summary>
- /// Returns a string used to validate the token.
- /// Must be constant. Default is app setting 'ValidatorString'
- /// </summary>
- /// <returns>The string</returns>
- public static string GetValidatorString()
- {
- return GetString("ValidatorString", "__default__");
- }
-
- /// <summary>
- /// Returns a string used to sign the token
- /// Must be constant. Default is app setting 'CredentialKey'
- /// </summary>
- /// <returns>The string</returns>
- public static string GetCredentialKey()
- {
- return GetString("CredentialKey", "__default__");
- }
- }
- }
|