asp.net mvc 4 - MVC 4 Windows Authentication & Custom Authorization -


i've been spending time trying grasp best way handle authentication/authorization in app. using windows authentication , able read users name.

to authorize user want query database role , create custom principal user role.

i believe have below work, however, curious if there better way query database once instead of using session variable check?

the code below in global.asax.

protected void application_authenticaterequest(object sender, eventargs args)     {         if (httpcontext.current != null)         {             if (this.session["authenticated"] == null)             {                 using (apptoolsentities db = new apptoolsentities())                 {                     var user = db.adobjects.where(x => x.cn == user.identity.name.removedomain()).firstordefault();                     string[] roles = new string[] { user.title == "ea" ? "reviewer" : "admin" };                     genericprincipal principal = new genericprincipal(httpcontext.current.user.identity, roles);                     thread.currentprincipal = httpcontext.current.user = principal;                     this.session["authenticated"] = true;                 }             }         }     } 

am going right way?

thanks in advance.


Comments