Csharp/C#教程:WCF服务和添加自定义Identity声明分享


WCF服务和添加自定义Identity声明

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

问题出在哪儿?

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

   

自定义validation器;

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

和我的行动合同;

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

找到了解决方案,所以;

我在Wif和会话认证模块(SAM)中使用了WCF Forms身份validation

你可以找到信息和样本;

Replacing forms authentication with WIF’s session authentication module (SAM) to enable claims aware identity

其他选项启用WCF身份validation服务;

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

上述就是C#学习教程:WCF服务和添加自定义Identity声明分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1023158.html

(0)
上一篇 2022年1月7日
下一篇 2022年1月7日

精彩推荐