我有一個使用自定義ServiceAuthorizationManager
的WCF服務。自定義身份驗證管理器已經設置爲處理Windows和窗體身份驗證。WCF UserName身份驗證:我可以在自定義ServiceAuthorizationManager中獲取用戶名嗎?
但是,如果我連接的客戶端設置爲UserName
auth,我似乎無法在任何地方找到用戶名。
客戶端代碼如下所示:
this.ClientCredentials.UserName.UserName = "user";
this.ClientCredentials.UserName.Password = "pass";
this.Open();
this.MyMethod(); // my actual contract method
this.Close();
然後在服務器上,我有我的自定義身份驗證經理:
public sealed class AppAuthorizationManager : ServiceAuthorizationManager
{
public override bool CheckAccess(OperationContext operationContext, ref Message message)
{
// would like to check user/pwd here...
}
}
這可能嗎?
- 的
Thread.CurrentPrincipal
沒有設置, operationContext.ServiceSecurityContext.PrimaryIdentity
未設置。operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets
爲空。
用戶/ pwd是否應該在任何地方都可用?或者我還必須添加自定義UsernamePasswordValidator
?
更新:所以我增加了一個自定義的UserNamePasswordValidator
和IAuthorizationPolicy
。 我的更新WCF的配置是這樣的:
<behavior name="Server2ServerBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="MyApp.AuthManager, MyApp">
<authorizationPolicies>
<add policyType="MyApp.TokenAuthorizationPolicy, MyApp" />
</authorizationPolicies>
</serviceAuthorization>
<serviceCredentials>
<userNameAuthentication customUserNamePasswordValidatorType="MyApp.PFUserNameValidator, MyApp" />
</serviceCredentials>
</behavior>
如果我在這些類中的所有3個設置斷點,WCF拋出該異常:
LogonUser failed for the 'username' user. Ensure that the user has a valid Windows account.
at System.IdentityModel.Selectors.WindowsUserNameSecurityTokenAuthenticator.ValidateUserNamePasswordCore(String userName, String password)
之前任何人都運行。嗯...
謝謝!對於稍後可能需要它的人,以下是如何添加自定義的AuthorizationPolicy:http://msdn.microsoft.com/en-us/library/ms729794.aspx – CodingWithSpike 2009-02-09 21:00:22