2017-02-07 7 views
0

データを取得するために別のサーバーXに接続するWCFサービスアプリケーションがあります。サーバーXが一方向SSLで構成されている場合、WCFサービスアプリケーションは問題なく接続できます。しかし、サーバーXが双方向SSLで構成されている場合、WCFサービスアプリケーションは接続に問題があります。HttpWebRequestで動作するように双方向SSLを取得できない

WCFサービスアプリケーションは、自己署名証明書を使用します。この証明書は秘密鍵なしで.cerファイルにエクスポートされ、サーバーXの信頼ストアにインポートされました。同じエクスポートした証明書は、WCFサービスアプリケーションのコードで使用された:

string URL = "https://11.205.112.49:5311/Ping"; 
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL); 
X509Certificate2 Certificate = new X509Certificate2("F:\\cert.cer"); 
Request.ClientCertificates.Add(Certificate); 
Request.Method = "GET"; 
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse(); 

上記のコードの最後の行は、次のメッセージとWebExceptionがスローされます:上記のエラーは、多くの詳細を提供しません

The request was aborted: Could not create SSL/TLS secure channel. 

ヘルプは感謝しています。

ありがとうございます。

EDIT:

これは私の全体のプログラムです:

using System.Net; 
using System.Security.Cryptography.X509Certificates; 
using System.IO; 

namespace TestTwoWaySSL 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      HttpWebResponse Response = null; 
      try 
      { 
       string URL = "https://11.205.112.49:5311/Ping"; 
       Console.WriteLine("URL: [{0}].", URL); 
       HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL); 
       X509Certificate2 Certificate = new X509Certificate2("F:\\cert.pfx", "password"); 
       Request.ClientCertificates.Add(Certificate); 
       Request.Method = "GET"; 
       Console.WriteLine("Sending request."); 
       Response = (HttpWebResponse)Request.GetResponse(); 
       Console.WriteLine("Request sent."); 
      } 
      catch (WebException webException) 
      { 
       Console.WriteLine("Web exception occurred: [{0}].", webException.Message); 
       Console.WriteLine(webException.StackTrace); 
       Response = webException.Response as HttpWebResponse; 
      } 
      catch (Exception exception) 
      { 
       Console.WriteLine("Exception occurred: [{0}].", exception.Message); 
       Console.WriteLine(exception.StackTrace); 
      } 
      Console.WriteLine("Response status code: [{0}].", Response.StatusCode); 
      Console.WriteLine("Response \"Content-Type\" header value: [{0}].", Response.ContentType); 
      Console.WriteLine("Response \"Content-Length\" header value: [{0}].", Response.ContentLength); 
      String ResponseMessageBody = new StreamReader(Response.GetResponseStream()).ReadToEnd(); 
      Console.WriteLine("Response message body: [{0}].", ResponseMessageBody); 
     } 
    } 
} 

が、それはラインRequest.GetResponseを()に達し、AccessVioationExceptionがスローされます。問題

Unhandled Exception: System.AccessViolationException: Attempted to read or write 
protected memory. This is often an indication that other memory is corrupt. 
    at System.Net.UnsafeNclNativeMethods.SafeNetHandles_SECURITY.AcquireCredentialsHandleW(String principal, String moduleName, Int32 usage, Void* logonID, SecureCredential& authData, Void* keyCallback, Void* keyArgument, SSPIHandle& handlePtr, Int64& timeStamp) 
    at System.Net.SafeFreeCredentials.AcquireCredentialsHandle(SecurDll dll, String package, CredentialUse intent, SecureCredential& authdata, SafeFreeCredentials& outCredential) 
    at System.Net.SSPISecureChannelType.AcquireCredentialsHandle(String moduleName, CredentialUse usage, SecureCredential& authdata, SafeFreeCredentials& outCredential) 
    at System.Net.SSPIWrapper.AcquireCredentialsHandle(SSPIInterface SecModule, String package, CredentialUse intent, SecureCredential scc) 
    at System.Net.Security.SecureChannel.AcquireCredentialsHandle(CredentialUse credUsage, SecureCredential& secureCredential) 
    at System.Net.Security.SecureChannel.AcquireClientCredentials(Byte[]& thumbPrint) 
    at System.Net.Security.SecureChannel.GenerateToken(Byte[] input, Int32 offset, Int32 count, Byte[]& output) 
    at System.Net.Security.SecureChannel.NextMessage(Byte[] incoming, Int32 offset, Int32 count) 
    at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) 
    at System.Net.TlsStream.CallProcessAuthentication(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) 
    at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) 
    at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) 
    at System.Net.ConnectStream.WriteHeaders(Boolean async) 
    at System.Net.HttpWebRequest.EndSubmitRequest() 
    at System.Net.HttpWebRequest.SetRequestSubmitDone(ConnectStream submitStream) 
    at System.Net.Connection.CompleteConnection(Boolean async, HttpWebRequest request) 
    at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest) 
    at System.Net.Connection.CompleteStartRequest(Boolean onSubmitThread, HttpWebRequest request, TriState needReConnect) 
    at System.Net.Connection.SubmitRequest(HttpWebRequest request, Boolean forcedsubmit) 
    at System.Net.ServicePoint.SubmitRequest(HttpWebRequest request, String connName) 
    at System.Net.HttpWebRequest.SubmitRequest(ServicePoint servicePoint) 
    at System.Net.HttpWebRequest.GetResponse() 
    at TestTwoWaySSL.Program.Main(String[] args) 

答えて

1

秘密鍵がないことです。証明書のみを使用しています。

どちらかを使用するPKCS#12 loading certificate into X509Certificate2 class

X509Certificate2 Certificate = new X509Certificate2("F:\\cert.p12", "p12 password"); 

またはload certificate from store

X509Store store = new X509Store("MY",StoreLocation.CurrentUser); 
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); 

X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates; 
X509Certificate2 certificate = collection.Find(...)[0]; 
store.Close(); 
+0

こんにちはPEPO、私は両方の方法を試みたが、両方の方法は、AccessViolationExceptionがスローされます。上の私の完全なソースコードと例外スタックトレースを参照してください。 – user3573403

+0

OK、CNG Key Isolationサービスを有効にする必要があることがわかりました。有効にすると、AccessViolationExceptionはなくなります。しかし、今では「基盤となる接続が閉じられました:SSL/TLSセキュアチャネルの信頼関係を確立できませんでした」というメッセージが表示されています。 – user3573403

+0

"ServicePointManager.ServerCertificateValidationCallback = ..."という行に入れなかったことに気づきました。私が入れた後、それは動作します。 – user3573403

関連する問題