/MvcLoggingDemo/Global.asax.cs

# · C# · 78 lines · 60 code · 14 blank · 4 comment · 3 complexity · 3fbbbd43e370106c4152cc136f9c1fb9 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Security.Principal;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Web.Routing;
  9. using System.Web.Security;
  10. using MvcLoggingDemo.Models;
  11. using MvcLoggingDemo.Services;
  12. namespace MvcLoggingDemo
  13. {
  14. // Note: For instructions on enabling IIS6 or IIS7 classic mode,
  15. // visit http://go.microsoft.com/?LinkId=9394801
  16. public class MvcApplication : System.Web.HttpApplication
  17. {
  18. public static void RegisterRoutes(RouteCollection routes)
  19. {
  20. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  21. routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
  22. routes.MapRoute(
  23. "Default", // Route name
  24. "{controller}/{action}/{id}", // URL with parameters
  25. new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
  26. );
  27. routes.MapRoute(
  28. "OpenIdDiscover",
  29. "Auth/Discover");
  30. }
  31. public override void Init()
  32. {
  33. this.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);
  34. this.PostAuthenticateRequest += new EventHandler(MvcApplication_PostAuthenticateRequest);
  35. base.Init();
  36. }
  37. protected void Application_Start()
  38. {
  39. AreaRegistration.RegisterAllAreas();
  40. RegisterRoutes(RouteTable.Routes);
  41. // Setup our custom controller factory so that the [HandleErrorWithElmah] attribute
  42. // is automatically injected into all of the controllers
  43. ControllerBuilder.Current.SetControllerFactory(new ErrorHandlingControllerFactory());
  44. }
  45. void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
  46. {
  47. HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
  48. if (authCookie != null)
  49. {
  50. string encTicket = authCookie.Value;
  51. if (!String.IsNullOrEmpty(encTicket))
  52. {
  53. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket);
  54. WebIdentity id = new WebIdentity(ticket);
  55. GenericPrincipal prin = new GenericPrincipal(id, null);
  56. HttpContext.Current.User = prin;
  57. }
  58. }
  59. }
  60. void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
  61. {
  62. }
  63. }
  64. }