在 Asp.Net Web API 中创建一个消息处理程序,您可以在其中删除 WWW-Authenticate 标头,这样浏览器就不会弹出窗口。您可以在此级别进行许多自定义。
要创建您自己的消息处理程序,您需要扩展DelegatingHandler
以下内容
public class MyHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
//Your code goes here
var response = await base.SendAsync(request,cancellationToken);
if(response.StatusCode == HttpStatusCode.Unauthorized)
{
// remove WWW-Authenticate from the header and add Location header to redirect to a page
OR
// Create a new response and then return that response
}
return response;
}
}