我使用Owin
自己托管Web应用程序。如何通过智能卡进行身份验证作为httpClint/httpListener中的Windows身份验证的回退
在服务器中定义的身份验证方案如下:
HttpListener listener = (HttpListener)app.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
我的客户端的代码如下所示:
using (var webRequestHandler = new WebRequestHandler {UseDefaultCredentials = true})
using (var httpClient = new HttpClient(webRequestHandler))
{
var responseCode = httpClient.PostAsync("https://server:443/myapi/dosomething/", null).Result.StatusCode;
Console.WriteLine(responseCode == HttpStatusCode.OK ? "Success" : "Failure");
}
这个伟大的工程在客户端的机器上时,登录的用户是用户已知的服务器的机器。
例如,当客户端的机器未加入域并且客户端由本地用户运行时,就会出现问题。在这种情况下,我已经延长我的客户了一下,如下:
HttpStatusCode responseCode;
using (var webRequestHandler = new WebRequestHandler {UseDefaultCredentials = true})
using (var httpClient = new HttpClient(webRequestHandler))
{
responseCode = httpClient.PostAsync("https://server:443/myapi/dosomething/", null).Result.StatusCode;
}
if (responseCode == HttpStatusCode.Unauthorized)
{
string username;
string password;
// prompt user for credentials and store them at the above variables
using (var webRequestHandler = new WebRequestHandler { Credentials = new NetworkCredential(username, password)})
using (var httpClient = new HttpClient(webRequestHandler))
{
responseCode = httpClient.PostAsync("https://server:443/myapi/dosomething/", null).Result.StatusCode;
}
}
Console.WriteLine(responseCode == HttpStatusCode.OK ? "Success" : "Failure");
这解决了它,但我仅限于只执行使用用户名/密码的备用身份验证。
我的问题是我需要支持智能卡身份验证以及用户名/密码身份验证。