客户端身份验证方案“Anonymous”禁止HTTP请求

时间:2012-06-06 09:53:15

标签: wcf

我有一个代码片段如下:

WCF服务:

    internal static void Main()
    {
        WSHttpBinding myBinding = new WSHttpBinding();
        myBinding.Security.Mode = SecurityMode.Transport;
        myBinding.Security.Transport.ClientCredentialType = 
            HttpClientCredentialType.Certificate;

        Uri baseAddress = new Uri("Https://10.2.5.29:8056/WCFService/");

        ServiceHost myServiceHost = 
            new ServiceHost(typeof(GetIdentity), baseAddress);

        ServiceEndpoint myServiceEndpoint = 
            myServiceHost.AddServiceEndpoint(typeof(IGetIdentity), 
            myBinding, "GetIdentity");

        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
        behavior.HttpGetEnabled = true;
        behavior.HttpGetUrl = new Uri("http://10.2.5.29:8057/mex");
        myServiceHost.Description.Behaviors.Add(behavior);

        myServiceHost.Open();
        Console.WriteLine("Service started!");
        Console.ReadLine();
        myServiceHost.Close();
    }

WCF客户端:

    static bool ValidateServerCertificate(object sender, 
        X509Certificate certificate, 
        X509Chain chain, 
        SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }


    static void Main(string[] args)
    {
        WSHttpBinding myBinding = new WSHttpBinding();
        myBinding.Security.Mode = SecurityMode.Transport;
        myBinding.Security.Transport.ClientCredentialType = 
            HttpClientCredentialType.Certificate;

        EndpointAddress ea = 
            new EndpointAddress("https://10.2.5.29:8056/WCFService/GetIdentity");

        GetIdentityClient gc = new GetIdentityClient(myBinding, ea);

        gc.ClientCredentials.ClientCertificate.SetCertificate(
            "CN=TestClient", StoreLocation.CurrentUser, StoreName.My);

        ServicePointManager.ServerCertificateValidationCallback = 
            new RemoteCertificateValidationCallback(ValidateServerCertificate);

        string result = gc.Get(WindowsIdentity.GetCurrent().Name);
        Console.WriteLine(result);
        Console.ReadLine();
    }

我还使用以下命令将端口与证书绑定。

netsh http add sslcert ipport=0.0.0.0:8056 certhash=bc2935a1d7aab31911613abcb05e9291fcc7bd60 appid= {0D997C3D-0599-45FC-90FE-B0373FBF1709} clientcertnegotiation=enable

最后,客户端抛出“HTTP请求被禁止使用客户端身份验证方案'Anonymous'。”。 谁能告诉我原因?

感谢

0 个答案:

没有答案