C# WCF数据服务认证

C# WCF数据服务认证,c#,.net,wcf,wcf-data-services,C#,.net,Wcf,Wcf Data Services,-是否可以使用基于证书的身份验证来保护WCF数据服务 -是否有描述此过程的资源 -我们可以在WCF数据服务中使用消息安全性吗?您所有问题的答案都是“是”。下面是一个由微软模式与实践团队提供的信息丰富的链接,它可以准确地完成您想要的内容 基于证书的身份验证可以这样做: 服务器端: public class ODataService : DataService<Database> { public ODataService() {

-是否可以使用基于证书的身份验证来保护WCF数据服务

-是否有描述此过程的资源


-我们可以在WCF数据服务中使用消息安全性吗?

您所有问题的答案都是“是”。下面是一个由微软模式与实践团队提供的信息丰富的链接,它可以准确地完成您想要的内容


基于证书的身份验证可以这样做:

服务器端:

public class ODataService : DataService<Database>
    {
        public ODataService()
        {
            ProcessingPipeline.ProcessingRequest += ProcessingPipeline_ProcessingRequest;
        }

        void ProcessingPipeline_ProcessingRequest(object sender, DataServiceProcessingPipelineEventArgs e)
        {
            if (!HttpContext.Current.Request.ClientCertificate.IsPresent)
            {
                throw new DataServiceException(401, "401 Unauthorized");
            }

            var cert = new X509Certificate2(HttpContext.Current.Request.ClientCertificate.Certificate);
            if (!ValidateCertificate(cert))
            {
                throw new DataServiceException(401, "401 Unauthorized");
            }

            var identity = new GenericIdentity(cert.Subject, "ClientCertificate");
            var principal = new GenericPrincipal(identity, null);
            Thread.CurrentPrincipal = principal;
            HttpContext.Current.User = principal;
        }

        private bool ValidateCertificate(X509Certificate2 cert)
        {
            // do some validation
        }
像这样使用它

        Database db = new Database(new Uri(service));
        db.ClientCertificate = CertificateUtil.GetCertificateByThumbprint(StoreName.My,
                                                                          StoreLocation.LocalMachine,
                                                                          "<a thumbprint>");
Database db=新数据库(新Uri(服务));
db.ClientCertificate=CertificateUtil.GetCertificateByThumbprint(StoreName.My,
StoreLocation.LocalMachine,
"");
私钥存储在客户端计算机上,公钥存储在本地计算机/受信任根CA中的服务器上

记住在IIS中要求/协商此站点的客户端证书


(在WCF Data Services 5.2上测试,VS 2012)

我遵循了教程,但在尝试访问该服务时不断出现异常:“403-禁止:访问被拒绝”是的,但您需要使用WS-*绑定来获得消息级别的安全性。确保您的客户端可以使用WS*标准。我认为.NET3.0及更高版本支持它。Silverlight不支持此操作。
        Database db = new Database(new Uri(service));
        db.ClientCertificate = CertificateUtil.GetCertificateByThumbprint(StoreName.My,
                                                                          StoreLocation.LocalMachine,
                                                                          "<a thumbprint>");