2 // System.Web.Configuration.AuthenticationSectionHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Configuration;
36 namespace System.Web.Configuration
38 class AuthenticationConfigHandler : IConfigurationSectionHandler
40 public object Create (object parent, object context, XmlNode section)
42 AuthConfig config = new AuthConfig (parent);
44 string mode = AttValue ("mode", section);
46 config.SetMode (mode);
48 if (section.Attributes != null && section.Attributes.Count != 0)
49 ThrowException ("Unrecognized attribute", section);
51 XmlNodeList authNodes = section.ChildNodes;
52 foreach (XmlNode child in authNodes) {
53 XmlNodeType ntype = child.NodeType;
54 if (ntype != XmlNodeType.Element)
57 if (child.Name == "forms") {
58 config.CookieName = AttValue ("name", child);
59 config.CookiePath = AttValue ("path", child);
60 config.LoginUrl = AttValue ("loginUrl", child);
61 config.SetProtection (AttValue ("protection", child));
62 config.SetTimeout (AttValue ("timeout", child));
64 string att = AttValue ("requireSSL", child);
67 config.RequireSSL = true;
68 } else if (att == "false") {
69 config.RequireSSL = false;
71 HandlersUtil.ThrowException
72 ("Invalid value for RequireSSL", child);
76 att = AttValue ("slidingExpiration", child);
79 config.SlidingExpiration = true;
80 } else if (att == "false") {
81 config.SlidingExpiration = false;
83 HandlersUtil.ThrowException
84 ("Invalid value for SlidingExpiration", child);
89 ReadCredentials (child.ChildNodes, config);
93 if (child.Name == "passport") {
97 HandlersUtil.ThrowException ("Unexpected element", child);
103 static void ReadCredentials (XmlNodeList nodes, AuthConfig config)
105 foreach (XmlNode child in nodes) {
106 XmlNodeType ntype = child.NodeType;
107 if (ntype != XmlNodeType.Element)
110 if (child.Name != "credentials")
111 HandlersUtil.ThrowException ("Unexpected element", child);
113 config.SetPasswordFormat (AttValue ("passwordFormat", child));
114 ReadUsers (child.ChildNodes, config.CredentialUsers);
118 static void ReadUsers (XmlNodeList nodes, Hashtable users)
120 foreach (XmlNode child in nodes) {
121 XmlNodeType ntype = child.NodeType;
122 if (ntype != XmlNodeType.Element)
125 if (child.Name != "user")
126 HandlersUtil.ThrowException ("Unexpected element", child);
128 string name = AttValue ("name", child, false);
129 string password = AttValue ("password", child);
130 if (users.ContainsKey (name))
131 ThrowException ("User '" + name + "' already added.", child);
133 users [name] = password;
134 if (child.HasChildNodes)
135 ThrowException ("Child nodes not allowed here", child.FirstChild);
138 // A few methods to save some typing
139 static string AttValue (string name, XmlNode node, bool optional)
141 return HandlersUtil.ExtractAttributeValue (name, node, optional);
144 static string AttValue (string name, XmlNode node)
146 return HandlersUtil.ExtractAttributeValue (name, node, true);
149 static void ThrowException (string message, XmlNode node)
151 HandlersUtil.ThrowException (message, node);