2012-02-12 13 views
8

Qualcuno mi può fornire un esempio su come creare un certificato auto firmato, che verrebbe accettato dal seguente codice:Come usare makecert per creare un certificato X509 accettato da 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(); 

ho utilizzato

makecert -pe myCertificate 

e

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

e

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

e ho provato a generare il certificato con BouncyCastle, ma ogni volta che sto ottenendo seguente eccezione:

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. 

e l'eccezione interna è nullo.

Probabilmente c'è un trucco, ma non lo capisco.

Come si genera un certificato adeguato per il servizio WCF ??

+2

Date un'occhiata a questo come collegare. http://msdn.microsoft.com/en-us/library/ff648498.aspx –

+0

Questo collegamento è stato il più utile per me nel creare il mio. Cammina attraverso tutti i passaggi. http://www.codeproject.com/Articles/96028/WCF-Service-with-custom-username-password-authenti – vikingben

risposta

1

Il seguente codice funziona per me per il quadro 4.0: E 'importante prima
per installare il certificato manualmente come certificato di fiducia nel vostro LocalMachine
Per fare questo è possibile installarlo semplicemente da Internet Explorer aprendo la posizione del server.

e la seconda per rispondere alle errore del server, a causa del certificato auto segno

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; 
     // } 
    } 

    } 
} 
Problemi correlati