连接到OData服务时,通过代码附加客户端证书

时间:2014-04-06 12:57:33

标签: c# ssl client ssl-certificate odata

我试图查询IIS7上托管的odata web.api。该站点需要客户端证书。如何将证书附加到查询?使用web.api 2,framework 4.5,mvc5

string certPath = @"E:\ClientCertificate.cer";

Uri uri = new Uri("https://server/odata/"); 
var container = new CourseService.Container(uri);
container.ClientCertificate = new X509Certificate(certPath);

通过阅读以下内容实现了容器类的扩展: http://bartwullems.blogspot.co.uk/2013/03/odata-attach-client-certificate-through.htm

1 个答案:

答案 0 :(得分:0)

您可以自己附加证书以在SendRequest2事件中请求:

    context.SendingRequest2 += (sender, eventArgs) =>
        {
            // We can safely cast RequestMessage to HttpWebRequestMessage if this is not in batch.
            if (!eventArgs.IsBatchPart)
            {
                ((HttpWebRequestMessage)eventArgs.RequestMessage).HttpWebRequest.ClientCertificates.Add(theCertificate);
            }
        };