C# WCF服务和添加自定义标识声明

C# WCF服务和添加自定义标识声明,c#,.net,wcf,C#,.net,Wcf,我创建了一些声明并将它们附加到线程,然后我想访问操作契约GetCurrentUser();用户电子邮件声明始终返回null 问题在哪里 使用本地IIS和绑定类型为wsHttpBinding的WCF <serviceCredentials> <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidator

我创建了一些声明并将它们附加到线程,然后我想访问操作契约GetCurrentUser();用户电子邮件声明始终返回null

问题在哪里

使用本地IIS和绑定类型为wsHttpBinding的WCF

<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...");
        }
    }
}
公共类UserValidator:UserNamePasswordValidator
{
公共覆盖无效验证(字符串用户名、字符串密码)
{
如果(用户名==null | |密码==null)
{
抛出新ArgumentNullException();
}
如果(用户名==密码)
{
var索赔=新列表
{
新索赔(ClaimTypes.Name,“AbbA”),
新索赔(ClaimTypes.Email,“foo@bar.com"),
新索赔(索赔类型。角色,“客户”)
};
var id=新的索赔实体(索赔,“样本”);
var principal=新的索赔人(id);
Thread.CurrentPrincipal=主体;
}
其他的
{
抛出新的FaultException(“错误的密码…”);
}
}
}
以及我的经营合同

    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";
        }
    }
公共字符串GetCurrentUser()
{
var principal=Thread.CurrentPrincipal;
if(主体身份验证)
{
var cp=权利要求电流;
var email=cp.FindFirst(ClaimTypes.email).Value;找到了解决方案,所以

我将WCF表单身份验证与Wif和会话身份验证模块(SAM)一起使用

你可以找到资料和样品

其他选项启用WCF身份验证服务

(一)

(二)

3) 找到了解决方案,所以

我将WCF表单身份验证与Wif和会话身份验证模块(SAM)一起使用

你可以找到资料和样品

其他选项启用WCF身份验证服务

(一)

(二)

(三)