首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保存有关联合cookie问题的自定义信息

保存有关联合cookie问题的自定义信息
EN

Stack Overflow用户
提问于 2013-04-07 18:01:13
回答 2查看 972关注 0票数 2

您好,我正在MVC4应用程序中使用authentication与表单身份验证。在登录期间,我的应用程序完美地工作,认证和重定向到主页后,通过gmail等单点登录。我想保存用户的代理ID,其电子邮件id与数据库记录的email.But相匹配,问题是在一段时间后,代理id自动删除和索赔,我添加了AgentID删除,并显示空期间,当我试图通过此代码从其他页面访问代理Id。

代码语言:javascript
复制
((ClaimsIdentity)HttpContext.Current.User.Identity).Claims.Where(c => c.Type.Equals(ClaimTypes.Role)).FirstOrDefault();

当用户在输入gmail凭据后重定向时,登录方法如下。

代码语言:javascript
复制
     [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");

    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-07 18:07:20

您似乎已将AgentID添加为ClaimTypes.SerialNumber声明,但尚未将新主体持久化到联合cookie中:

代码语言:javascript
复制
var principal = new ClaimsPrincipal(identity);
var token = new SessionSecurityToken(principal);
FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(token);

WriteSessionTokenToCookie将使用您添加的自定义声明更新联合cookies,以便在后续请求中能够检索它们。

票数 2
EN

Stack Overflow用户

发布于 2013-04-07 18:09:41

看起来您正在扩充内存中的声明集,但是您不再坚持在cookie中。

有两种选择--要么手动保存您的新声明标识,但只是忘记显示代码,要么不保存对联邦cookie的更改(这解释了您的更改丢失的原因)。

看看我的一个教程,我在其中展示了如何从头开始创建claims身份并将其持久化。我希望这能给你一些关于如何处理你的增强身份的想法:

http://netpl.blogspot.com/2012/09/forms-authentication-revisited.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15861152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档