Custom logging
[wsti_pai.git] / Projects / Global.asax.cs
index c0cafe1af1166bcbc0021dc4d6ee02b8856c5cb6..61f3d92a52d4f617ae55b7c36d3778483129c455 100644 (file)
@@ -1,10 +1,12 @@
-using System;
+using Projects.Models;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.Mvc;
 using System.Web.Optimization;
 using System.Web.Routing;
+using System.Web.Security;
 
 namespace Projects
 {
@@ -17,5 +19,38 @@ namespace Projects
             RouteConfig.RegisterRoutes(RouteTable.Routes);
             BundleConfig.RegisterBundles(BundleTable.Bundles);
         }
+
+        protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
+        {
+            if (FormsAuthentication.CookiesSupported == true)
+            {
+                if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
+                {
+                    try
+                    {
+                        //let us take out the username now                
+                        string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
+                        string roles = string.Empty;
+
+                        using (ProjectsDBEntities entities = new ProjectsDBEntities())
+                        {
+                            User user = entities.Users.SingleOrDefault(u => u.login == username);
+
+                            roles = user.UserRole.role.Trim();
+                        }
+                        //let us extract the roles from our own custom cookie
+
+
+                        //Let us set the Pricipal with our user specific details
+                        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(
+                          new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
+                    }
+                    catch (Exception)
+                    {
+                        //somehting went wrong
+                    }
+                }
+            }
+        }
     }
 }