/// <summary> /// Initializes a new instance of the <see cref="DefaultCaptchaManager" /> class. /// </summary> public DefaultCaptchaManager(IStorageProvider storageProvider, string tokenParameterName, string inputElementName, string imageElementName, string tokenElementName) { Validate.ArgumentNotNull(storageProvider, "storageProvider"); Validate.ArgumentNotNullOrEmpty(tokenParameterName, "tokenParameterName"); Validate.ArgumentNotNullOrEmpty(inputElementName, "inputElementName"); Validate.ArgumentNotNullOrEmpty(imageElementName, "imageElementName"); Validate.ArgumentNotNullOrEmpty(tokenElementName, "tokenElementName"); IntelligencePolicy = new FakeInputIntelligencePolicy(); StorageProvider = storageProvider; TokenParameterName = tokenParameterName; InputElementName = inputElementName; ImageElementName = imageElementName; TokenElementName = tokenElementName; ImageUrlFactory = GenerateImageUrl; RefreshUrlFactory = GenerateRefreshUrl; MathCaptchaPairFactory = GenerateMathCaptcha; PlainCaptchaPairFactory = GenerateSimpleCaptcha; DrawingModelFactory = CreateDrawingModel; CharactersFactory = GetCharacters; AddAreaRouteValue = true; }
/// <summary> /// Gets an <see cref="ICaptchaValue" /> associated with the specified token. /// </summary> /// <param name="token">The specified token.</param> /// <param name="tokenType">The specified token type.</param> /// <returns> /// An instance of <see cref="ICaptchaValue" />. /// </returns> public virtual ICaptchaValue GetValue(string token, TokenType tokenType) { Validate.ArgumentNotNullOrEmpty(token, "token"); ICaptchaValue value; switch (tokenType) { case TokenType.Drawing: if (DrawingKeys.TryGetValue(token, out value)) { DrawingKeys.Remove(token); } break; case TokenType.Validation: if (ValidateKeys.TryGetValue(token, out value)) { ValidateKeys.Remove(token); } break; default: throw new ArgumentOutOfRangeException("tokenType"); } return(value); }
/// <summary> /// Removes the specified token and <see cref="ICaptchaValue" /> to the storage. /// </summary> /// <param name="token">The specified token.</param> public bool Remove(string token) { Validate.ArgumentNotNullOrEmpty(token, "token"); var remove = DrawingKeys.Remove(token); var validation = ValidateKeys.Remove(token); return(remove || validation); }
/// <summary> /// Removes the specified token and <see cref="ICaptchaValue" /> to the storage. /// </summary> /// <param name="token">The specified token.</param> public bool Remove(string token) { Validate.ArgumentNotNullOrEmpty(token, "token"); bool removeDr = RemoveFromCookie(CookieName + DrawingKey, token); bool removeVal = RemoveFromCookie(CookieName, token); return(removeDr || removeVal); }
/// <summary> /// Initializes a new instance of the <see cref="CookieStorageProvider" /> class. /// </summary> /// <param name="expiresMinutes">The specified expires of the cookie in minutes.</param> /// <param name="cookieName">The specified cookie name.</param> /// <param name="password">The specified password to encrypt cookie</param> /// <param name="salt">The specified salt to encrypt cookie</param> public CookieStorageProvider(int expiresMinutes, string cookieName, string password, byte[] salt) { Validate.ArgumentNotNullOrEmpty(cookieName, "cookieName"); Validate.ArgumentNotNullOrEmpty(password, "password"); Validate.ArgumentNotNull(salt, "salt"); ExpiresMinutes = expiresMinutes; _salt = salt; _password = password; CookieName = cookieName; }
/// <summary> /// Deserializes the specified <see cref="string" /> into an <see cref="ICaptchaValue" />. /// </summary> /// <param name="value">The specified serialize state.</param> /// <returns> /// The result <see cref="ICaptchaValue" />. /// </returns> protected virtual ICaptchaValue Deserialize(string value) { Validate.ArgumentNotNullOrEmpty(value, "value"); byte[] inputBytes = Convert.FromBase64String(value); using (var pdb = new Rfc2898DeriveBytes(Password, Salt)) { using (var ms = new MemoryStream()) { Rijndael alg = Rijndael.Create(); alg.Key = pdb.GetBytes(32); alg.IV = pdb.GetBytes(16); using (var cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(inputBytes, 0, inputBytes.Length); } value = Encoding.Unicode.GetString(ms.ToArray()); string[] strings = value.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries); if (strings.Length != 2) { throw new ArgumentException( string.Format("It is not possible to deserialize an object from a string '{0}'.", value)); } Type type = Type.GetType(strings[0]); if (type == null) { throw new ArgumentException( string.Format( "It is not possible to deserialize an object from a string '{0}', the type {1} not found.", value, strings[0])); } ConstructorInfo constructorInfo = type.GetConstructor( BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null); if (constructorInfo == null) { throw new ArgumentException( string.Format( "It is not possible to deserialize an object from a string '{0}', type {1}, the constructor with empty types not found.", value, type)); } var result = (ICaptchaValue)constructorInfo.Invoke(new object[0]); result.Deserialize(strings[1]); return(result); } } }
/// <summary> /// Determines whether the <see cref="IStorageProvider" /> contains a specific token. /// </summary> /// <param name="token">The specified token.</param> /// <param name="tokenType">The specified token type.</param> /// <returns> /// <c>True</c> if the value is found in the <see cref="IStorageProvider" />; otherwise <c>false</c>. /// </returns> public virtual bool IsContains(string token, TokenType tokenType) { Validate.ArgumentNotNullOrEmpty(token, "token"); switch (tokenType) { case TokenType.Drawing: return(DrawingKeys.ContainsKey(token)); case TokenType.Validation: return(ValidateKeys.ContainsKey(token)); default: throw new ArgumentOutOfRangeException("tokenType"); } }
/// <summary> /// Gets an <see cref="ICaptchaValue" /> associated with the specified token. /// </summary> /// <param name="token">The specified token.</param> /// <param name="tokenType">The specified token type.</param> /// <returns> /// An instance of <see cref="ICaptchaValue" />. /// </returns> public virtual ICaptchaValue GetValue(string token, TokenType tokenType) { Validate.ArgumentNotNullOrEmpty(token, "token"); switch (tokenType) { case TokenType.Drawing: return(GetFromCookie(CookieName + DrawingKey, token)); case TokenType.Validation: return(GetFromCookie(CookieName, token)); default: throw new ArgumentOutOfRangeException("tokenType"); } }