62,046
社区成员
发帖
与我相关
我的任务
分享
public string Sys_Public_CertificateInfo()
{
string functionReturnValue = null;
string str = "";
System.Security.Cryptography.SHA256Managed tmp = new System.Security.Cryptography.SHA256Managed();
str = ServiceSecurityContext.Current.PrimaryIdentity.Name;//这句报错,未将对象引用设置到对象实例
//str = ServiceSecurityContext.Current.AuthorizationPolicies.Count.ToString();
str = str.Substring(str.Length - 40, 40).ToUpper();
functionReturnValue = BitConverter.ToString(tmp.ComputeHash(System.Text.UnicodeEncoding.Unicode.GetBytes(str)));
if (functionReturnValue != null)
{
functionReturnValue = functionReturnValue.Replace("-", "");
}
else
{
functionReturnValue = "";
}
tmp.Clear();
tmp = null;
return functionReturnValue;
}
获取证书信息OK
if (Request.IsSecureConnection)
{
HttpClientCertificate hCert = Request.ClientCertificate;
if (hCert == null)
{
Response.Write("没有客户端证书");
return;
}
X509Certificate2 cert = new X509Certificate2();
try
{
cert.Import(hCert.Certificate);
str = cert.Thumbprint;
sub = cert.Subject;
after = cert.NotAfter.ToShortDateString();
}
catch (Exception ex)
{
Response.Write(ex.Message);
return;
}
}
DateTime da = DateTime.Parse(after);
DateTime dt = DateTime.Now;
if (Common.common.md5(str.ToUpper(), 32) != ds3.Tables[0].Rows[0]["certificateinfo"].ToString())
{
err = "证书错误!";
flag = false;
}
if (dt > da)
{
err = "证书过期!";
flag = false;
}
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
//轮询存储区中的所有证书
string str="";
string sub="";
string after="";
bool flag = true;
foreach (X509Certificate2 myX509Certificate2 in store.Certificates)
{
//将证书的名称跟要导出的证书MyTestCert比较,找到要导出的证书
str = myX509Certificate2.Thumbprint;
sub = myX509Certificate2.Subject.Split(',')[0].ToString().Split('=')[1].ToString();
after = myX509Certificate2.NotAfter.ToString();
if (sub == ds3.Tables[0].Rows[0]["certificatinfo_name"].ToString())
{
if (Common.common.md5(str.ToUpper(), 32) != ds3.Tables[0].Rows[0]["certificateinfo"].ToString())
{
flag = false;
break;
}
}
}
store.Close();