外部 Web サービスを利用する Web アプリケーションを実行しています。この外部 Web サービスでは、要求ごとに署名する必要があります。WebServicesClientProtocol
そのため、最初に外部 Web サービスを使用してクラスと .NET 2.0 を使用し、次にReference.cs
ファイルを手動で編集して、拡張クラスを からSystem.Web.Services.Protocols.SoapHttpClientProtocol
に変更しますMicrosoft.Web.Services2.WebServicesClientProtocol
。次に、Page_Load
メソッドには次のコードがあります。
try
{
// Create the ws endpoint
var uriServiceAddress = new Uri("urn:something-wse:something_NNNN");
var uribuilderViaRouter = new UriBuilder("http://xx.xxx.xx/SrvXXX_NNNN/Test.asmx");
var endpointReference = new EndpointReference(uriServiceAddress, uribuilderViaRouter.Uri);
// Create the ws client
var client = (WebServicesClientProtocol) new Test.Something();
client.Destination = endpointReference;
// Read the certificate from MyStore on LocalMachine
X509CertificateStore localStore = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);
X509SecurityToken securityToken = null;
if (!localStore.OpenRead()) throw new Exception("Unable to open localstore for read");
X509CertificateCollection certificateCollection = localStore.FindCertificateBySubjectString("email@subject.test");
if (certificateCollection.Count == 0) throw new Exception("Unable to obtain security token.");
securityToken = new X509SecurityToken(certificateCollection[0]);
localStore.Close();
// Attach the security toekn to the client request
client.RequestSoapContext.Security.Tokens.Add(securityToken);
client.RequestSoapContext.Security.Elements.Add(new MessageSignature(securityToken));
// Set the timeouts
client.RequestSoapContext.Security.Timestamp.TtlInSeconds = 2 * 60;
client.Timeout = 60 * 10 * 1000; // 10 mínútur ættu að duga í flest.
// Call the test function
DataSet set = ((Test.Something)client).searchMethod("Parameter 1", "Parameter 2");
Label1.Text = User.Identity.Name+ " worked! " + set.Tables.Count + " tables!";
}
catch (Exception exc)
{
Label1.Text = User.Identity.Name + " exception: " + exc.ToString();
}
Visual Studio Development Server を使用してこれを実行すると問題なく動作しますが、IIS に変更すると動作が停止し、醜いCryptography_CSP_NoPrivateKey
例外が発生します。
1) MMC を使用して証明書を LocalMachine/MyStore に適切に読み込んでから、WSE 2.0 SP3 を使用して秘密鍵のアクセス許可を変更し、全員がアクセスできるようにします。これはここから見ることができます:
代替テキスト http://www1.ruedenet.is/files/CertError1.png
2)アプリケーションのデバッグ時に Visual Studio Development Server が使用されるように、プロパティも設定します。
代替テキスト http://www1.ruedenet.is/files/CertError2.png
3)次に、それを実行すると、素晴らしい結果が得られます。
代替テキスト http://www1.ruedenet.is/files/CertError3.png
4)ただし、IIS を使用するようにプロパティを変更すると (VS に仮想ディレクトリを作成させると)、次のようになります。
代替テキスト http://www1.ruedenet.is/files/CertError4.png
5)また、IIS の認証方法を変更して、ログオンできるようにします (実際には理由はありません)。
代替テキスト http://www1.ruedenet.is/files/CertError5.png
6) Windows へのログオンを求められます。
代替テキスト http://www1.ruedenet.is/files/CertError6.png
7)そして、私のページが実行され、エラーが発生します:
代替テキスト http://www1.ruedenet.is/files/CertError7.png
あなたがこれで私を助けることができれば、私はきっとそれを感謝します. 私はすでに何時間も費やしてきましたが、皆さんが見ることができる根本的なエラーを犯している場合、これ以上時間を費やしたくありません. ところで:UACをオフにして、Windows Server 2008でVisual Studio 2008を使用して開発しています:-)
皆さんからの連絡を本当に楽しみにしています。ありがとう。