您可以使用称为 Profile
的概念
使用配置文件,您可以声明要向用户公开的属性,这适用于匿名用户
基本上,配置文件属性存储在 cookie 中,因此您可以配置它们何时到期以及其他与 cookie 相关的设置
您的配置文件属性在 ASP.Net 中被编译为 top-level items compilation - AKA Compilation Life-Cycle 的一部分,因此它们将通过 Profile 类公开为强类型属性
例如:
Web.config 设置
<configuration>
<system.web>
<anonymousIdentification enabled="true"/>
<profile defaultProvider="AspNetSqlProfileProvider" enabled="true">
<properties>
<add name="FirstName"/>
<add name="LastName"/>
<add allowAnonymous="true" name="LastVisit" type="System.Nullable`1[System.DateTime]"/>
<group name="Address">
<add name="Street"/>
<add name="PC"/>
<add name="InternalNumber" type="System.Int32" defaultValue="0"/>
</group>
<add name="EmployeeInfo" serializeAs="Binary" type="EmployeeInfo"/>
</properties>
</profile>
</system.web>
</configuration>
在代码中使用配置文件(本示例为 Global.asax)
void Application_EndRequest(object sender, EventArgs e)
{
if (Profile != null)
{
Profile.LastVisit = DateTime.Now;
Profile.Save();
}
}
此外,ASP.Net 允许您使用 Microsoft AJAX 组件访问 JavaScript 中的属性:
Web.config
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<profileService enabled="true" readAccessProperties="LastVisit" writeAccessProperties="LastVisit"/>
<jsonSerialization maxJsonLength="102400" recursionLimit="100" />
</webServices>
</scripting>
</system.web.extensions>
</configuration>
ASPX
<script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function () {
$("#profile").click(function () {
Sys.Services.ProfileService.load();
Sys.Services.ProfileService.properties.LastVisit = new Date();
Sys.Services.ProfileService.save(
null,
function (m) {
alert(m);
},
function (e) {
alert(e);
},
null
);
});
Sys.Services.ProfileService.load(null, function (r) {
$("#res").append("<br/>");
$("#res").append(Sys.Services.ProfileService.properties.LastVisit.toString());
$("#res").append("<br/>");
}, function (m) {
$("#res").append(m.get_message());
}, null);
});
</script>
<asp:ScriptManager runat="server" ID="sm">
<AuthenticationService />
<ProfileService />
<RoleService />
</asp:ScriptManager>