ASP.NET 状态管理之配置文件属性,首先需要强调的是,这里所描述的配置文件属性相关概念为: 1:配置文件属性是在web.config中定义的属性,是在system.web节点profile定义,如: <system.web> <!--启?用®?匿?名?--> <anonymousIdentification enabled="true"/> <!--个?性?化¡¥设¦¨¨计?区?域®¨°--> <profile> <properties> <!--每?项?都?需¨¨要°a配?置?激¡è活?--> <add name="StudentName" type="System.String" allowAnonymous="true"/> <add name="StudentAge" type="System.Int" allowAnonymous="true"/> </properties> </profile> </system.web>
2:其中的属性的真实值可以存储数据库中或其它地方。它是运行时的HttpContext 的 Profile 属性。 ASP.NET 配置文件功能将信息与单个用户关联,并采用持久性的格式存储这些信息。通过配置文件,可以管理用户信息,而无需创建和维护自己的数据库。此外,通过使用可从应用程序中的任何位置访问的强类型 API,就可以借助 ASP.NET 配置文件功能使用相关的用户信息。 可以使用配置文件存储任何类型的对象。配置文件功能提供了一项通用存储功能,使您能够定义和维护几乎任何类型的数据,同时仍可用类型安全的方式使用数据。 若要使用配置文件,首先应通过修改 ASP.NET Web 应用程序的配置文件来启用配置文件。要在配置中指定配置文件提供程序,该提供程序是执行存储和检索配置文件数据等低级任务的基础类。可以使用 .NET Framework 中包括的配置文件提供程序(会将配置文件数据存储在 SQL Server 中),也可以按照 实现配置文件提供程序 主题中描述的那样创建并使用自己的配置文件提供程序。可以指定连接到所选的数据库的 SqlProfileProvider 实例,也可以使用将配置文件数据存储在本地 Web 服务器上的默认 SqlProfileProvider 实例。
1.配置文件属性的用户标识 1.通过身份验证的用户 ASP.NET Forms 身份验证系统,该系统在身份验证成功之后设置用户标识。 1.匿名用户 如果启用了匿名标识,则用户首次访问站点时,ASP.NET 将为其创建一个唯一标识。该唯一用户标识存储在用户计算机上的 Cookie 中,这样,对于每个页请求,其用户都可以得到标识。Cookie 的默认有效期设置为大约 70 天,当用户访问站点时会定期对其进行更新。如果用户的计算机不接受 Cookie,则可将该用户的标识作为请求的页 URL 的一部分来维护(尽管用户关闭浏览器时该标识会丢失)。 有关启用匿名标识的信息,请参见 anonymousIdentification 元素(ASP.NET 设置架构)。 当用户登录(即不再是匿名用户)时,将引发 MigrateAnonymous 事件。如果有必要,可以对此事件进行处理,以便将信息从用户的匿名标识迁移到新的通过身份验证的标识。下面的代码示例演示用户通过身份验证时如何迁移信息。
public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args) { ProfileCommon anonymousProfile = Profile.GetProfile(args.AnonymousID);
Profile.ZipCode = anonymousProfile.ZipCode; Profile.CityAndState = anonymousProfile.CityAndState; Profile.StockSymbols = anonymousProfile.StockSymbols;
//////// // Delete the anonymous profile. If the anonymous ID is not // needed in the rest of the site, remove the anonymous cookie.
ProfileManager.DeleteProfile(args.AnonymousID); AnonymousIdentificationModule.ClearAnonymousIdentifier();
// Delete the user row that was created for the anonymous user. Membership.DeleteUser(args.AnonymousID, true);
}
1.定义配置文件属性 type 指定属性的类型。默认为 String。可以将任何 .NET 类指定为类型(Int32、DateTime、StringCollection 等)。如果 .NET Framework 中没有定义该类型,则必须确保 Web 应用程序可以访问该类型。可以在网站的 Bin 目录中或全局程序集缓存 (GAC) 中包含该类型编译后的程序集,也可以将该类型的源代码放入网站的 App_Code 目录中。 [Serializable] public class ShoppingCart { public DateTime Created; public DateTime LastUpdated; public Dictionary<string, CartItem> CartItems = new Dictionary<string, CartItem>(); }
[Serializable] public class CartItem { public CartItem(int itemId, string itemName, double itemCost) { ID = itemId; Name = itemName; Cost = itemCost; }
int ID; string Name; double Cost; } 若要配置用户配置文件以使用此类的存储实例,请向应用程序的 Web.config 文件中添加以下内容: <profile defaultProvider="AspNetSqlProfileProvider"> <properties> <add name="MyCart" type="Samples.AspNet.Profile.ShoppingCart" serializeAs="Binary" /> </properties> </profile> 若要将自定义类型数据存储到用户配置文件中,如同在任何应用程序中一样,创建自定义类型的一个实例,然后将该实例分配给为该类型定义的配置文件属性。下面的代码示例演示如何使用一个以自定义类型创建的配置文件属性: ShoppingCart bookCart = new ShoppingCart(); bookCart.CartItems.Add("Contoso", new CartItem(37843, "Widget", 49.99)); bookCart.CartItems.Add("Microsoft", new CartItem(39232, "Software", 49.99)); Profile.MyCart = bookCart;
1.ASP.NET 配置文件提供程序 一般情况下,不建议使用默认。 1.自定义配置文件提供程序 需要在 .NET Framework 包含的配置文件提供程序不支持的数据源中存储配置文件信息,如在 FoxPro 数据库或在 Oracle 数据库中。 1.将配置文件信息与 ASP.NET AJAX 一起使用 |