您好,我正在MVC4应用程序中使用authentication与表单身份验证。在登录期间,我的应用程序完美地工作,认证和重定向到主页后,通过gmail等单点登录。我想保存用户的代理ID,其电子邮件id与数据库记录的email.But相匹配,问题是在一段时间后,代理id自动删除和索赔,我添加了AgentID删除,并显示空期间,当我试图通过此代码从其他页面访问代理Id。
((ClaimsIdentity)HttpContext.Current.User.Identity).Claims.Where(c => c.Type.Equals(ClaimTypes.Role)).FirstOrDefault();当用户在输入gmail凭据后重定向时,登录方法如下。
[HttpPost]
[ValidateInput(false)]
[AllowAnonymous]
public ActionResult SignIn()
{
UserRoles userObj=null;
Customer customerObj=null;
ClaimsIdentity identity = User.Identity as ClaimsIdentity;
var claimCollection = identity.Claims;
Dictionary<string, string> tempClaims = new Dictionary<string, string>();
foreach (Claim claim in claimCollection)
{
tempClaims.Add(claim.Type, claim.Value);
}
if (tempClaims["http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider"] != null)
{
string strIdentifier = string.Empty;
if (tempClaims["http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider"] == "uri:WindowsLiveID")
strIdentifier = tempClaims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"];
else
strIdentifier = tempClaims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"];
userObj = repository.Filter(f => f.EmailId.Equals(strIdentifier)).FirstOrDefault();
if (userObj != null)
{
identity.AddClaim(new Claim(ClaimTypes.Role, userObj.Role));
identity.AddClaim(new Claim(ClaimTypes.SerialNumber, userObj.AgentID.ToString()));
}
}
return User.Identity.IsAuthenticated ? RedirectToAction("Index", "Home") : RedirectToAction("Login");
}发布于 2013-04-07 18:07:20
您似乎已将AgentID添加为ClaimTypes.SerialNumber声明,但尚未将新主体持久化到联合cookie中:
var principal = new ClaimsPrincipal(identity);
var token = new SessionSecurityToken(principal);
FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(token);WriteSessionTokenToCookie将使用您添加的自定义声明更新联合cookies,以便在后续请求中能够检索它们。
发布于 2013-04-07 18:09:41
看起来您正在扩充内存中的声明集,但是您不再坚持在cookie中。
有两种选择--要么手动保存您的新声明标识,但只是忘记显示代码,要么不保存对联邦cookie的更改(这解释了您的更改丢失的原因)。
看看我的一个教程,我在其中展示了如何从头开始创建claims身份并将其持久化。我希望这能给你一些关于如何处理你的增强身份的想法:
http://netpl.blogspot.com/2012/09/forms-authentication-revisited.html
https://stackoverflow.com/questions/15861152
复制相似问题