我创建了一些声明并附加了它们的线程,然后我想访问操作契约GetCurrentUser();用户电子邮件声明始终返回null。
问题出在哪里?
使用本地IIS和绑定类型的WCF是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...");
}
}
}
和我的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 :(得分:0)
找到解决方案,所以;
我使用Wif Forms身份验证和Wif以及会话身份验证模块(SAM)
您可以找到信息和样本;
其他选项启用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