亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用代碼隱藏而不是 Web.Config 端點調用服務時出現不安全或安全錯誤錯誤

使用代碼隱藏而不是 Web.Config 端點調用服務時出現不安全或安全錯誤錯誤

C#
HUH函數 2023-09-16 15:14:34
我有一個具有消息安全性的 WCF 服務,并且我正在嘗試將客戶端連接到它。使用由 Visual Studio 中的“添加服務引用”自動生成的默認 Web.Config 設置,連接效果非常好,設置如下:端點:  <endpoint address="http://localhost:56017/services/MaternumPdfService.svc/soap"    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMaternumPdfService"    contract="MaternumPdfServiceReference.IMaternumPdfService" name="WSHttpBinding_IMaternumPdfService">    <identity>      <certificate encodedValue="BASE64 IS HERE" />    </identity>  </endpoint>捆綁:    <binding name="WSHttpBinding_IMaternumPdfService" maxReceivedMessageSize="2147483647">      <security>        <message clientCredentialType="UserName" negotiateServiceCredential="false"          establishSecurityContext="false" />      </security>      <readerQuotas maxStringContentLength="2147483647" maxDepth="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>    </binding>這可以順利進行:        using (MaternumPdfServiceClient pdfService = new MaternumPdfServiceClient("WSHttpBinding_IMaternumPdfService"))        {            pdfService.ClientCredentials.UserName.UserName = "sample";            pdfService.ClientCredentials.UserName.Password = "sample";            pdfService.Open();            if (!pdfService.State.Equals(CommunicationState.Opened))            {                return null;            }            MemoryStream ms = new MemoryStream();            pdfService.GetMaternumPdf(pdftype,params).CopyTo(ms);        }我以為我正在 C# 版本和使用 Web.Config 設置的版本之間構建完全等效的代碼,但與第二個版本連接會導致異常:'An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.'具有以下內部異常:An error occurred when verifying security for the message.不是特別清楚...這似乎與消息安全有關,但與 Web.Config 的連接工作得很好,這讓我相信我沒有在正確的類中設置證書。我調查過:WCF 給出不安全或不正確安全的故障錯誤服務器和客戶端都是本地主機,所以不應該是時間不匹配憑據正確,因為 Web.Config 版本使用相同的憑據并且驗證良好我使用的是 HTTP,而不是 HTTPS使用 IISExpress,而不是 IIS就是這樣。我認為我將客戶端證書設置在錯誤的位置,如果不是通過 ClientCredentials.ClientCertificate.Certificate 或端點的身份,我應該如何設置它?
查看完整描述

2 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

...通常情況下,我的配置錯誤。我獲得了一個名為“MaternumCertificateClient”的證書,而不是我的服務器配置的證書,即“MaternumCertificateServer”。


更重要的是我是如何得出這個錯誤的。


第一步,我設置 Wireshark 以查看服務器的回復內容:


HTTP/1.1 500 Internal Server Error

Cache-Control: private

Content-Type: application/soap+xml; charset=utf-8

Server: Microsoft-IIS/10.0

Set-Cookie: ASP.NET_SessionId=k0xmopx3eitnvmocv1rjas4h; path=/; HttpOnly

X-AspNet-Version: 4.0.30319

X-SourceFiles: =?UTF-8?B?QzpcREVWXE1BVFxNYXRlcm51bV9hcHBcc2VydmljZXNcTWF0ZXJudW1QZGZTZXJ2aWNlLnN2Y1xzb2Fw?=

X-Powered-By: ASP.NET

Date: Thu, 22 Aug 2019 16:25:19 GMT

Content-Length: 648


<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">

<s:Header>

? ? <a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/soap/fault</a:Action>

? ? <a:RelatesTo>urn:uuid:uid is here</a:RelatesTo>

</s:Header>

<s:Body>

? ? <s:Fault>

? ? ? ? <s:Code>

? ? ? ? ? ? <s:Value>s:Sender</s:Value>

? ? ? ? ? ? <s:Subcode>

? ? ? ? ? ? ? ? <s:Value xmlns:a="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">a:InvalidSecurity</s:Value>

? ? ? ? ? ? </s:Subcode>

? ? ? ? </s:Code>

? ? ? ? <s:Reason>

? ? ? ? ? ? <s:Text xml:lang="en-GB">An error occurred when verifying security for the message.</s:Text>

? ? ? ? </s:Reason>

? ? </s:Fault>

</s:Body>

</s:Envelope>

好不了多少,但我至少知道是誰拋出了異常。

因此,正如所示,我使用以下內容設置我的服務器:

<serviceSecurityAudit?auditLogLocation=“Application“?
????serviceAuthorizationAuditLevel=“Failure“?
????messageAuthenticationAuditLevel=“Failure“?
????suppressAuditFailure=“true“?/>

在配置/system.serviceModel/behaviors/serviceBehaviors/behavior。

然后,Windows 事件查看器會顯示有關該錯誤的詳細信息。

顯示的消息,

MessageSecurityException:?The?EncryptedKey?clause?was?not?wrapped?with?the?required?encryption?token?'System.IdentityModel.Tokens.X509SecurityToken'.

表示證書不匹配。我正在加載一個名為 MaternumCertificateClient 的證書,并且我需要 MaternumCertificateServer。此外,這些行

pdfService.ClientCredentials.ClientCertificate.Certificate?=?cert;

pdfService.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode?=?X509CertificateValidationMode.PeerTrust;

pdfService.ClientCredentials.ServiceCertificate.DefaultCertificate?=?GetCertificate("MaternumCertificateClient");

實際上并不需要;證書是從端點的身份中讀取的,并且我的設置不需要這些設置即可工作。


查看完整回答
反對 回復 2023-09-16
?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

正如您所指出的,除了我們使用證書對客戶端進行身份驗證之外,不需要在客戶端提供證書。比如下面的配置。

<wsHttpBinding>??
????<binding?name="WSHttpBinding_ICalculator">??
??????<security?mode="Message">??
????????<message?clientCredentialType="Certificate"?/>??
??????</security>??
????</binding>??
??</wsHttpBinding>


在服務器端的綁定配置中,只有用戶名/密碼需要憑證。

??<security>
????<message?clientCredentialType="UserName"?negotiateServiceCredential="false"
??????establishSecurityContext="false"?/>
??</security>

證書通過使用公鑰加密 SOAP 消息來提供消息安全性。在通信過程中,客戶端和服務器端交換證書的公鑰。我們甚至可以指定服務器域名作為身份,它只是用來保證服務器的身份。
另外,如果你的回復解決了你的問題。請標記為有用,以便幫助遇到類似問題的其他人。
如果有什么需要我幫忙的,請隨時告訴我。


查看完整回答
反對 回復 2023-09-16
  • 2 回答
  • 0 關注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號