AuthenticationConfigHandler.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. ReadCredentials (child.ChildNodes, config);
  38. continue;
  39. }
  40. if (child.Name == "passport") {
  41. Console.WriteLine ("**WARNING**: Passport not supported! Ignoring section.");
  42. continue;
  43. }
  44. HandlersUtil.ThrowException ("Unexpected element", child);
  45. }
  46. return config;
  47. }
  48. static void ReadCredentials (XmlNodeList nodes, AuthConfig config)
  49. {
  50. foreach (XmlNode child in nodes) {
  51. XmlNodeType ntype = child.NodeType;
  52. if (ntype != XmlNodeType.Element)
  53. continue;
  54. if (child.Name != "credentials")
  55. HandlersUtil.ThrowException ("Unexpected element", child);
  56. config.SetPasswordFormat (AttValue ("passwordFormat", child));
  57. ReadUsers (child.ChildNodes, config.CredentialUsers);
  58. }
  59. }
  60. static void ReadUsers (XmlNodeList nodes, Hashtable users)
  61. {
  62. foreach (XmlNode child in nodes) {
  63. XmlNodeType ntype = child.NodeType;
  64. if (ntype != XmlNodeType.Element)
  65. continue;
  66. if (child.Name != "user")
  67. HandlersUtil.ThrowException ("Unexpected element", child);
  68. string name = AttValue ("name", child, false);
  69. string password = AttValue ("password", child);
  70. if (users.ContainsKey (name))
  71. ThrowException ("User '" + name + "' already added.", child);
  72. users [name] = password;
  73. if (child.HasChildNodes)
  74. ThrowException ("Child nodes not allowed here", child.FirstChild);
  75. }
  76. }
  77. // A few methods to save some typing
  78. static string AttValue (string name, XmlNode node, bool optional)
  79. {
  80. return HandlersUtil.ExtractAttributeValue (name, node, optional);
  81. }
  82. static string AttValue (string name, XmlNode node)
  83. {
  84. return HandlersUtil.ExtractAttributeValue (name, node, true);
  85. }
  86. static void ThrowException (string message, XmlNode node)
  87. {
  88. HandlersUtil.ThrowException (message, node);
  89. }
  90. //
  91. }
  92. }