Robin's Blog

记录 积累 学习 成长

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

HttpRequest.AnonymousID 属性

获取该用户的匿名标识符(如果存在)。

命名空间:  System.Web
程序集:  System.Web(在 System.Web.dll 中)

AnonymousId()()() 属性将一个长久的唯一标识符分配给一个未经身份验证的用户,该标识符可用于跟踪用户或将配置文件属性分配给该用户,而无需将数据存储在 Session 对象中。默认情况下,使用 Cookie 来跟踪 AnonymousId()()() 属性,但是,当匿名标识配置节中的 Cookieless 属性设置为 UseUriUseDeviceProfileAutoDetect 值时,AnonymousId()()() 也可以设置为使用 URI。如果您不再需要该 Cookie 起作用(例如,在对一个匿名用户进行身份验证时),则必须显式清除它。

在需要标识未经身份验证的实体时,以及在要求进行身份验证时,使用匿名标识。有关更多信息,请参见 anonymousIdentification 元素(ASP.NET 设置架构)

下面的代码示例演示如何通过访问 Global.asax 文件中的 AnonymousIdentification_OnCreate 事件来使用 AnonymousId()()() 属性。此示例包括两个部分:

  • Web 窗体页。

  • 处理 AnonymousIdentification_OnCreate 事件的方法。

代码示例的第一部分说明:如何通过访问 Global.asax 文件中 AnonymousIdentification_OnCreate 事件来使用 AnonymousId()()() 属性。每当创建新的匿名 ID 时都会引发 AnonymousIdentification_OnCreate 事件。

Code

 代码示例的第二部分说明:如何显示由前一个示例中的 AnonymousIdentification_OnCreate 事件创建的新 AnonymousId()()()

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

public void AnonymousIdentification_OnCreate(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;
}

 

 

posted on 2009-07-17 09:43  Robin99  阅读(803)  评论(0)    收藏  举报