WCF服务,并添加自定义的身份声明 [英] WCF service and adding custom Identity claims
问题描述
我创造了一些要求,重视他们的线程,那么我想访问经营合同GetCurrentUser();用户电子邮件要求总是返回null。
问题出在哪里?
WCF使用本地IIS和绑定类型。WsHttpBinding的
< serviceCredentials>
< userNameAuthentication userNamePasswordValidationMode =自定义
customUserNamePasswordValidatorType =SampleService.UserValidator,SampleService/>
< / serviceCredentials>
自定义验证;
公共类UserValidator:UserNamePasswordValidator
{
公共覆盖无效验证(用户名字符串,字符串密码)
{
如果(用户名== NULL ||密码= = NULL)
{
抛出新的ArgumentNullException();
}
如果(用户名密码==)
{
变种索赔=新的List<权利要求GT;
{
新的索赔(ClaimTypes.Name,利群),
新的索赔(ClaimTypes.Emailfoo@bar.com),
新的索赔(ClaimTypes。的作用,客户)
};
VAR ID =新ClaimsIdentity(索赔,样本);
变种本金=新ClaimsPrincipal(ID);
= Thread.CurrentPrincipal中校长;
}
,否则
{
抛出新的FaultException(密码错误......);
}
}
}
和我的OperationContract的;
公共字符串GetCurrentUser()
{
VAR本金= Thread.CurrentPrincipal中;
如果(principal.Identity.IsAuthenticated)
{
变种CP = ClaimsPrincipal.Current;
变种的电子邮件= cp.FindFirst(ClaimTypes.Email).value的; <<<<它总是返回空值:(
返回的String.Format(您的电子邮件为:{0},电子邮件);
}
,否则
{
返回NONE;
}
}
找到了解决办法,所以;
我使用WCF表单的Wif和会话认证模块(SAM)
$ b认证$ b
您可以查找资料和样品;
的 http://brockallen.com/2013/01/26/replacing-forms-authentication -with-wifs会话认证模块-SAM到使-声明感知身份/
其他选项启用WCF身份验证服务;
1) https://msdn.microsoft.com /库/ 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
I created some claims and attach them thread, then I want to access operation contract GetCurrentUser(); user email claim always return null.
Where is the problem?
WCF using Local IIS and binding type is 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)
You can find information and sample;
Other option Enable WCF Authentication Service;
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
这篇关于WCF服务,并添加自定义的身份声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!