2016-01-21 57 views
0

我有一个MVC5网站,我希望任何人都能够从受限制的管理区域以外查看站点。我已经创建了一个过滤器来检查我已经应用到管理控制器的用户AD组,并且这个工作非常好。授权页面上的MVC Windows身份验证401

这些问题是为人们查看主站点,如果它闲置一段时间,他们会在导航到页面时收到未经授权的响应。一旦他们刷新它的罚款,并让他们回到页面。

的Web.Config在

<authentication mode="Windows" /> 

以下如果我删除此则问题消失,但管理控制器不工作,因为在我的自定义过滤器没有发现Windows用户。

的自定义过滤器只检查一个AD组:

var groupList = GetGroupList(); 

     if (base.AuthorizeCore(httpContext)) 
     { 
      if (string.IsNullOrEmpty(groupList)) 
       return true; 

      var groups = groupList.Split(',').ToList(); 

      var context = new PrincipalContext(ContextType.Domain, "MYDOMAIN"); 

      var userPrincipal = UserPrincipal.FindByIdentity(
       context, 
       IdentityType.SamAccountName, 
       httpContext.User.Identity.Name); 

      if (userPrincipal == null) 
       return false; 

      try 
      { 
       foreach (var group in groups) 
        if (userPrincipal.IsMemberOf(context, 
         IdentityType.Name, 
         group)) 
         return true; 
      } 
      catch 
      { 
       return false; 
      } 

回答

0

我想你想重定向未授权访问一些错误页面上的用户。 你可以通过在web配置中添加这个来做到这一点。 (AccessDenied.aspx页面中,你已经写了对于联合国授权的用户访问的消息)

 <customErrors defaultRedirect="ErrorPage.aspx" mode="On"> 
    <error statusCode="401" redirect="AccessDenied.aspx" />  
    </customErrors> 

,或者你可以做到这一点在Global.asax文件

 protected void Application_AuthenticateRequest(Object sender, 
               EventArgs e) 
     { 
     if (!Request.IsAuthenticated) Response.Redirect(
             "AccessDenied.aspx"); 
    } 
+0

由于某些原因,即使控制器没有[Authorize]属性,我仍然得到重定向到访问被拒绝的页面,任何人都应该能够查看页面。 – tonyjoanes

0

在我最近的经验,与AD工作从一个IIS框中,我使用一个完全相同的过滤器来按AD组进行过滤。虽然我的过滤器是相同的,但我注意到httpContext.User.Identity.Name不会像控制器中的“正常”匿名网站那样返回用户。

,并使其自动地通过AD用户SANS授权的登录界面,我必须禁用匿名身份验证,启用Windows验证,设置<identity impersonate="true" />及用途:

UserPrincipal adUser = null; 
     using (HostingEnvironment.Impersonate()) 
     { 
      var userContext = System.Web.HttpContext.Current.User.Identity; 
      PrincipalContext ctx = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["AllowedDomain"], null, 
       ContextOptions.Negotiate | ContextOptions.SecureSocketLayer); 
      adUser = UserPrincipal.FindByIdentity(ctx, userContext.Name); 
     } 
// and work with 'adUser' from here on in my controllers... 

我的应用才刚刚进入生产内部,但现在我没有遇到你的假期“超时”问题。你的web.config中有没有多余的表单auth标签?我也不得不在我的web.config中设置<sessionState mode="InProc" />