public class AuthenticationFilter : IAsyncAuthorizationFilter
{
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
{
var authToken = context.HttpContext.Request.Headers.ContainsKey("Authorization");
if (!authToken)
{
context.Result = new UnauthorizedResult();
}
}
}
此过滤器在每个控制器之前被调用,我想跳过此过滤器进行登录并注册用户控制器。如何在Asp.net Core中执行此操作,我尝试添加AllowAnonymous装饰器,但该方法无效。
在startup.cs
services.Configure<MvcOptions>(options =>
{
//TODO: remove in production.
options.Filters.Add(new CorsAuthorizationFilterFactory("AllowSpecificOrigin"));
options.Filters.Add(new AuthenticationFilter());
});