[英]Accessing WCF client credentials from the service
我有一個WCF服務的以下調用(使用基本身份驗證):
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";
client.MyServiceFunction();
在服務器上,我有:
class MyService : IMyService
{
string MyServiceFunction()
{
return GetUsernameHere();
}
}
我的問題是,我可以在WCF服務中訪問這些憑據,如果是,如何訪問? 也就是說,我將如何實現GetUsernameHere
函數?
要使此類驗證起作用,您必須為用戶名和密碼編寫自己的驗證器。
您創建一個繼承自UserNamePasswordValidator
的類,並在您的webconfig中指定它,如下所示:
<serviceBehaviors>
<behavior name="CustomValidator">
<serviceCredentials>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType=
"SomeAssembly.MyCustomUserNameValidator, SomeAssembly"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
自定義驗證器類將如下所示:
public class MyCustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
// Do your username and password check here
}
}
密碼在WCF的驗證部分之外不可用,因此您只能使用自定義驗證器檢索密碼。
但是,如果您只想要用戶名,則應通過以下方式訪問:
OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name
但是,您可能需要指定<message establishSecurityContext="true" />
作為綁定安全性的一部分,並且這不適用於所有綁定,例如。 basicHttpBinding的
<bindings>
<wsHttpBinding>
<!-- username binding -->
<binding name="Binding">
<security mode="Message">
<message clientCredentialType="UserName" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.