簡體   English   中英

WCF UserName身份驗證:我可以在自定義ServiceAuthorizationManager中獲取用戶名嗎?

[英]WCF UserName Authentication: Can I get the Username in a custom ServiceAuthorizationManager?

我有一個使用自定義ServiceAuthorizationManager的WCF服務。 自定義身份驗證管理器已設置為處理Windows和Forms身份驗證。

但是,如果我連接到設置為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為空。

用戶/密碼應該在任何地方可用嗎? 或者我是否還要添加自定義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)

在任何一個運行之前。 嗯...

這通常在UsernamePasswordValidator中處理 - 這是您唯一可以訪問密碼的地方。 但是,這不是您設置主體的位置 - 這將在IAuthorizationPolicyEvaluate方法中,它可能類似於:

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是我們的密碼驗證器的名稱)

有了這個,線程的主體將被設置,我們可以識別自己(並使用基於角色的安全性等)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM