Is it possible to disable authentication Filter on one action in an MVC 5 controller?

Is it possible to disable authentication Filter on one action in an MVC 5 controller?

Since it is your custom filter, you can extend it to handle AllowAnonymous (if you don't want to use AllowAnonymous, yoy can create own f.e. NoAuthentication):

public class AuthenticateUser : IAuthenticationFilter
{

    public void OnAuthentication(AuthenticationContext filterContext)
    { 
        if (this.IsAnonymousAction(filterContext))
        {
            return;
        }

        // some code
    }

    private bool IsAnonymousAction(AuthenticationContext filterContext)
    {
        return  filterContext.ActionDescriptor
                             .GetCustomAttributes(inherit: true)
                             .OfType<AllowAnonymousAttribute>() 
                                             //or any attr. you want
                             .Any();
    }
}

AllowAnonymousAttribute Class

[AllowAnonymous] not working for Authentication filter disable... It is for Authorization Filter

Specifies that the class or method that this attribute is applied to does not require authorization.


 

原文地址:https://www.cnblogs.com/chucklu/p/14442652.html