如何从 cookie ASPXANONYMOUS 获取 AnonymousID?

发布于 2024-08-25 15:51:10 字数 278 浏览 6 评论 0原文

如果我查看我的 cookievalue .ASPXANONYMOUS,它是一个字符串,即

WZnX-rXHygEkAAAAOTFhZjE5YTctZmEzZi00MTMwLWEwNTAtYjYwMzI0N2M0NTY4gQUsRlThiJWAjBgmBnpeIba7eGo1

值 Request.AnonymousID 是一个 Guid。

如何从 ASPXANONYMOUS 转到 AnonymousID ?

我需要它来调试 FormsAuthentication 的一些问题。

If I have a look at my cookievalue .ASPXANONYMOUS it is a string ie

WZnX-rXHygEkAAAAOTFhZjE5YTctZmEzZi00MTMwLWEwNTAtYjYwMzI0N2M0NTY4gQUsRlThiJWAjBgmBnpeIba7eGo1

The value Request.AnonymousID is a Guid.

How do you get from ASPXANONYMOUS to AnonymousID ?

I need this to debug some issues I have with FormsAuthentication.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2024-09-01 15:51:10

是的,匿名 ID 就是 GUID。 cookie 字符串是包含 id 和其他数据的加密值:

[Serializable]
internal class AnonymousIdData
{
    internal string AnonymousId;
    internal DateTime ExpireDate;

    internal AnonymousIdData(string id, DateTime dt);
}

默认情况下,匿名 cookie 的有效期为 90 天,并且每次访问都会刷新。

当Request.IsAuthenticated==false时,可以将Request.AnonymousID视为请求用户名。

请参阅 AnonymousIdentificationModule

更新:
作为对评论的回应,是的,您可以解码该值,但为什么呢?

string aId = Request.AnonymousID;

string anonCookieValue = Request.Cookies[".ASPXANONYMOUS"].Value;
MethodInfo method = typeof(AnonymousIdentificationModule).GetMethod("GetDecodedValue", BindingFlags.Static | BindingFlags.NonPublic);
object anonymousIdData = method.Invoke(null, new object[] { anonCookieValue });
var field = anonymousIdData.GetType().GetField("AnonymousId", BindingFlags.Instance | BindingFlags.NonPublic);
string anonymousId = (string) field.GetValue(anonymousIdData);
field = anonymousIdData.GetType().GetField("ExpireDate", BindingFlags.Instance | BindingFlags.NonPublic);
DateTime expired = (DateTime) field.GetValue(anonymousIdData);

// why? just use Request.AnonymousID    
Debug.Assert(aId == anonymousId);

Yes, an anonymous id is a GUID. The cookie string is an encrypted value containing the id and other data:

[Serializable]
internal class AnonymousIdData
{
    internal string AnonymousId;
    internal DateTime ExpireDate;

    internal AnonymousIdData(string id, DateTime dt);
}

By default, anonymous cookies are valid for 90 days and are refreshed every visit.

You can treat Request.AnonymousID as the request username when Request.IsAuthenticated==false.

see AnonymousIdentificationModule

UPDATE:
In response to a comment, yes, you can decode the value, but why?

string aId = Request.AnonymousID;

string anonCookieValue = Request.Cookies[".ASPXANONYMOUS"].Value;
MethodInfo method = typeof(AnonymousIdentificationModule).GetMethod("GetDecodedValue", BindingFlags.Static | BindingFlags.NonPublic);
object anonymousIdData = method.Invoke(null, new object[] { anonCookieValue });
var field = anonymousIdData.GetType().GetField("AnonymousId", BindingFlags.Instance | BindingFlags.NonPublic);
string anonymousId = (string) field.GetValue(anonymousIdData);
field = anonymousIdData.GetType().GetField("ExpireDate", BindingFlags.Instance | BindingFlags.NonPublic);
DateTime expired = (DateTime) field.GetValue(anonymousIdData);

// why? just use Request.AnonymousID    
Debug.Assert(aId == anonymousId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文