Custom logging
[wsti_pai.git] / Projects / Global.asax.cs
1 using Projects.Models;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Web;
6 using System.Web.Mvc;
7 using System.Web.Optimization;
8 using System.Web.Routing;
9 using System.Web.Security;
10
11 namespace Projects
12 {
13     public class MvcApplication : System.Web.HttpApplication
14     {
15         protected void Application_Start()
16         {
17             AreaRegistration.RegisterAllAreas();
18             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
19             RouteConfig.RegisterRoutes(RouteTable.Routes);
20             BundleConfig.RegisterBundles(BundleTable.Bundles);
21         }
22
23         protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
24         {
25             if (FormsAuthentication.CookiesSupported == true)
26             {
27                 if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
28                 {
29                     try
30                     {
31                         //let us take out the username now                
32                         string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
33                         string roles = string.Empty;
34
35                         using (ProjectsDBEntities entities = new ProjectsDBEntities())
36                         {
37                             User user = entities.Users.SingleOrDefault(u => u.login == username);
38
39                             roles = user.UserRole.role.Trim();
40                         }
41                         //let us extract the roles from our own custom cookie
42
43
44                         //Let us set the Pricipal with our user specific details
45                         HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(
46                           new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
47                     }
48                     catch (Exception)
49                     {
50                         //somehting went wrong
51                     }
52                 }
53             }
54         }
55     }
56 }