2012-02-12 37 views
8

bất cứ ai có thể cung cấp cho tôi với một ví dụ về cách tạo một chứng chỉ tự ký kết, trong đó sẽ được chấp nhận bởi các đoạn mã sau:Làm thế nào để sử dụng makecert để tạo ra một giấy chứng nhận X509 được chấp nhận bởi WCF

 ServiceHost svh = new ServiceHost(typeof(MyClass)); 

     var tcpbinding = new NetTcpBinding(SecurityMode.TransportWithMessageCredential, true); 
     //security 
     tcpbinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; 
     svh.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new BWUserNamePasswordValidator(); 
     svh.Credentials.UserNameAuthentication.UserNamePasswordValidationMode =UserNamePasswordValidationMode.Custom; 
     svh.Credentials.ServiceCertificate.Certificate = BookmarkWizSettings.TcpBindingCertificate; 
     .... 
     svh.Open(); 

Tôi đã sử dụng

makecert -pe myCertificate 

makecert -sv SignRoot.pvk -cy authority -r signroot.cer -a sha1 -n "CN=Dev Certification Authority" -ss my -sr localmachine 

makecert -r -pe -n "CN=Client" -ss MyApp -sky Exchange 

và tôi đã cố gắng để tạo ra các giấy chứng nhận với BouncyCastle, nhưng mỗi lần tôi nhận được ngoại lệ sau đây:

It is likely that certificate 'CN=Dev Certification Authority' may not have a 
private key that is capable of key exchange or the process may not have access 
rights for the private key. Please see inner exception for detail. 

và ngoại lệ bên trong là null.

Có thể là một thủ thuật, nhưng tôi không hiểu.

Làm cách nào để tạo chứng chỉ phù hợp cho dịch vụ WCF của tôi ??

+2

Hãy xem này làm thế nào để liên kết. http://msdn.microsoft.com/en-us/library/ff648498.aspx –

+0

Liên kết này hữu ích nhất cho tôi khi thiết lập mỏ. Nó đi qua tất cả các bước. http://www.codeproject.com/Articles/96028/WCF-Service-with-custom-username-password-authenti – vikingben

Trả lời

1

Các mã sau đây làm việc cho tôi cho khung 4.0: Điều quan trọng đầu tiên là
để cài đặt chứng chỉ của bạn bằng tay như chứng chỉ tin cậy trong LocalMachine bạn
Để làm được điều này, bạn có thể cài đặt nó chỉ đơn giản từ internet explorer bởi mở vị trí máy chủ.

và thứ hai để đáp ứng các lỗi máy chủ, vì các chứng chỉ tự ký

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Security.Cryptography.X509Certificates; 
using System.Net; 
using System.Net.Security; 
namespace WCFSelfSignCert 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     //You have to install your certificate as trusted certificate in your LocalMachine 

     //create your service client/ procy 
     using (MyProxy.ServiceClient client = new MyProxy.ServiceClient()) 
     { 

      //server certification respond with an error, because doesnt recognize the autority 
      ServicePointManager.ServerCertificateValidationCallback += OnServerValError; 


      //Assign to self sign certificate 
      client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, 
      StoreName.Root, 
      X509FindType.FindBySubjectName, 
      "MY custom subject name"); //SubjectName(CN) from certificate 

      //make a test call to ensure that service responds 
      var res = client.echo("test"); 

      Console.WriteLine(res); 
      Console.ReadKey(); 
     } 

    } 

    public static bool OnServerValError(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { 
     //mute the error, or provide some custom validation code 
     return true; 

     //or more restrictive 

     // if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateNameMismatch) 
     //{ 


     // return true; 
     // } 
     // else 
     //{ 

     // return false; 
     // } 
    } 

    } 
} 
Các vấn đề liên quan