【发布时间】:2010-10-15 11:36:02
【问题描述】:
我正在做一个利用外部网络服务的网络应用程序。这个外部网络服务要求我签署我的每一个请求。所以我使用WebServicesClientProtocol 类和.NET 2.0,首先使用外部Web 服务,然后手动编辑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 开发服务器运行它时,它工作正常,但是当我更改为 IIS 时它停止工作,并且我得到了丑陋的 Cryptography_CSP_NoPrivateKey 异常。
1) 我已经使用 MMC 将证书正确读入 LocalMachine/MyStore,然后我使用 WSE 2.0 SP3 更改了私钥权限,以便每个人都可以完全访问它。你可以从这里看到:
alt text http://www1.ruedenet.is/files/CertError1.png
2) 我还设置了属性,以便在调试应用程序时使用 Visual Studio 开发服务器:
alt text http://www1.ruedenet.is/files/CertError2.png
3) 然后我运行它并得到一个不错的结果:
alt text http://www1.ruedenet.is/files/CertError3.png
4) 但是,当我将属性更改为使用 IIS(并让 VS 创建虚拟目录)时,如下所示:
alt text http://www1.ruedenet.is/files/CertError4.png
5) 我还更改了 IIS 中的身份验证方法,以便我能够登录(真的没有理由):
alt text http://www1.ruedenet.is/files/CertError5.png
6) 所以我被要求登录 Windows:
alt text http://www1.ruedenet.is/files/CertError6.png
7) 然后我的页面运行并产生错误:
alt text http://www1.ruedenet.is/files/CertError7.png
如果您能帮我解决这个问题,我将不胜感激。我已经在这上面花了几个小时,如果我犯了你们可以看到的基本错误,我不想花更多时间。顺便说一句:我在关闭 UAC 的情况下在 Windows Server 2008 上使用 Visual Studio 2008 进行开发:-)
真的很期待收到你们的来信。谢谢。
【问题讨论】:
标签: c# web-services security certificate signing