我是WCF的新手,现在真的迷失了这个问题...... 我希望我的WCF服务使用客户端提供的用户名和密码来验证传入的请求。
Web.Config的相关部分如下所示:
<endpoint name="wsBinding"
address=""
binding="wsHttpBinding"
contract="ServiceLib.IBooking"
bindingConfiguration="myWSSettings"
/>
和...
<bindings>
<wsHttpBinding>
<binding name="myWSSettings">
<security mode="Transport">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceCredentials>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType= "ServiceLib.MyCustomUserNameValidator, ServiceLib" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
MyCustomUserNameValidator只是一个临时验证器,如果用户名不等于密码,它只会抛出异常。
客户端程序(控制台应用程序)正在执行此操作:
BookingClient client = new BookingClient("wsBinding");
Passenger passenger = new Passenger();
// ../
client.ClientCredentials.UserName.UserName = "SomeUserName";
client.ClientCredentials.UserName.Password = "WrongPassword";
// ...
// ...
// NOTE: Following should throw My SecurityException Since username and
// Password are not equal
bool confirmed = client.IsTicketConfirmed(passenger);
这是我得到的错误:
HTTP请求未经授权,客户端身份验证方案为“Negotiate”。从服务器收到的身份验证标头是“Negotiate,NTLM,Basic
”非常感谢任何帮助!我花了很多时间试图解决这个问题但是徒劳无功。
由于 和Sandeep
注意:--------
答案 0 :(得分:1)
您必须将安全模式更改为TransportWithMessageCredential:
<wsHttpBinding>
<binding name="SafeServiceConf">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
您会找到一个好样本here。