c# - 自定义 Api 授权忽略 AllowAnonymous

标签 c# asp.net-mvc custom-attributes

我有一个 CustomApiAuthorizeAttribute:

public class CustomApiAuthorizeAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(HttpActionContext actionContext)
    {
        if (actionContext == null)
            throw new ArgumentNullException("actionContext");

        bool skipAuthorization = actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || 
            actionContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any();

        if (skipAuthorization) return;

        var cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];

        if (cookie != null)
        {
            var decCookie = FormsAuthentication.Decrypt(cookie.Value);

            if (decCookie != null)
            {
                if (!string.IsNullOrEmpty(decCookie.UserData))
                {
                    HttpContext.Current.User = new CustomPrinciple(new CustomIdentity(decCookie));
                    return;
                }
            }
        }

        HttpContext.Current.Items["RequestWasNotAuthorized"] = true;

        HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName) { Expires = DateTime.Now.AddDays(-1d) });

        HandleUnauthorizedRequest(actionContext);
    }
}

我有一个 Controller :

[CustomApiAuthorize]
public class RacingController : CustomApiController
{
    [HttpGet]
    [AllowAnonymous]
    public Venues Venues()
    {
        var asr = Services.GetVenues(Token);
        if(!string.IsNullOrEmpty(Token))
            SetAuthTicket(asr.Token);
        return asr.Payload;
    }
 }

尝试调用此操作时,我不断收到 401 未经授权的错误。调试告诉我 authorizeattribute 没有检测到 [AllowAnonymous] 的存在,但我不明白为什么。

有人能看出我做错了什么吗?或者知道是否有其他事情可能会发生冲突?

最佳答案

如果您查看 System.Web.Http.AuthorizeAttribute 的源代码,会进行以下检查以查看是否应跳过授权:

public override void OnAuthorization(HttpActionContext actionContext)
    {
        if (actionContext == null)
        {
            throw Error.ArgumentNull("actionContext");
        }

        if (SkipAuthorization(actionContext))
        {
            return;
        }

        if (!IsAuthorized(actionContext))
        {
            HandleUnauthorizedRequest(actionContext);
        }
    }

        private static bool SkipAuthorization(HttpActionContext actionContext)
    {
        Contract.Assert(actionContext != null);

        return actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()
               || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any();
    }

所以我所做的是在我的自定义授权属性中实现类似的检查。

关于c# - 自定义 Api 授权忽略 AllowAnonymous,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19671411/

相关文章:

symfony1 - 为 sfDoctrineGuard 插件的 sfGuardUser 模型添加 avatar 字段

c# - 如何枚举具有自定义类属性的所有类?

c# - DataAnnotations 属性是否已缓存?如果是这样,如何在不同文化之间切换?

c# - WPF 使用 CollectionViewSource 隐藏重复项

javascript - 如何从 Controller 创建进度条来处理 MVC3 中的长流程

asp.net-mvc - 如何使用 Ninject 将 ModelState 作为参数注入(inject)?

c# - 如何将 SignalR 数据源与 Kendo 网格一起使用

c# - 从 Mac 上的 Rider 附加到 Azure Functions 的调试器

c# - 如何对枚举值进行分组?

c# - 从 ListView 中拖动文件