如何从Web服务中获取客户端发送的X509Certificate?

Dav*_*N59 10 c# web-services x509certificate2

显然我在之前的帖子中提出了错误的问题.我有一个使用X.509证书保护的Web服务,作为安全的网站运行(https:// ..).我想使用公司的根CA颁发的客户端机器证书(也是X.509)来验证客户端机器是否有权使用该服务.为了做到这一点,我需要检查证书并寻找一些识别功能,并将其与存储在数据库中的值(可能是指纹?)相匹配.

这是我用来从本地证书商店获取证书的代码(直接从http://msdn.microsoft.com/en-us/magazine/cc163454.aspx获取):

 public static class SecurityCertificate
{
    private static X509Certificate2 _certificate = null;

    public static X509Certificate2 Certificate
    {
        get { return _certificate; }
    }

    public static bool LoadCertificate()
    {
        // get thumbprint from app.config
        string thumbPrint = Properties.Settings.Default.Thumbprint;
        if ( string.IsNullOrEmpty( thumbPrint ) )
        {
            // if no thumbprint on file, user must select certificate to use
            _certificate = PickCertificate( StoreLocation.LocalMachine, StoreName.My );
            if ( null != _certificate )
            {
                // show certificate details dialog
                X509Certificate2UI.DisplayCertificate( _certificate );
                Properties.Settings.Default.Thumbprint = _certificate.Thumbprint;
                Properties.Settings.Default.Save();
            }
        }
        else
        {
            _certificate = FindCertificate( StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, thumbPrint );
        }

        if ( null == _certificate )
        {
            MessageBox.Show( "You must have a valid machine certificate to use STS." );
            return false;
        }

        return true;
    }

    private static X509Certificate2 PickCertificate( StoreLocation location, StoreName name )
    {
        X509Store store = new X509Store( name, location );
        try
        {
            // create and open store for read-only access
            store.Open( OpenFlags.ReadOnly );

            X509Certificate2Collection coll = store.Certificates.Find( X509FindType.FindByIssuerName, STSClientConstants.NBCCA, true );
            if ( 0 == coll.Count )
            {
                MessageBox.Show( "No valid machine certificate found - please contact tech support." );
                return null;
            }

            // pick a certificate from the store
            coll = null;
            while ( null == coll || 0 == coll.Count )
            {
                coll = X509Certificate2UI.SelectFromCollection(
                        store.Certificates, "Local Machine Certificates",
                        "Select one", X509SelectionFlag.SingleSelection );
            }

            // return first certificate found
            return coll[ 0 ];
        }
        // always close the store
        finally { store.Close(); }
    }

    private static X509Certificate2 FindCertificate( StoreLocation location, StoreName name, X509FindType findType, string findValue )
    {
        X509Store store = new X509Store( name, location );
        try
        {
            // create and open store for read-only access
            store.Open( OpenFlags.ReadOnly );

            // search store
            X509Certificate2Collection col = store.Certificates.Find( findType, findValue, true );

            // return first certificate found
            return col[ 0 ];
        }
        // always close the store
        finally { store.Close(); }
    }
Run Code Online (Sandbox Code Playgroud)

然后,我将证书附加到出站流:

public static class ServiceDataAccess
{    
    private static STSWebService _stsWebService = new STSWebService();

    public static DataSet GetData(Dictionary<string,string> DictParam, string action)
    {
        // add the machine certificate here, the first web service call made by the program (only called once)
        _stsWebService.ClientCertificates.Add( SecurityCertificate.Certificate );
        // rest of web service call here...
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是 - 如何在Web服务代码中"获取"证书?我遇到的大多数示例代码片段都涵盖了如何进行自定义验证,其中有一个GetCertificate()调用,显然假设部件很容易,每个人都应该知道如何做到这一点?

我的主类继承自WebService,因此我可以使用Context.Request.ClientCertificate来获取证书,但这是一个HttpClientCertificate,而不是X509Certificate2.HttpContext给了我相同的结果.其他方法都使用Web配置代码来调用预定义的验证代码,而不知道如何调用自定义C#方法来进行验证.

DSO*_*DSO 13

我记得做了类似的事情,它已经有一段时间但是,你在网络服务中试过这个:

X509Certificate2 cert = new X509Certificate2(Context.Request.ClientCertificate.Certificate);
Run Code Online (Sandbox Code Playgroud)