AuthenticationConfigHandler.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. using System;
  10. using System.Collections;
  11. using System.Configuration;
  12. using System.Xml;
  13. namespace System.Web.Configuration
  14. {
  15. class AuthenticationConfigHandler : IConfigurationSectionHandler
  16. {
  17. public object Create (object parent, object context, XmlNode section)
  18. {
  19. AuthConfig config = new AuthConfig (parent);
  20. string mode = AttValue ("mode", section);
  21. if (mode != null)
  22. config.SetMode (mode);
  23. if (section.Attributes != null && section.Attributes.Count != 0)
  24. ThrowException ("Unrecognized attribute", section);
  25. XmlNodeList authNodes = section.ChildNodes;
  26. foreach (XmlNode child in authNodes) {
  27. XmlNodeType ntype = child.NodeType;
  28. if (ntype != XmlNodeType.Element)
  29. continue;
  30. if (child.Name == "forms") {
  31. config.CookieName = AttValue ("name", child);
  32. config.CookiePath = AttValue ("path", child);
  33. config.LoginUrl = AttValue ("loginUrl", child);
  34. config.SetProtection (AttValue ("protection", child));
  35. config.SetTimeout (AttValue ("timeout", child));
  36. #if NET_1_1
  37. string att = AttValue ("requireSSL", child);
  38. if (att != null) {
  39. if (att == "true") {
  40. config.RequireSSL = true;
  41. } else if (att == "false") {
  42. config.RequireSSL = false;
  43. } else {
  44. HandlersUtil.ThrowException
  45. ("Invalid value for RequireSSL", child);
  46. }
  47. }
  48. att = AttValue ("slidingExpiration", child);
  49. if (att != null) {
  50. if (att == "true") {
  51. config.SlidingExpiration = true;
  52. } else if (att == "false") {
  53. config.SlidingExpiration = false;
  54. } else {
  55. HandlersUtil.ThrowException
  56. ("Invalid value for SlidingExpiration", child);
  57. }
  58. }
  59. #endif
  60. ReadCredentials (child.ChildNodes, config);
  61. continue;
  62. }
  63. if (child.Name == "passport") {
  64. continue;
  65. }
  66. HandlersUtil.ThrowException ("Unexpected element", child);
  67. }
  68. return config;
  69. }
  70. static void ReadCredentials (XmlNodeList nodes, AuthConfig config)
  71. {
  72. foreach (XmlNode child in nodes) {
  73. XmlNodeType ntype = child.NodeType;
  74. if (ntype != XmlNodeType.Element)
  75. continue;
  76. if (child.Name != "credentials")
  77. HandlersUtil.ThrowException ("Unexpected element", child);
  78. config.SetPasswordFormat (AttValue ("passwordFormat", child));
  79. ReadUsers (child.ChildNodes, config.CredentialUsers);
  80. }
  81. }
  82. static void ReadUsers (XmlNodeList nodes, Hashtable users)
  83. {
  84. foreach (XmlNode child in nodes) {
  85. XmlNodeType ntype = child.NodeType;
  86. if (ntype != XmlNodeType.Element)
  87. continue;
  88. if (child.Name != "user")
  89. HandlersUtil.ThrowException ("Unexpected element", child);
  90. string name = AttValue ("name", child, false);
  91. string password = AttValue ("password", child);
  92. if (users.ContainsKey (name))
  93. ThrowException ("User '" + name + "' already added.", child);
  94. users [name] = password;
  95. if (child.HasChildNodes)
  96. ThrowException ("Child nodes not allowed here", child.FirstChild);
  97. }
  98. }
  99. // A few methods to save some typing
  100. static string AttValue (string name, XmlNode node, bool optional)
  101. {
  102. return HandlersUtil.ExtractAttributeValue (name, node, optional);
  103. }
  104. static string AttValue (string name, XmlNode node)
  105. {
  106. return HandlersUtil.ExtractAttributeValue (name, node, true);
  107. }
  108. static void ThrowException (string message, XmlNode node)
  109. {
  110. HandlersUtil.ThrowException (message, node);
  111. }
  112. //
  113. }
  114. }