Custom authorization attribute not working in WebAPI

前端 未结 4 661
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 12:33
 public class CustomAuthorizeAttribute : AuthorizationFilterAttribute
 {  
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
       return         


        
4条回答
  •  忘掉有多难
    2021-01-01 13:24

    To add onto the other answers that have you inherit from System.Web.Http.Filters.AuthorizationFilterAttribute, I put this into my OnAuthorization method to make sure the user was logged in:

    if (!actionContext.RequestContext.Principal.Identity.IsAuthenticated)
    {
         // or whatever sort you want to do to end the execution of the request
         throw new HttpException(403, "Forbidden");
    } 
    

提交回复
热议问题