更新时间:2023-12-01 15:22:46
将DelegatingHandler
添加到您的Web api项目并在WebApiConfig.cs中注册它:
Add a DelegatingHandler
to your web api project and register it in WebApiConfig.cs:
config.MessageHandlers.Add(new UnAuthorizedDelegatehandler());
public class UnAuthorizedDelegatehandler: DelegatingHandler
您可以在其中检查401状态代码,并执行重定向到任何操作,还可以将重定向URL用作querystring参数.
There you can check for 401 status codes and do the redirect to whatever and also apply a redirect url as querystring parameter.
HttpResponseMessage rm = await base.SendAsync(request, cancellationToken);
if (rm.StatusCode == HttpStatusCode.Unauthorized)
{
// configure the redirect here
return rm;
}