C# POST HTTPS 的问题,在代码中怎么知道客户端证书已经安装?

chenzhenhe 2006-12-22 04:12:58

代码如下:


using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using System.Web.Services;

using System.Security.Cryptography.X509Certificates;
using System.Security.Policy;
using Microsoft.Web.Services3.Security.Tokens ;

public static XmlDocument GetResponseXmlSSLByThumbprint(string request, Uri destination, string thumbprint)
{

//test
if (thumbprint == null)
{
thumbprint = "b3 ea c4 47 76 c9 c8 1c ea f2 9d 95 b6 cc a0 08 1b 67 ec 9d".Replace(" ",string.Empty); ;
}

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);

X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);

X509Certificate2 clientcert = null ;
if (certs.Count == 1)
{
clientcert = certs[0];
}
else
{
//what i can do ....?? sos ....
}


HttpWebRequest webRequest = (HttpWebRequest) System.Net .WebRequest.Create(destination);
webRequest.KeepAlive = true;

webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = RequestMethod;

webRequest.ClientCertificates.Add(clientcert);

return GetResponseXml(webRequest, request);

}

private static XmlDocument GetResponseXml(HttpWebRequest webRequest, string request)
{
byte[] requestBytes = Encoding.ASCII.GetBytes(request);
webRequest.ContentLength = requestBytes.Length;

// Write the request
Stream reqStream = webRequest.GetRequestStream();
reqStream.Write(requestBytes, 0, requestBytes.Length);
reqStream.Close();
// Get a response
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

if (webRequest.HaveResponse)
{
// Read response
StreamReader oStream = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.ASCII);

//Create an XMLTextReader and parse the response into a DOM
XmlTextReader Reader = new XmlTextReader(new StringReader(oStream.ReadToEnd()));
XmlDocument responseDoc = new XmlDocument();

responseDoc.Load(Reader);

oStream.Close();
webResponse.Close();

return responseDoc;

}
return null;
}

//////////////////////////

如果执行代码时候证书还没有安装,怎么办,如何通过代码去安装?就是下面这段:


if (certs.Count == 1)
{
clientcert = certs[0];
}
else
{
//what i can do ....?? sos ....
}

...全文
433 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
DADA_dotnet 2006-12-25
  • 打赏
  • 举报
回复
不知道哦~我也想知道~!!学习~帮顶!!!
chenzhenhe 2006-12-25
  • 打赏
  • 举报
回复
没有人知道吗

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧