SqlProfileProvider 允许将用户配置信息存储到一个 SQL Server 7.0 或者后续版本的数据库中。其实,你可以选择在任何数据库中创建用户配置表,但是,你无法修改其他任何的数据库架构。你只能操作指定的表名、列名、序列化方式。
你需要完成下面这些步骤使用用户配置:
- 创建用户配置表(SQL Server Express 版本,这步会自动发生)
- 配置用户配置提供程序
- 定义一些用户配置属性
- 激活验证功能
- 在网页代码中使用用户配置
1. 创建用户配置表
如果没有使用 SQL Server Express ,那么必须手动创建用户配置表。需要使用带有 -A p 命令行选项的 aspnet_regsql.exe 工具,需要你提供的信息是:服务器地址(-S)、数据库(-d)、连接数据的验证信息(-U 用户名 -P 密码)或者使用当前 Windows 帐号(-E)。
这是一个使用当前 Windows 帐号登录到数据库,然后在当前机器上创建默认的 aspnetdb 数据库的例子:
aspnet_regsql.exe -A p -E
用户配置使用的数据库
| aspnet_Applications | 列出数据库中具有记录的所有网页程序。几个 ASP.NET 程序使用同一个 aspntdb 数据库是可用的,因此,需要将用户信息加以区分,这样对于每一个应用程序来讲用户配置都是不同的。注册用户配置提供程序时可分别设置不同的程序名。或者共享用户配置(设置相同的程序名) |
| aspnet_Profile | 存储指定用户的用户配置信息。每个记录包含一个单独用户的完整配置信息。PropertyNames 字段列出属性的名称,PropertyValuesString 和 PropertyValuesBinary 字段列出所有属性的数据。每天记录还包含了最后更新的日期和时间 LastUpdateDate |
| aspnet_SchemaVersions | 列出存储用户配置信息的被支持的架构。 |
| aspnet_Users | 列出了用户名,并将其映射到 aspnet_Applications 表中的某个程序上。同时记录了最后请求的日期和时间(LastActivityDate)和这条记录是否属于某个匿名用户自动创建的(IsAnonymous) |
用户配置使用的数据库存储过程
| aspnet_Applications_CreateApplications | 检查 aspnet_Applications 表中是否存在指定程序名,如果需要就创建它 |
| aspnet_CheckSchemaVersion | 使用 aspnet_SchemaVersions 表检查指定架构版本是否支持某个功能(比如用户配置) |
| aspnet_Profile_GetProfiles | 从 aspnet_Profile 表中获取某个指定 Web 程序的所有用户配置记录的用户名和更新时间,但不会返回实际的用户配置数据 |
| aspnet_Profile_GetProperties | 获得指定用户名的用户配置信息。这个信息不会经过任何的分析处理。仅仅是返回底层的 4 个字段 |
| aspnet_Profile_SetProperties | 为指定的用户设置用户配置信息。需要指定 3 个字段的值。用户配置中,没有任何方法可单独更新一个属性 |
| aspnet_Profile_GetNumberOfInactiveProfiles | 返回指定的时间窗口内没有使用的用户配置记录 |
| aspnet_Profile_DeleteInactiveProfiles | 移除指定的时间窗口内没有使用的用户配置记录 |
| aspnet_Users_CreateUser | 为特定用户在 aspnet_Users 表中创建一条新记录。 |
| aspnet_Users_DeleteUser | 从 aspnet_Users 表中移除一个指定的用户 |
2. 配置提供程序
有了数据库,可以通过 web.config 文件注册 SqlProfileProvider 了:配置一个连接字符串;在 <profile> 节中移除所有现存提供程序(使用 <clear>);添加一个新的 System.Web.Profile.SqlProfileProvider 实例:
>
<connectionStrings>
<add name="SqlServices" connectionString="data source=localhost;integrated security=true;catalog=aspnetdb;"/>
</connectionStrings>
<system.web>
<profile defaultProvider="">
<providers>
<clear />
<add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider"
connectionStringName="SqlServices" applicationName="TestApplication"/>
</providers>
</profile>
</system.web>
</configuration>