【问题标题】:Federated Authentication - two cookies, first cookie has closing xml tag联合身份验证 - 两个 cookie,第一个 cookie 具有关闭 xml 标记
【发布时间】:2016-07-29 14:35:33
【问题描述】:

我正在使用代码 FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(token); 为网站创建身份验证 cookie

令牌相当大,所以 cookie 被分成两个 cookie。 99% 的情况下一切正常,以下是成功登录后的两个 cookie 的示例,这些 cookie 已被 Base64 解码:

网站验证:

<?xml version="1.0" encoding="utf-8"?><SecurityContextTokenp1:Id="_e00ce4ab-> 2439-48d3-a1cd-f6a31180d02f-B99934A3DBEDB9B3EA191AB595FA8011" xmlns:p1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512"><Identifier>urn:uuid:adbfc4e1-c4a1-4882-9980-aa59431cdf48</Identifier><Cookie xmlns="http://schemas.microsoft.com/ws/2006/05/security">ENCRYPTED_COOKIE_VALUE

WebSiteAuth1:

ENCRYPTED_COOKIE_VALUE</Cookie></SecurityContextToken>

但有时用户会遇到以下错误:

异常信息: 异常类型:FormatException 异常消息:输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符,或 填充字符中的非法字符。在 System.Convert.FromBase64_Decode(Char* startInputPtr, Int32 inputLength, Byte* startDestPtr, Int32 destLength) 在 System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
在 System.Convert.FromBase64String(String s) 在 System.IdentityModel.Services.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken& sessionToken) 在 System.IdentityModel.Services.SessionAuthenticationModule.OnAuthenticateRequest(对象 发件人,EventArgs eventArgs)在 System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤, Boolean & completedSynchronously)

我在引发错误时记录了用户的 cookie,这是我对它们进行 Base64 解码后的 cookie 的样子。

网站验证:

<?xml version="1.0" encoding="utf-8"?><SecurityContextToken p1:Id="_3518f851-bbec-4bb3-b7bb-c4c9bd9165e2-978AD0895E2683747B7CAFF4F1C7131B" xmlns:p1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512"><Identifier>urn:uuid:dd9a6856-9bd1-486c-9f5c-e980fbcc3b02</Identifier><Cookie xmlns="http://schemas.microsoft.com/ws/2006/05/security">ENCRYPTED_COOKIE_VALUE</Cookie></SecurityContextToken>

WebSiteAuth1:

ENCRYPTED_COOKIE_VALUE</Cookie></SecurityContextToken>

如您所见,不同之处在于第一个 cookie 有结束标签 &lt;/Cookie&gt;&lt;/SecurityContextToken&gt;,它不应该在那里,因为 xml 在第二个 cookie 中是关闭的。

我认为这是导致错误的原因。

有人遇到过这个问题吗?或者有什么想法可以解决这个问题?

【问题讨论】:

    标签: c# asp.net-mvc-4


    【解决方案1】:

    我的解决方案是减小 cookie 的大小。

    SessionSecurityToken 上有一个设置,称为 IsReferenceMode。所以我把它设置为真的。这意味着 cookie 存储在服务器上,并且仅对该“服务器 cookie”的引用存储在用户的计算机上。这意味着 cookie 小得多,并且不会分成两个 cookie,这避免了我遇到的第一个 cookie 块有时随机包含关闭 xml 标记的问题。

    这种方法的缺点是,当应用程序池重新启动时,客户会丢失他们的 cookie,即使他们设置 cookie 应该是持久的。为了解决这个问题,我能够从 SessionSecurityTokenCache 类继承并覆盖 AddOrUpdate、Get 和 Remove 方法以将数据库用作备份存储,因此即使会话被清除也可以检索令牌。

    我调整了 thinktecture 模型,这里是: https://github.com/identitymodel/Thinktecture.IdentityModel

    这里有一个很好的博客解释了基础知识: https://brockallen.com/2013/02/21/server-side-session-token-caching-in-wif-and-thinktecture-identitymodel/

    【讨论】:

      猜你喜欢
      • 2011-09-02
      • 1970-01-01
      • 2020-01-07
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 2022-11-08
      • 2019-04-28
      相关资源
      最近更新 更多