簡體   English   中英

WCF服務並添加自定義身份聲明

[英]WCF service and adding custom Identity claims

我創建了一些聲明並將它們附加到線程中,然后我想訪問操作合同GetCurrentUser();。 用戶電子郵件聲明始終返回null。

問題出在哪兒?

WCF使用本地IIS,綁定類型為wsHttpBinding。

<serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
              customUserNamePasswordValidatorType="SampleService.UserValidator, SampleService" />
</serviceCredentials>

自定義驗證器;

public class UserValidator : UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        if (userName == null || password == null)
        {
            throw new ArgumentNullException();
        }

        if (userName == password)
        {
            var claims = new List<Claim>
                {
                    new Claim(ClaimTypes.Name, "AbbA"),
                    new Claim(ClaimTypes.Email, "foo@bar.com"),
                    new Claim(ClaimTypes.Role,"Customer")
                };

            var id = new ClaimsIdentity(claims, "Sample");
            var principal = new ClaimsPrincipal(id);

            Thread.CurrentPrincipal = principal;
        }
        else
        {
            throw new FaultException("Wrong Password...");
        }
    }
}

和我的運營合同;

    public string GetCurrentUser()
    {
        var principal = Thread.CurrentPrincipal;

        if (principal.Identity.IsAuthenticated)
        {
            var cp = ClaimsPrincipal.Current;
            var email = cp.FindFirst(ClaimTypes.Email).Value; <<<< It's return always null :(

            return string.Format("Your Email is:{0}", email);
        }
        else
        {
            return "NONE";
        }
    }

找到了解決方案,於是;

我將WCF表單身份驗證與Wif和會話身份驗證模塊(SAM)一起使用

您可以找到信息和樣本;

http://brockallen.com/2013/01/26/replacing-forms-authentication-with-wifs-session-authentication-module-sam-to-enable-claims-aware-identity/

其他選項啟用WCF身份驗證服務;

1) https://msdn.microsoft.com/library/bb398990

2) https://msdn.microsoft.com/tr-tr/library/bb398862%28v=vs.100%29.aspx

3) https://msdn.microsoft.com/tr-tr/library/bb398778%28v=vs.100%29.aspx

暫無
暫無

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

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