ProviderInfo 对象中的匿名用户名应该是什么样子?

发布于 2024-12-23 17:09:15 字数 331 浏览 1 评论 0原文

根据 Microsoft 的 ProfileInfo 定义 http://msdn.microsoft.com /en-us/library/system.web.profile.profileinfo.aspx,未经身份验证的 profileinfo 对象具有用户名;当然,这必须在给定会话中保留/引用配置文件信息(我假设它是基于会话的)。我猜这是一些指导或其他东西,但我看不到它是在哪里定义、创建、跟踪等的。有人能指出我正确的方向吗?

Per Microsoft's ProfileInfo definition http://msdn.microsoft.com/en-us/library/system.web.profile.profileinfo.aspx, an unauthenticated profileinfo object has a username; naturally this must be keyed off of to persist/ reference profile information in a given session (I am assuming it is session-based). I'm guessing this is some guid or something, but I don't see where this is defined, created, tracked, etc. Can someone point me in the right direction?

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

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

发布评论

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

评论(1

网白 2024-12-30 17:09:15

嗯...这个问题引起了我的兴趣,所以我决定做一些研究。

通过对文档进行一些挖掘,我首先实现配置文件提供程序 MSDN文章,我发现了以下内容:

GetPropertyValues 方法

将 SettingsContext 和 SettingsPropertyCollection 作为输入
对象。

SettingsContext 提供有关用户的信息。您可以使用
作为检索配置文件属性的主键的信息
为用户提供的信息。使用SettingsContext对象来获取
用户名以及用户是经过身份验证的还是匿名的。
...

因此,用户是否通过身份验证的确定通常是在更高级别上完成的。不管怎样,我查看了微软默认的SqlProfileProvider实现的代码(即GetPropertyValues方法实现),发现它调用了private void GetPropertyValuesFromDatabase(string userName, SettingsPropertyValueCollection svc) 实际上有以下代码:

HttpContext context = HttpContext.Current;
...
string sName = null;

if (context != null) 
    sName = (context.Request.IsAuthenticated ? context.User.Identity.Name : context.Request.AnonymousID);

因此,如果我们有一个未经身份验证的请求,则从以下位置获取用户 ID HttpContext.Current.Request.AnonymousID 属性。通过 MSDN 搜索此属性显示以下页面: HttpRequest .AnonymousID 属性 (System.Web)。虽然它仍然没有描述生成此 ID 的确切算法,但它提供了有关如何根据需要覆盖此默认算法的信息。您所需要做的就是在 Web 应用程序中重载 public void AnonymousIdentification_Creating(Object sender, AnonymousIdentificationEventArgs e) 方法。此页面还提供了有关如何在调用之间保留 AnonymousID 的一些信息(默认情况下,它存储在 .ASPXANONYMOUS cookie 中)。

示例代码:

void Application_Start(Object sender, EventArgs e)
    {
        // Initialize user count property
        Application["UserCount"] = 0;
    }

public void AnonymousIdentification_Creating(Object sender, AnonymousIdentificationEventArgs e)
    {
    // Change the anonymous id
    e.AnonymousID = "mysite.com_Anonymous_User_" + DateTime.Now.Ticks;

    // Increment count of unique anonymous users
    Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString()) + 1;
}

摘要: 我无法回答您关于默认情况下如何创建此 ID 的原始问题,但我认为最后一个代码片段足以让您使用您想要的任何算法覆盖它。

Well... The question interested me so I've decided to do some research.

A bit of digging in documentation lead me first to Implementing a Profile Provider MSDN article, where I've found the following:

GetPropertyValues method

Takes as input a SettingsContext and a SettingsPropertyCollection
object.

The SettingsContext provides information about the user. You can use
the information as a primary key to retrieve profile property
information for the user. Use the SettingsContext object to get the
user name and whether the user is authenticated or anonymous.
...

So, the determination of whether user is authenticated or not is generally done on higher level. Anyway, I took a look at code of Microsoft's default SqlProfileProvider implementation (namely, GetPropertyValues method implementation) and found out that it calls method private void GetPropertyValuesFromDatabase(string userName, SettingsPropertyValueCollection svc) which actually has the following code:

HttpContext context = HttpContext.Current;
...
string sName = null;

if (context != null) 
    sName = (context.Request.IsAuthenticated ? context.User.Identity.Name : context.Request.AnonymousID);

So, if we have a non-authenticated request then a user id is taken from HttpContext.Current.Request.AnonymousID property. Searching through MSDN for this property has revealed the following page: HttpRequest.AnonymousID property (System.Web). Although it still does not describe exact algorithm of generating this ID, but it provides information on how you can override this default algorithm if you want. All you need is to overload public void AnonymousIdentification_Creating(Object sender, AnonymousIdentificationEventArgs e) method in your web application. Also this page provides some information on how AnonymousID is persisted between calls (by default it's stored in .ASPXANONYMOUS cookie).

Example code:

void Application_Start(Object sender, EventArgs e)
    {
        // Initialize user count property
        Application["UserCount"] = 0;
    }

public void AnonymousIdentification_Creating(Object sender, AnonymousIdentificationEventArgs e)
    {
    // Change the anonymous id
    e.AnonymousID = "mysite.com_Anonymous_User_" + DateTime.Now.Ticks;

    // Increment count of unique anonymous users
    Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString()) + 1;
}

Summary: I have not been able to answer your original question on HOW this ID is created by default but I think that last code snippet will be enough for you to override this with any algorithm you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文