ASP.NET MVC and Windows Authentication with custom roles

后端 未结 2 417
遇见更好的自我
遇见更好的自我 2020-12-02 11:22

I am trying to implement windows authentication in my ASP.NET MVC2 application. I\'ve followed all the steps suggested by the official documentation:



        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 12:12

    Just create a new principal and assign it to the user and thread in Global.asax (or use an action filter).

    protected void Application_AuthenticateRequest(object sender, EventArgs args)
    {
      if(HttpContext.Current != null)
      {
         String [] roles = GetRolesFromSomeDataTable(HttpContext.Current.User.Identity.Name);
    
         GenericPrincipal principal = new GenericPrincipal(HttpContext.Current.User.Identity, roles);
    
         Thread.CurrentPrincipal = HttpContext.Current.User = principal;
      }
    }
    

    If a user doesn't have any role that matches, they can be barred from the app using the web.config authoirzation element:

    
      
                     
    
    

提交回复
热议问题