BasicAuthenticationModule.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Basic Authentication implementation
  3. //
  4. // Authors:
  5. // Greg Reinacker ([email protected])
  6. // Sebastien Pouliot ([email protected])
  7. //
  8. // Copyright 2002-2003 Greg Reinacker, Reinacker & Associates, Inc. All rights reserved.
  9. // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  10. //
  11. // Based on "DigestAuthenticationModule.cs". Original source code available at
  12. // http://www.rassoc.com/gregr/weblog/stories/2002/07/09/webServicesSecurityHttpDigestAuthenticationWithoutActiveDirectory.html
  13. //
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.Configuration;
  36. using System.IO;
  37. using System.Security.Principal;
  38. using System.Text;
  39. using System.Web;
  40. using System.Xml;
  41. namespace Mono.Http.Modules
  42. {
  43. public class BasicAuthenticationModule : AuthenticationModule
  44. {
  45. static char[] separator = {':'};
  46. public BasicAuthenticationModule () : base ("Basic") {}
  47. protected override bool AcceptCredentials (HttpApplication app, string authentication)
  48. {
  49. byte[] userpass = Convert.FromBase64String (authentication);
  50. string[] up = Encoding.UTF8.GetString (userpass).Split (separator);
  51. string username = up [0];
  52. string password = up [1];
  53. string userFileName = app.Request.MapPath (ConfigurationSettings.AppSettings ["Basic.Users"]);
  54. if (userFileName == null || !File.Exists (userFileName))
  55. return false;
  56. XmlDocument userDoc = new XmlDocument ();
  57. userDoc.Load (userFileName);
  58. string xPath = String.Format ("/users/user[@name='{0}']", username);
  59. XmlNode user = userDoc.SelectSingleNode (xPath);
  60. if (user == null)
  61. return false;
  62. XmlAttribute att = user.Attributes ["password"];
  63. if (att == null || password != att.Value)
  64. return false;
  65. XmlNodeList roleNodes = user.SelectNodes ("role");
  66. string[] roles = new string [roleNodes.Count];
  67. int i = 0;
  68. foreach (XmlNode xn in roleNodes) {
  69. XmlAttribute rolename = xn.Attributes ["name"];
  70. if (rolename == null)
  71. continue;
  72. roles [i++] = rolename.Value;
  73. }
  74. app.Context.User = new GenericPrincipal (new GenericIdentity (username, AuthenticationMethod), roles);
  75. return true;
  76. }
  77. #region Event Handlers
  78. // We add the WWW-Authenticate header here, so if an authorization
  79. // fails elsewhere than in this module, we can still request authentication
  80. // from the client.
  81. public override void OnEndRequest (object source, EventArgs eventArgs)
  82. {
  83. HttpApplication app = (HttpApplication) source;
  84. if (app.Response.StatusCode != 401 || !AuthenticationRequired)
  85. return;
  86. string realm = ConfigurationSettings.AppSettings ["Basic.Realm"];
  87. string challenge = String.Format ("{0} realm=\"{1}\"", AuthenticationMethod, realm);
  88. app.Response.AppendHeader ("WWW-Authenticate", challenge);
  89. }
  90. #endregion
  91. }
  92. }