我正在为一个外部工具开发一个webhook接收器。
我正在使用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登录页面。我甚至不知道它是从哪里来的。
我现在的解决方案就是返回“BadRequest”和消息“Authorization failed”
发布于 2018-08-07 03:37:50
好的,明白了。
这是非常正常的。
https://www.restapitutorial.com/httpstatuscodes.html
请求需要用户身份验证。响应必须包括WWW-Authenticate报头字段(第14.47节),其中包含适用于所请求资源的质询。
在我的例子中,我应该使用403禁止
https://stackoverflow.com/questions/51713910
复制相似问题