2 // System.Web.Configuration.MachineKeyValidationConverter
5 // Chris Toshok (toshok@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005, 2010 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.ComponentModel;
33 using System.Configuration;
34 using System.Globalization;
38 namespace System.Web.Configuration {
40 public sealed class MachineKeyValidationConverter : ConfigurationConverterBase
43 const string InvalidValue = "The enumeration value must be one of the following: SHA1, MD5, 3DES, AES, HMACSHA256, HMACSHA384, HMACSHA512.";
45 const string InvalidValue = "The enumeration value must be one of the following: SHA1, MD5, 3DES, AES.";
47 public MachineKeyValidationConverter ()
51 public override object ConvertFrom (ITypeDescriptorContext ctx, CultureInfo ci, object data)
53 switch ((string) data) {
55 return MachineKeyValidation.MD5;
57 return MachineKeyValidation.SHA1;
59 return MachineKeyValidation.TripleDES;
61 return MachineKeyValidation.AES;
64 return MachineKeyValidation.HMACSHA256;
66 return MachineKeyValidation.HMACSHA384;
68 return MachineKeyValidation.HMACSHA512;
71 throw new ArgumentException (InvalidValue);
75 public override object ConvertTo (ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type)
78 if ((value == null) || (value.GetType () != typeof (MachineKeyValidation)))
79 throw new ArgumentException (InvalidValue);
81 if (value.GetType () != typeof (MachineKeyValidation)) {
82 /* MS throws this exception on an invalid */
83 throw new FormatException (InvalidValue);
87 switch ((MachineKeyValidation) value) {
88 case MachineKeyValidation.MD5:
90 case MachineKeyValidation.SHA1:
92 case MachineKeyValidation.TripleDES:
94 case MachineKeyValidation.AES:
97 case MachineKeyValidation.HMACSHA256:
99 case MachineKeyValidation.HMACSHA384:
101 case MachineKeyValidation.HMACSHA512:
104 // includes MachineKeyValidation.Custom
105 throw new ArgumentException (InvalidValue);
108 /* MS throws this exception on an invalid */
109 throw new FormatException (InvalidValue);