AuthenticationConfigHandler.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // System.Web.Configuration.AuthenticationSectionHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Configuration;
  32. using System.Xml;
  33. namespace System.Web.Configuration
  34. {
  35. class AuthenticationConfigHandler : IConfigurationSectionHandler
  36. {
  37. public object Create (object parent, object context, XmlNode section)
  38. {
  39. AuthConfig config = new AuthConfig (parent);
  40. string mode = AttValue ("mode", section);
  41. if (mode != null)
  42. config.SetMode (mode);
  43. if (section.Attributes != null && section.Attributes.Count != 0)
  44. ThrowException ("Unrecognized attribute", section);
  45. XmlNodeList authNodes = section.ChildNodes;
  46. foreach (XmlNode child in authNodes) {
  47. XmlNodeType ntype = child.NodeType;
  48. if (ntype != XmlNodeType.Element)
  49. continue;
  50. if (child.Name == "forms") {
  51. config.CookieName = AttValue ("name", child);
  52. config.CookiePath = AttValue ("path", child);
  53. config.LoginUrl = AttValue ("loginUrl", child);
  54. config.SetProtection (AttValue ("protection", child));
  55. config.SetTimeout (AttValue ("timeout", child));
  56. #if NET_1_1
  57. string att = AttValue ("requireSSL", child);
  58. if (att != null) {
  59. if (att == "true") {
  60. config.RequireSSL = true;
  61. } else if (att == "false") {
  62. config.RequireSSL = false;
  63. } else {
  64. HandlersUtil.ThrowException
  65. ("Invalid value for RequireSSL", child);
  66. }
  67. }
  68. att = AttValue ("slidingExpiration", child);
  69. if (att != null) {
  70. if (att == "true") {
  71. config.SlidingExpiration = true;
  72. } else if (att == "false") {
  73. config.SlidingExpiration = false;
  74. } else {
  75. HandlersUtil.ThrowException
  76. ("Invalid value for SlidingExpiration", child);
  77. }
  78. }
  79. #endif
  80. ReadCredentials (child.ChildNodes, config);
  81. continue;
  82. }
  83. if (child.Name == "passport") {
  84. continue;
  85. }
  86. HandlersUtil.ThrowException ("Unexpected element", child);
  87. }
  88. return config;
  89. }
  90. static void ReadCredentials (XmlNodeList nodes, AuthConfig config)
  91. {
  92. foreach (XmlNode child in nodes) {
  93. XmlNodeType ntype = child.NodeType;
  94. if (ntype != XmlNodeType.Element)
  95. continue;
  96. if (child.Name != "credentials")
  97. HandlersUtil.ThrowException ("Unexpected element", child);
  98. config.SetPasswordFormat (AttValue ("passwordFormat", child));
  99. ReadUsers (child.ChildNodes, config.CredentialUsers);
  100. }
  101. }
  102. static void ReadUsers (XmlNodeList nodes, Hashtable users)
  103. {
  104. foreach (XmlNode child in nodes) {
  105. XmlNodeType ntype = child.NodeType;
  106. if (ntype != XmlNodeType.Element)
  107. continue;
  108. if (child.Name != "user")
  109. HandlersUtil.ThrowException ("Unexpected element", child);
  110. string name = AttValue ("name", child, false);
  111. string password = AttValue ("password", child);
  112. if (users.ContainsKey (name))
  113. ThrowException ("User '" + name + "' already added.", child);
  114. users [name] = password;
  115. if (child.HasChildNodes)
  116. ThrowException ("Child nodes not allowed here", child.FirstChild);
  117. }
  118. }
  119. // A few methods to save some typing
  120. static string AttValue (string name, XmlNode node, bool optional)
  121. {
  122. return HandlersUtil.ExtractAttributeValue (name, node, optional);
  123. }
  124. static string AttValue (string name, XmlNode node)
  125. {
  126. return HandlersUtil.ExtractAttributeValue (name, node, true);
  127. }
  128. static void ThrowException (string message, XmlNode node)
  129. {
  130. HandlersUtil.ThrowException (message, node);
  131. }
  132. //
  133. }
  134. }