发帖到Stack Overflow后,你总是能找到解决方案。
https://github.com/restsharp/RestSharp/issues/414
不要在RestClient上使用IAuthenticator,而是要构建一个自定义的System.Net.IAuthenticationModule。这是我的解决方案:
我的RestSharp认证器
public class MyAuthenticator : IAuthenticator
{
private readonly CredentialCache _credentials = new CredentialCache();
public MyAuthenticator(Uri loginServerUrl, string username, string password)
{
if (loginServerUrl == null)
{
throw new ArgumentNullException("loginServerUrl");
}
__registerAuthenticationModule(loginServerUrl);
_credentials.Add(loginServerUrl, MyAuthenticationModule.TheAuthenticationType, new NetworkCredential(username, password, loginServerUrl.Host));
}
private static MyAuthenticationModule __registerAuthenticationModule(Uri loginServerUrl)
{
IEnumerator registeredModules = AuthenticationManager.RegisteredModules;
MyAuthenticationModule authenticationModule;
while (registeredModules.MoveNext())
{
object current = registeredModules.Current;
if (current is MyAuthenticationModule)
{
authenticationModule = (MyAuthenticationModule)current;
if (authenticationModule.LoginServerUrl.Equals(loginServerUrl))
{
return authenticationModule;
}
}
}
authenticationModule = new MyAuthenticationModule(loginServerUrl);
AuthenticationManager.Register(authenticationModule);
return authenticationModule;
}
public void Authenticate(IRestClient client, IRestRequest request)
{
request.Credentials = _credentials;
}
}
My .NET Authentication Module
public class MyAuthenticationModule : IAuthenticationModule
{
internal const string TheAuthenticationType = "MyAuthentication";
private readonly CredentialCache _credentialCache = new CredentialCache();
private readonly Uri _loginServerUrl;
internal CredentialCache CredentialCache
{
get
{
return _credentialCache;
}
}
internal Uri LoginServerUrl
{
get
{
return _loginServerUrl;
}
}
internal MyAuthenticationModule(Uri loginServerurl)
{
if (loginServerurl == null)
{
throw new ArgumentNullException("loginServerUrl");
}
_loginServerUrl = loginServerurl;
}
public Authorization Authenticate(string challenge, WebRequest request, ICredentials credentials)
{
Authorization result = null;
if (request == null || credentials == null)
{
result = null;
}
else
{
NetworkCredential creds = credentials.GetCredential(LoginServerUrl, TheAuthenticationType);
if (creds == null)
{
return null;
}
ICredentialPolicy policy = AuthenticationManager.CredentialPolicy;
if (policy != null && !policy.ShouldSendCredential(LoginServerUrl, request, creds, this))
{
return null;
}
string token = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", creds.UserName, creds.Password)));
result = new Authorization(string.Format("Basic {0}", token));
}
return result;
}
public string AuthenticationType
{
get { return TheAuthenticationType; }
}
public bool CanPreAuthenticate
{
get { return false; }
}
public Authorization PreAuthenticate(WebRequest request, ICredentials credentials)
{
return null;
}
}
像这样将其添加到 RestSharp 客户端
var client = new RestClient(commonApiUrl)
{
Authenticator = new MyAuthenticator(loginServerUrl, username, password)
};