简体   繁体   English

WCF服务并添加自定义身份声明

[英]WCF service and adding custom Identity claims

I created some claims and attach them thread, then I want to access operation contract GetCurrentUser(); 我创建了一些声明并将它们附加到线程中,然后我想访问操作合同GetCurrentUser();。 user email claim always return null. 用户电子邮件声明始终返回null。

Where is the problem? 问题出在哪儿?

WCF using Local IIS and binding type is wsHttpBinding. WCF使用本地IIS,绑定类型为wsHttpBinding。

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

Custom Validator; 自定义验证器;

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...");
        }
    }
}

And My OperationContract; 和我的运营合同;

    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";
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM