0

在我的C#MVC4应用程序中,我使用基于窗体的身份验证与Active Directory。我有一个自定义AD成员资格提供者。我已经测试成功,可以读取并验证用户属于哪个组。现在,我试着去创建一个自定义授权属性,该属性将执行以下操作:MVC4表单身份验证Active Directory自定义授权属性

if (user is logged-in/not timed-out/authenticated) 
{ 
    if (user's role is equal to role 1 or role 2) 
     { 
     return a specific view or (preferably) perform a specific redirect to action 
     } 
    else 
     { 
     return a different specific view or (preferably) perform a different specific  redirect to action 
     } 
} 
else 
    {  
    return View 
    } 

这是我到目前为止有:

public class AuthorizeEditAttribute : AuthorizeAttribute 
    { 
     protected override bool AuthorizeCore(HttpContextBase httpContext) 
     { 
      if (httpContext.Request.IsAuthenticated) 
      { 
       if ((httpContext.User.IsInRole("group1")) || (httpContext.User.IsInRole("group2"))) 
       { 

        return true; 
       } 
       else 
       { 
        return false; 
       } 
      } 
      else 
      { 
       return false; 
      } 
} 

我无法弄清楚如何还执行重定向任务。我已经看过这个post讨论如何做重定向,但不明白我如何可以将其与我迄今为止的内容进行整合。特别是因为我相信我必须使用AuthorizeCore访问httpcontext.user才能执行第一次检查,并且我不知道如何传递需要的另一个类型为AuthorizationContext的参数,以执行看起来正沿着期望的路径传递重定向。

+0

请看看它是否能帮助 [http://stackoverflow.com/questions/35120816/mvc4-and-ef5 -with-有源目录的验证和 - 角色的在-SQL](http://stackoverflow.com/questions/35120816/mvc4-and-ef5-with-active-directory-authentication-and-roles-in- SQL) – user2988717 2016-02-01 10:57:26

回答

1

我想你也应该覆盖OnAuthorization方法。这有一个AuthorizationContext参数可能让你到结果集根据自己的喜好的RedirectResult ...