Getting the certificate info from the request on R

2019-09-17 13:52发布

问题:

Hi I am having a Restful service (DotNet 4.0 WCF VS 2012) in HTTPS. My client will attach a certificate to it (certificate is given by me (.cer file)) I need to get the certificate back from the request and read its information to authenticate it, The serial Number, Thumprint are stored in DB when I need to check the same.

I did the SSL and Share the cer file to the client.

I used the following code to read back my certificate

C# code start

if (OperationContext.Current.ServiceSecurityContext.AuthorizationContext.ClaimSets == null) throw new Exception ("No claimset service configured wrong");

        if (OperationContext.Current.ServiceSecurityContext.AuthorizationContext.ClaimSets.Count <= 0)
                 throw new Exception  ("No claimset service configured wrong");



       var cert = ((X509CertificateClaimSet)OperationContext.Current.ServiceSecurityContext.
       AuthorizationContext.ClaimSets[0]).X509Certificate;

C# code end

in the above code i always gets claimSets.Count = 0.

Is any setting I need to do in my server web.config, I did the following setting in my Server Side web.config

'security mode="Transport"' 'transport clientCredentialType="None"' 'security'

Please let me know Is I am missing any settings in the client side or the server side.

In the client side I am using following code the add the cer to the request

C# Code Start

X509Certificate2 cert = new X509Certificate2 ("C:\\xxxxxx.cer");

            System.Net.ServicePointManager.ServerCertificateValidationCallback = 
            delegate(Object obj, X509Certificate X509certificate, X509Chain chain, System.Net.Security.SslPolicyErrors errors)
            {
                return true;
            };


            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(xxxxx.Text.Trim());
            webRequest.ClientCertificates.Add(cert);

C# Code End

I did not have any special setting in my client web.config file.

回答1:

Why you use clientCredentialType="None" and not clientCredentialType="Certificate"?

It is also possible your client does not send any certificate. Try to enable Network Tracing in App.config on the client - instructions here. That should create network.log with more debug info. Look for SecureChannel entries in log.