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

C# 保存有关联合cookie问题的自定义信息,c#,asp.net-mvc-4,acs,C#,Asp.net Mvc 4,Acs,您好,我正在MVC4应用程序中使用acs联合身份验证。在登录期间,我的应用程序在SSO后通过gmail等完美地进行身份验证并重定向到主页。我想保存其电子邮件ID与数据库记录的电子邮件匹配的用户的代理ID。但问题是,经过一段时间后,代理ID自动删除并声明我添加的位置。在我尝试时,代理ID已删除并显示为空通过此代码从其他页面访问代理Id ((ClaimsIdentity)HttpContext.Current.User.Identity).Claims.Where(c => c.Type.Eq

您好,我正在MVC4应用程序中使用acs联合身份验证。在登录期间,我的应用程序在SSO后通过gmail等完美地进行身份验证并重定向到主页。我想保存其电子邮件ID与数据库记录的电子邮件匹配的用户的代理ID。但问题是,经过一段时间后,代理ID自动删除并声明我添加的位置。在我尝试时,代理ID已删除并显示为空通过此代码从其他页面访问代理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");

    }
[HttpPost]
[验证输入(错误)]
[异名]
公共行动结果签名()
{
UserRoles userObj=null;
客户customerObj=null;
ClaimsIdentity identity=User.identity作为ClaimsIdentity;
var claimCollection=identity.Claims;
Dictionary tempClaims=新字典();
foreach(索赔收集中的索赔)
{
添加(claim.Type,claim.Value);
}
如果(临时)索赔[”http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider“]!=null)
{
string-strintentifier=string.Empty;
如果(临时)索赔[”http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider“]==”uri:WindowsLiveID“)
Stratindifier=tempClaims[”http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"];
其他的
Stratindifier=tempClaims[”http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"];
userObj=repository.Filter(f=>f.EmailId.Equals(strindifier)).FirstOrDefault();
if(userObj!=null)
{
AddClaim(新的声明(ClaimTypes.Role,userObj.Role));
AddClaim(新声明(ClaimTypes.SerialNumber,userObj.AgentID.ToString());
}                
}           
return User.Identity.IsAuthenticated?RedirectToAction(“索引”、“主页”):RedirectToAction(“登录”);
}

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

var principal = new ClaimsPrincipal(identity);
var token = new SessionSecurityToken(principal);
FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(token);

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

看起来您正在扩充内存中的声明集,但不会再次保留在cookie中

有两种选择—要么手动保留新的声明标识,但忘记显示代码,要么不将更改保存到联合cookie(这解释了更改丢失的原因)

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


请更好地解释您的问题。问题是
AgentID
有时为空?实际上问题是在登录期间,添加了一个新声明,例如identity.AddClaim(新声明(ClaimTypes.SerialNumber,userObj.AgentID.ToString());但问题是,一段时间后,这种说法并没有持续下去。