【问题标题】:Authorize Attribute working unexpectively授权属性意外工作
【发布时间】:2015-12-18 20:12:42
【问题描述】:

我已扩展 Authorize 属性以包含来自 cookie 的角色。调试给出了很好的结果,它相应地返回真或假。但是,如果我首先使用“管理员”角色登录,然后尝试转到需要用户角色的控制器,授权返回 false,但控制器仍然允许访问。

protected override bool AuthorizeCore(HttpContextBase httpContext) 
    {


        if (httpContext == null) throw new ArgumentNullException("httpContext"); 

        if (httpContext.User != null)
        {
            if (httpContext.User.Identity.IsAuthenticated)
            {
                if (httpContext.User.Identity is FormsIdentity)
                {
                    FormsIdentity id = httpContext.User.Identity as FormsIdentity;
                    FormsAuthenticationTicket ticket = id.Ticket;
                    string role = ticket.UserData;

                    if (RequiredRole.Contains(role)) return true;
                }
            }
            else 
                return false;

        }
        return false;

    }

Requiredrole 是类的一个属性。

 [CustomAuthorize(RequiredRole = "Admin", LoginPage = "Club")]
public class UsuarioAdminController : Controller
{

上面是需要管理员角色的控制器的代码。

[CustomAuthorize(RequiredRole = "User", LoginPage = "Club")]
public class HotelController : Controller
{

以上代码用于具有用户角色的控制器。 有人能明白为什么如果 Authorize 返回 false 它允许访问吗?谢谢

AuthorizeCore 属性的行为符合预期,它返回 true 或 false;但是当 AuthorizeCore 方法返回 false 时控制器允许访问。

是的,还有更多代码,但我不认为它有什么不同……就是这样。

public class CustomAuthorizeAttribute: AuthorizeAttribute
{
    public string RequiredRole;
    public string LoginPage;

    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    {


        if (httpContext == null) throw new ArgumentNullException("httpContext"); 

        if (httpContext.User != null)
        {
            if (httpContext.User.Identity.IsAuthenticated)
            {
                if (httpContext.User.Identity is FormsIdentity)
                {
                    FormsIdentity id = httpContext.User.Identity as FormsIdentity;
                    FormsAuthenticationTicket ticket = id.Ticket;
                    string role = ticket.UserData;

                    if (RequiredRole.Contains(role)) return true;
                }
            }
            else 
                return false;

        }
        return false;

    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {

        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            var routeValues = new RouteValueDictionary();
            if (LoginPage == "Club")
            {
                routeValues["action"] = "Index";
                routeValues["controller"] = LoginPage;
                routeValues["ReturnUrl"] = filterContext.HttpContext.Request.RawUrl;
                filterContext.Result = new RedirectToRouteResult(routeValues);
            }
            else {
                routeValues["area"] = "mobile";
                routeValues["action"] = "login";
                routeValues["controller"] = LoginPage;
                routeValues["ReturnUrl"] = filterContext.HttpContext.Request.RawUrl;
                filterContext.Result = new RedirectToRouteResult(routeValues);
            }
        }

    }

}

【问题讨论】:

  • 设置一个断点并检查AuthorizeCore 代码中发生了什么,当它返回false 时。
  • 是的,我做到了。它确实返回我需要的东西,真或假。但即使它返回 false 控制器也允许访问。
  • 哦,原来如此,不好意思,之前没看懂。
  • 请显示您自定义AuthorizeAttribute 代码的其余部分。最有可能的是,您已经覆盖了其他导致它无法运行的东西。

标签: c# asp.net-mvc


【解决方案1】:

我找到了这种特殊情况的答案,我正在分享我的发现。当请求被适当地未经授权时,它由

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)

方法。在该方法中,我首先检查用户是否未通过身份验证。对于第一个请求,它可以正常工作,将用户发送到适当的登录页面,但是现在如果您尝试导航到需要不同角色的另一个页面,因为您已经过身份验证,它不会进入重定向并允许访问控制。因此,通过删除开头的 !IsAuthenticated if,现在所有未经授权的请求都会正确发送到正确的登录页面...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多