我们有20个微服务都在使用客户端库。
这个库只是为微服务提供了一种连接到目的地的方法:CRM Dynamics。
在每个服务中,我们都有一个调用此库的包装器:
public static class Client
{
public static HttpClient Instance;
static Client()
{
Instance = new TheCrmClient<AdfsAuthenticationHelper>(MyConfigSettings).HttpClient;
}
}
在每项服务中,我们都像这样使用客户端:
//Issue request
var response = Client.Instance.SendAsync(request).Result;
ADFS会话超时后,我们将收到401响应。
我们如何在收到401响应时强制重新验证?
认证发生在GetHttpsClient()中:
public class TheCrmClient<T> where T : IAdfsAuthenticator
{
private readonly RestCRMClientConfiguration _configuration;
private IAdfsAuthenticator _authenticator;
private HttpClient _https;
public TheCrmClient(RestCRMClientConfiguration configuration)
{
_configuration = configuration;
}
public HttpClient HttpClient => GetClientHttps();
private void InitializeAuthenticator(HttpClient client)
{
_authenticator = Activator.CreateInstance(typeof(T), client) as IAdfsAuthenticator;
}
private HttpClient GetClientHttps()
{
var clientHandler = new HttpClientHandler
{
UseCookies = true,
CookieContainer = new CookieContainer(),
AllowAutoRedirect = false
};
_https = new HttpClient(clientHandler)
{
BaseAddress = _configuration.GetServiceUri(RestCRMClientConfiguration.UrlProtocolType.Https)
};
if (_authenticator == null)
InitializeAuthenticator(_https);
_authenticator.Authenticate(_configuration.GetNetworkCredential(),
_configuration.GetAuthenticationTestEndpointUrl(RestCRMClientConfiguration.UrlProtocolType.Https));
return _https;
}
}
澄清请求的生命周期是:
Microservice.Controller - &gt; Microservice.ClientWrapper - &gt; TheCrmClientLibrary - &gt; CrmDestination