Custom [Authorize] message on 401

前端 未结 1 617
死守一世寂寞
死守一世寂寞 2021-01-20 02:01

I need to deliver custom \"not authorized\" message in different languages, instead of the default \"Authorization has been denied for this request\". I\'m using Owin as aut

1条回答
  •  有刺的猬
    2021-01-20 02:40

    Unfortunately It is not possible.

    Based on the source code, one option would be to create a new custom Authorize attribute, inheriting from AuthorizeAttribute. You then would only need to override the HandleUnauthorizedRequest method, and replace with your own. The rest of the methods would be still handled by the base AuthorizeAttribute

      public class MyCustomAuthAttribute : AuthorizeAttribute
      {
          protected virtual void HandleUnauthorizedRequest(HttpActionContext actionContext)
            {
                if (actionContext == null)
                {
                    throw Error.ArgumentNull("actionContext");
                }
    
                    actionContext.Response =
                    actionContext.ControllerContext.Request.CreateErrorResponse(
                                          HttpStatusCode.Unauthorized, 
                                          "My Un-Authorized Message");
          }
        } 
    

    0 讨论(0)
提交回复
热议问题