【问题标题】:MVC4 Forms Authentication Active Directory Custom Authorize AttributeMVC4 表单身份验证 Active Directory 自定义授权属性
【发布时间】:2013-04-20 20:43:02
【问题描述】:

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

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 类型的另一个参数来执行看起来沿着所需路径传递的操作重定向。

【问题讨论】:

标签: c# asp.net-mvc-4 active-directory authorization forms-authentication


【解决方案1】:

我认为您还应该覆盖OnAuthorization 方法。这有一个AuthorizationContext 参数,可以让您将结果设置为您喜欢的RedirectResult...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    相关资源
    最近更新 更多