NetAuthenticationModuleHandler.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // System.Net.Configuration.NetAuthenticationModuleHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System.Collections;
  10. using System.Configuration;
  11. using System.Xml;
  12. namespace System.Net.Configuration
  13. {
  14. class NetAuthenticationModuleHandler : IConfigurationSectionHandler
  15. {
  16. public virtual object Create (object parent, object configContext, XmlNode section)
  17. {
  18. if (section.Attributes != null && section.Attributes.Count != 0)
  19. HandlersUtil.ThrowException ("Unrecognized attribute", section);
  20. XmlNodeList httpHandlers = section.ChildNodes;
  21. foreach (XmlNode child in httpHandlers) {
  22. XmlNodeType ntype = child.NodeType;
  23. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  24. continue;
  25. if (ntype != XmlNodeType.Element)
  26. HandlersUtil.ThrowException ("Only elements allowed", child);
  27. string name = child.Name;
  28. if (name == "clear") {
  29. if (child.Attributes != null && child.Attributes.Count != 0)
  30. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  31. AuthenticationManager.Clear ();
  32. continue;
  33. }
  34. string type = HandlersUtil.ExtractAttributeValue ("type", child);
  35. if (child.Attributes != null && child.Attributes.Count != 0)
  36. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  37. if (name == "add") {
  38. AuthenticationManager.Register (CreateInstance (type, child));
  39. continue;
  40. }
  41. if (name == "remove") {
  42. AuthenticationManager.Unregister (CreateInstance (type, child));
  43. continue;
  44. }
  45. HandlersUtil.ThrowException ("Unexpected element", child);
  46. }
  47. return AuthenticationManager.RegisteredModules;
  48. }
  49. static IAuthenticationModule CreateInstance (string typeName, XmlNode node)
  50. {
  51. IAuthenticationModule module = null;
  52. try {
  53. Type type = Type.GetType (typeName, true);
  54. module = (IAuthenticationModule) Activator.CreateInstance (type);
  55. } catch (Exception e) {
  56. HandlersUtil.ThrowException (e.Message, node);
  57. }
  58. return module;
  59. }
  60. }
  61. }