【发布时间】:2017-02-10 02:20:26
【问题描述】:
我是网络服务的新手。我想编写一个带有用户名和密码身份验证的简单 WCF。使用SoapUI进行测试时,如果将安全模式设置为none(无基本认证),则可以调用成功。一旦将安全模式设置为消息(使用基本身份验证),它就无法返回正确的结果。两者都可以使用 C# 客户端成功调用。 我在 StackOverflow 上应用了很多建议,但仍然无法得到正确的结果。
- 在 SoapUI 的“请求属性”中的“Wss-密码类型”部分 只需选择选项“PasswordText”。
- negotiateServiceCredential="true"
- 复选标记“将默认 WSA 添加到”
- 在 http 设置中检查“为传出结果添加身份验证信息”
是否需要进行其他设置,例如配置证书?我期待您的帮助。
设置详情如下。
安全模式设置为消息时,返回结果为
<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>
</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="zh-HK">An error occurred when verifying security for the message.</s:Text>
</s:Reason>
</s:Fault>
</s:Body>
</s:Envelope>
SoapUI 日志:
DEBUG:尝试 1 执行请求 调试:发送请求:POST /ServiceHello.svc HTTP/1.1 调试:接收响应:HTTP/1.1 500 内部服务器错误 调试:连接可以无限期地保持活动状态 信息:在 3 毫秒(576 字节)内得到 [WSHttpBinding_IServiceHello.HelloWorld:Request 1] 的响应
安全模式的结果=无
<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://tempuri.org/IServiceHello/HelloWorldResponse</a:Action>
</s:Header>
<s:Body>
<HelloWorldResponse xmlns="http://tempuri.org/">
<HelloWorldResult>Hello World</HelloWorldResult>
</HelloWorldResponse>
</s:Body>
</s:Envelope>
这是 web.config
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="WCFTest.ServiceHello"
behaviorConfiguration="WCFTest_Behavior">
<endpoint
address=""
binding="wsHttpBinding"
contract="WCFTest.IServiceHello"
bindingConfiguration="WCFTest_Config">
</endpoint>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="WCFTest_Config">
<security mode="None">
<!—The only difference between two web service is the security mode, which is set to message in the authentication version -->
<message clientCredentialType="UserName" negotiateServiceCredential="false"
establishSecurityContext="false" algorithmSuite="Default"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="WCFTest_Behavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="None"/>
</clientCertificate>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WCFTest.App_Code.Authentication.CustomValidator,App_Code/Authentication"/>
<serviceCertificate
findValue="myCertificate"
storeLocation="LocalMachine"
storeName="My"
x509FindType="FindBySubjectName" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
【问题讨论】:
标签: c# wcf authentication soapui