代码之家  ›  专栏  ›  技术社区  ›  Jurion

ASP.NET WebHookReceiver“return HttpStatusCode.Unauthorized”改为返回HTML

  •  0
  • Jurion  · 技术社区  · 6 年前

    我正在开发一个外部工具的webhook接收器。

    Im使用ASP.NET WebHooks框架(更多: https://docs.microsoft.com/en-us/aspnet/webhooks/ )

    一切正常,除了一件事:我不能返回HttpStatusCode.Unauthorized。

    这是WebHookReceiver中的代码结构:

    public override async Task<HttpResponseMessage> ReceiveAsync(string id, HttpRequestContext context, HttpRequestMessage request)
            {
                try
                {
                    var authheader = request.Headers.Authorization;
                    if (authheader == null || string.IsNullOrEmpty(authheader.Parameter))
                    {
                        return new HttpResponseMessage(HttpStatusCode.Unauthorized);
                    }
    

    我希望得到一个简单的HTTP响应,代码为HttpStatusCode.Unauthorized(附带说明,任何其他HTTP代码都可以正常工作)。相反,它返回整个HTML Microsoft登录页。我甚至不知道它是从哪里来的。

    The HTML page returned

    目前,我的解决方案是返回“BadRequest”和消息“Authorization failed”

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jurion    6 年前

    好的,明白了。

    这很正常。

    https://www.restapitutorial.com/httpstatuscodes.html

    请求需要用户身份验证。响应必须包含 WWW Authenticate头字段(第14.47节)包含质询 适用于请求的资源。

    对于我的情况,我应该使用403禁止