2005-10-29 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration / MachineKeyConfigHandler.cs
1 //
2 // System.Web.Configuration.MachineKeyConfigHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Configuration;
34 using System.Xml;
35
36 namespace System.Web.Configuration
37 {
38         class MachineKeyConfigHandler : IConfigurationSectionHandler
39         {
40                 public object Create (object parent, object context, XmlNode section)
41                 {
42                         if (section.HasChildNodes)
43                                 ThrowException ("Child nodes not allowed here", section.FirstChild);
44
45                         MachineKeyConfig config = new MachineKeyConfig (parent);
46
47                         try {
48                                 config.SetValidationKey (AttValue ("validationKey", section));
49                         } catch (ArgumentException e) {
50                                 ThrowException (e.Message, section);
51                         }
52
53                         try {
54                                 config.SetDecryptionKey (AttValue ("decryptionKey", section));
55                         } catch (ArgumentException e) {
56                                 ThrowException (e.Message, section);
57                         }
58
59                         string validation = AttValue ("validation", section);
60                         MachineKeyValidation valid = 0;
61                         if (validation == "SHA1")
62                                 valid = MachineKeyValidation.SHA1;
63                         else if (validation == "MD5")
64                                 valid = MachineKeyValidation.MD5;
65                         else if (validation == "TripleDES")
66                                 valid = MachineKeyValidation.TripleDES;
67                         else
68                                 ThrowException ("Invalid 'validation' value", section);
69
70                         config.ValidationType = valid;
71
72                         if (section.Attributes != null && section.Attributes.Count != 0)
73                                 ThrowException ("Unrecognized attribute", section);
74
75                         return config;
76                 }
77
78                 // A few methods to save some typing
79                 static string AttValue (string name, XmlNode node)
80                 {
81                         return HandlersUtil.ExtractAttributeValue (name, node, true);
82                 }
83
84                 static void ThrowException (string message, XmlNode node)
85                 {
86                         HandlersUtil.ThrowException (message, node);
87                 }
88                 //
89
90         }
91 }
92