且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将未经授权的请求重定向到Azure AD进行登录

更新时间: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;                        
}