2015-09-16 79 views
1

我創建了一些聲明並附加它們的線程,然後我想訪問操作合同GetCurrentUser();用戶電子郵件聲明總是返回null。WCF服務和添加自定義身份聲明

問題在哪裏?

使用本地IIS和綁定類型的WCF是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, "[email protected]"), 
        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"; 
     } 
    } 

回答

0
相關問題