2
0

UrlAuthorizationModule.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // System.Web.Security.UrlAuthorizationModule
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.Web;
  11. using System.Web.Configuration;
  12. using System.Security.Principal;
  13. namespace System.Web.Security
  14. {
  15. public sealed class UrlAuthorizationModule : IHttpModule
  16. {
  17. public UrlAuthorizationModule ()
  18. {
  19. }
  20. public void Dispose ()
  21. {
  22. }
  23. public void Init (HttpApplication app)
  24. {
  25. app.AuthorizeRequest += new EventHandler (OnAuthorizeRequest);
  26. }
  27. void OnAuthorizeRequest (object sender, EventArgs args)
  28. {
  29. HttpApplication app = (HttpApplication) sender;
  30. HttpContext context = app.Context;
  31. if (context.SkipAuthorization)
  32. return;
  33. AuthorizationConfig config = (AuthorizationConfig) context.GetConfig ("system.web/authorization");
  34. if (config == null)
  35. return;
  36. if (!config.IsValidUser (context.User, context.Request.HttpMethod)) {
  37. HttpException e = new HttpException (401, "Forbidden");
  38. context.Response.StatusCode = 401;
  39. context.Response.Write (e.GetHtmlErrorMessage ());
  40. app.CompleteRequest ();
  41. }
  42. }
  43. }
  44. }