-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
{
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
+ }
+ }
+ }
+ }
}
}