AuthenticationConfigHandler.cs 3.4 KB

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