/// <summary> /// Binds (and decrypts) an encrypted field to a string /// </summary> /// <param name="controllerContext">The controller context.</param> /// <param name="bindingContext">The binding context.</param> /// <returns>The decrypted string</returns> public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (bindingContext.ModelType != typeof(string)) { throw new InvalidOperationException("You cannot bind an object that is not a string with the CryptoBinder"); } ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (valueProviderResult == null) { return(null); } string encryptedString = valueProviderResult.AttemptedValue; using (ICrypto crypto = new AesCrypto()) { return(crypto.DecryptToString(encryptedString)); } }