Fix for running against RabbitMQ 2.2
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / MachineKeyValidationConverter.cs
1 //
2 // System.Web.Configuration.MachineKeyValidationConverter
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005, 2010 Novell, Inc (http://www.novell.com)
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System.ComponentModel;
33 using System.Configuration;
34 using System.Globalization;
35
36 #if NET_2_0
37
38 namespace System.Web.Configuration {
39
40         public sealed class MachineKeyValidationConverter : ConfigurationConverterBase
41         {
42 #if NET_4_0
43                 const string InvalidValue = "The enumeration value must be one of the following: SHA1, MD5, 3DES, AES, HMACSHA256, HMACSHA384, HMACSHA512."; 
44 #else
45                 const string InvalidValue = "The enumeration value must be one of the following: SHA1, MD5, 3DES, AES."; 
46 #endif
47                 public MachineKeyValidationConverter ()
48                 {
49                 }
50
51                 public override object ConvertFrom (ITypeDescriptorContext ctx, CultureInfo ci, object data)
52                 {
53                         switch ((string) data) {
54                         case "MD5":
55                                 return MachineKeyValidation.MD5;
56                         case "SHA1":
57                                 return MachineKeyValidation.SHA1;
58                         case "3DES":
59                                 return MachineKeyValidation.TripleDES;
60                         case "AES":
61                                 return MachineKeyValidation.AES;
62 #if NET_4_0
63                         case "HMACSHA256":
64                                 return MachineKeyValidation.HMACSHA256;
65                         case "HMACSHA384":
66                                 return MachineKeyValidation.HMACSHA384;
67                         case "HMACSHA512":
68                                 return MachineKeyValidation.HMACSHA512;
69 #endif
70                         default:
71                                 throw new ArgumentException (InvalidValue);
72                         }
73                 }
74
75                 public override object ConvertTo (ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type)
76                 {
77 #if NET_4_0
78                         if ((value == null) || (value.GetType () != typeof (MachineKeyValidation)))
79                                 throw new ArgumentException (InvalidValue);
80 #else
81                         if (value.GetType () != typeof (MachineKeyValidation)) {
82                                 /* MS throws this exception on an invalid */
83                                 throw new FormatException (InvalidValue);
84                         }                               
85 #endif
86
87                         switch ((MachineKeyValidation) value) {
88                         case MachineKeyValidation.MD5:
89                                 return "MD5";
90                         case MachineKeyValidation.SHA1:
91                                 return "SHA1";
92                         case MachineKeyValidation.TripleDES:
93                                 return "3DES";
94                         case MachineKeyValidation.AES:
95                                 return "AES";
96 #if NET_4_0
97                         case MachineKeyValidation.HMACSHA256:
98                                 return "HMACSHA256";
99                         case MachineKeyValidation.HMACSHA384:
100                                 return "HMACSHA384";
101                         case MachineKeyValidation.HMACSHA512:
102                                 return "HMACSHA512";
103                         default:
104                                 // includes MachineKeyValidation.Custom
105                                 throw new ArgumentException (InvalidValue);
106 #else
107                         default:
108                                 /* MS throws this exception on an invalid */
109                                 throw new FormatException (InvalidValue);
110 #endif
111                         }
112                 }
113         }
114 }
115
116 #endif