1

スマートカードをリーダーに挿入すると、証明書がパーソナルストアに読み込まれます。私の質問は、実行時に特定の(これらの)証明書を使用する必要があることをWCFに説明する方法です。

私のWCFサービスは自己ホスト型であり、TCPを介してクライアントと通信しています。

証明書を使用する通信はすでにありますが、これらの証明書は構成ファイルに記載されています(1つはサービス用、もう1つはクライアント用)。

次に、通信する前にクライアント側で証明書を切り替える必要があります。

4

1 に答える 1

1

使用する証明書に固有の特定の属性セットを見つけるようにしてください。たとえば、次のようなフィルタリングを使用します。

    /// <summary>
    /// Get the certificate we need for authentication
    /// </summary>
    /// <returns>the certificate</returns>
    /// <exception cref="InvalidOperationException">when no certificate is available</exception>
    public static X509Certificate2 GetCertificate()
    {
        // open private certificate store of the user
        X509Store store = new X509Store(StoreName.My);
        store.Open(OpenFlags.ReadOnly);

        // get the collection of certificates which we need
        X509Certificate2Collection certColl = store.Certificates
            .Find(X509FindType.FindByExtension, new X509KeyUsageExtension().Oid.Value, false)
            .Find(X509FindType.FindByKeyUsage, X509KeyUsageFlags.NonRepudiation, false);

        store.Close();
        // if more certificates match the criteria, let the user choose one
        if (certColl.Count > 1)
        {
            certColl = X509Certificate2UI.SelectFromCollection(
                certColl, "Choose certificate", "We were unable to find the correct certificate. Please choose one from the list.",
                X509SelectionFlag.SingleSelection);
        }
        // if no certificate is available, fail
        if (certColl.Count < 1)
        {
            throw new InvalidOperationException("No certificates selected");
        }
        else
        {
            return certColl[0];
        }
    }

必要な証明書の属性を確認し、独自の検索基準を作成してみてください。もちろん、使用には、基準に一致する追加の証明書がストアにある可能性があります。この場合、ダイアログウィンドウがポップアップし、ユーザーにこれを自分で行うように求めます。

もちろん、ユーザーが正しい証明書を選択すると、証明書のCNをアプリケーション構成ファイルに保存できるため、次回はこれらの手順を再度実行する必要がなくなります。

于 2013-03-22T13:18:24.267 に答える