我有一个使用自定义的 WCF 服务ServiceAuthorizationManager
。自定义身份验证管理器已设置为处理 Windows 和窗体身份验证。
但是,如果我连接一个设置为UserName
身份验证的客户端,我似乎无法在任何地方找到用户名。
客户端代码如下所示:
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
是空的。
用户/密码是否应该在任何地方都可用?还是我也必须添加自定义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)
在它们中的任何一个运行之前。嗯……