[英]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";
}
}
Found a solution, so; 找到了解决方案,于是;
I used WCF Forms authentication with Wif and session authentication module (SAM) 我将WCF表单身份验证与Wif和会话身份验证模块(SAM)一起使用
You can find information and sample; 您可以找到信息和样本;
http://brockallen.com/2013/01/26/replacing-forms-authentication-with-wifs-session-authentication-module-sam-to-enable-claims-aware-identity/ http://brockallen.com/2013/01/26/replacing-forms-authentication-with-wifs-session-authentication-module-sam-to-enable-claims-aware-identity/
Other option Enable WCF Authentication Service; 其他选项启用WCF身份验证服务;
1) https://msdn.microsoft.com/library/bb398990 1) https://msdn.microsoft.com/library/bb398990
2) https://msdn.microsoft.com/tr-tr/library/bb398862%28v=vs.100%29.aspx 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 3) https://msdn.microsoft.com/tr-tr/library/bb398778%28v=vs.100%29.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.