我正在尝试在 C# 中实现一个客户端库以与 tomcat 服务器进行通信。身份验证应通过在 Windows 客户端的相互 SSL 身份验证中使用带有 X.509 证书的飞天 epass2003 令牌来完成。
但是,每次我运行客户端窗口时,我都会遇到糟糕的情况,因为它会请求令牌密码以继续操作,这对用户来说是一种过度杀伤并且不可接受的操作。
我想知道是否可以取消此请求并以某种方式将其添加到代码中。或者,如果有其他方法可以在不使用 Windows 证书管理器的情况下使用令牌。
这就是我扩展连接器的方式:
class WebClientEx : WebClient
{
public int Timeout { get; set; }
public X509Certificate certificate { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.Credentials = CredentialCache.DefaultCredentials;
request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
request.ClientCertificates.Add(this.certificate);
request.Timeout = this.Timeout;
return request;
}
...
}
这是我如何连接到服务器的示例:
byte[] certificate = getCertificate("autenticacao"); // Get certificate from token
X509Certificate cert = new X509Certificate(certificate);
var post = new NameValueCollection();
post["test"] = "1";
using (var wb = new WebClientEx(cert))
{
try
{
wb.Timeout = 30000;
var response = wb.UploadValues("https://localhost:8443", "POST", post);
Console.WriteLine("Done.");
}
catch (WebException e)
{
// Handle WebException
}
}
这是 Windows 请求屏幕:
此致,