AllowAnonymous not working with Custom AuthorizationAttribute

后端 未结 8 651
一整个雨季
一整个雨季 2020-12-03 06:34

This has had me stumped for a while. None of the commonly encountered similar situations seem to apply here apparently. I\'ve probably missed something obvious but I can\'

8条回答
  •  星月不相逢
    2020-12-03 06:50

    Here is a solution for ASP.NET Core 2+ and ASP.NET Core 3+. Add it into IAsyncAuthorizationFilter implementation:

    private static bool HasAllowAnonymous(AuthorizationFilterContext context)
    {
        var filters = context.Filters;
        return filters.OfType().Any();
    }
    

    And check like this:

    public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
    {
        if(HasAllowAnonymous(context))
            return;
    }
    

提交回复
热议问题