4

我有一个使用自定义的 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)

在它们中的任何一个运行之前。嗯……

4

1 回答 1

6

这通常在UsernamePasswordValidator中处理- 这是您可以访问密码的唯一地方。但是,这不是您设置主体的地方 - 这将在IAuthorizationPolicy'Evaluate方法中,它可能看起来像:

bool IAuthorizationPolicy.Evaluate(
    EvaluationContext evaluationContext, ref object state)
{           
    IList<IIdentity> idents;
    object identsObject;
    if (evaluationContext.Properties.TryGetValue(
        "Identities", out identsObject) && (idents =
        identsObject as IList<IIdentity>) != null)
    {
        foreach (IIdentity ident in idents)
        {
            if (ident.IsAuthenticated &&
                ident.AuthenticationType == TrustedAuthType)
            {                           
                evaluationContext.Properties["Principal"]
                    = //TODO our principal
                return true;
            }
        }
    }
    if (!evaluationContext.Properties.ContainsKey("Principal"))
    {
        evaluationContext.Properties["Principal"] = //TODO anon
    }                
    return false;
}

TrustedAuthType我们的密码验证器的名称在哪里)

有了这个,线程的主体将被设置,我们可以识别自己(并使用基于角色的安全等)

于 2009-02-09T19:58:14.483 回答