11

Request.ClientCertificate を使用して X509 証明書を読み取ろうとしていますが、何も返されません。リクエストを送信するページから証明書情報を取得できるため、証明書は間違いなくリクエストに添付されています。

いくつかの異なる場所から証明書を読み取ろうとしましたが、機能しないようです。

この KB Articleのコードから始めました。要求されたページで、証明書に関する情報を印刷しようとしましたが、応答で何も返されませんでした。

これは IIS 5.1 で実行されており、通信は SSL 経由です。これは、.Net フレームワークのバージョン 2 を使用して行う必要があります。

証明書が消えているように見えるのはなぜですか?

4

4 に答える 4

12

しばらく前に、クライアント証明書を探し、見つかった場合は証明書情報を表示するIDWebページを作成しました。私はそれがあなたが探しているものだと信じています...ここにページがあります:

<%@ Page Language="C#" Trace="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System.Security.Cryptography.X509Certificates" %>
<%@ Import Namespace="System.Security.Cryptography" %>

<script runat="server">
    //protected void Page_Load(object sender, EventArgs e)
    //{ }

    void LoadCertInfo()
    {
        string para = "<div style='margin: 10px 0 0 0; font-weight: bold'>{0}</div>";
        string subpara = "<div style='margin-left: 15px; font-size: 90%'>{0}</div>";

        if (Page.Request.ClientCertificate.IsPresent)
        {
            Response.Write("<hr /><div style='width: 500px; margin: 20px auto'>");
            Response.Write("<h3 style='width: 500px; margin: 20px auto'>Client Certificate Information</h3>");
            try
            {
                X509Certificate2 x509Cert2 = new X509Certificate2(Page.Request.ClientCertificate.Certificate);

                Response.Write(string.Format(para, "Issued To:"));
                Response.Write(string.Format(subpara, x509Cert2.Subject));

                Response.Write(string.Format(para, "Issued By:"));
                Response.Write(string.Format(subpara, x509Cert2.Issuer));

                Response.Write(string.Format(para, "Friendly Name:"));
                Response.Write(string.Format(subpara, string.IsNullOrEmpty(x509Cert2.FriendlyName) ? "(None Specified)" : x509Cert2.FriendlyName));

                Response.Write(string.Format(para, "Valid Dates:"));
                Response.Write(string.Format(subpara, "From: " + x509Cert2.GetEffectiveDateString()));
                Response.Write(string.Format(subpara, "To: " + x509Cert2.GetExpirationDateString()));

                Response.Write(string.Format(para, "Thumbprint:"));
                Response.Write(string.Format(subpara, x509Cert2.Thumbprint));

                //Response.Write(string.Format(para, "Public Key:"));
                //Response.Write(string.Format(subpara, x509Cert2.GetPublicKeyString()));

                #region EKU Section - Retrieve EKU info and write out each OID
                X509EnhancedKeyUsageExtension ekuExtension = (X509EnhancedKeyUsageExtension)x509Cert2.Extensions["Enhanced Key Usage"];
                if (ekuExtension != null)
                {
                    Response.Write(string.Format(para, "Enhanced Key Usages (" + ekuExtension.EnhancedKeyUsages.Count.ToString() + " found)"));

                    OidCollection ekuOids = ekuExtension.EnhancedKeyUsages;
                    foreach (Oid ekuOid in ekuOids)
                        Response.Write(string.Format(subpara, ekuOid.FriendlyName + " (OID: " + ekuOid.Value + ")"));
                }
                else
                {
                    Response.Write(string.Format(para, "No EKU Section Data"));
                }
                #endregion // EKU Section

                #region Subject Alternative Name Section
                X509Extension sanExtension = (X509Extension)x509Cert2.Extensions["Subject Alternative Name"];
                if (sanExtension != null)
                {
                    Response.Write(string.Format(para, "Subject Alternative Name:"));
                    Response.Write(string.Format(subpara, sanExtension.Format(true)));
                }
                else
                {
                    Response.Write(string.Format(para, "No Subject Alternative Name Data"));
                }

                #endregion // Subject Alternative Name Section

                #region Certificate Policies Section
                X509Extension policyExtension = (X509Extension)x509Cert2.Extensions["Certificate Policies"];
                if (policyExtension != null)
                        {
                            Response.Write(string.Format(para, "Certificate Policies:"));
                            Response.Write(string.Format(subpara, policyExtension.Format(true)));
                        }
                        else
                        {
                            Response.Write(string.Format(para, "No Certificate Policies Data"));
                        }
                #endregion //Certificate Policies Section


                // Example on how to enumerate all extensions
                //foreach (X509Extension extension in x509Cert2.Extensions)
                //    Response.Write(string.Format(para, extension.Oid.FriendlyName + "(" + extension.Oid.Value + ")"));
            }
            catch (Exception ex)
            {
                Response.Write(string.Format(para, "An error occured:"));
                Response.Write(string.Format(subpara, ex.Message));
                Response.Write(string.Format(subpara, ex.StackTrace));
            }
            finally
            {
                Response.Write("</div>");
            }
        }
    }
</script>
<html>
  <head runat="server">
    <title><% Page.Response.Write(System.Environment.MachineName); %></title>
  </head>
  <body>
      <% LoadCertInfo();  %>
  </body>
</html>
于 2010-09-04T05:57:19.573 に答える
2

完全に明確ではありませんが、クライアントが証明書を使用して自分自身を認証する必要がある Web サイトをお持ちですか? それがRequest.ClientCertificateプロパティの目的だからです。

あなたの質問には何か奇妙な点があるので、私はこれを言います。

「リクエストを送信したページから証明書情報を取得できます。」

一般に、ページはクライアントが行うリクエストを実際には送信しません。

サーバー証明書を取得するには、X509Store を開いて証明書をふるいにかけ、必要な CN を持つ証明書を見つけます。

于 2009-06-08T08:47:56.390 に答える
2

クライアント証明書が何のために必要なのかわかりませんが、それを独自のカスタム認証または承認に使用している場合は、独自のセキュリティ インフラストラクチャを実装する代わりに、Web サーバーのセキュリティ インフラストラクチャを使用することを検討してください。たとえば、クライアント証明書を要求し、証明書をユーザー アカウントにマップし、Windows ベースの認証を使用するように IIS を構成できます。もちろん、これは問題のドメインで必ずしも機能するとは限りません。

于 2009-06-11T01:07:13.103 に答える
1

クライアント証明書を受け入れる (または要求する) ようにローカル IIS を構成する必要があります。

于 2019-03-14T14:45:48.660 に答える