如何坚持匿名用户选择(例如:主题选择) [英] How to persist anon user selection (ex: theme selection)

查看:157
本文介绍了如何坚持匿名用户选择(例如:主题选择)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,暧昧的问题,但在这里,我走了。

Sorry for the ambiguous question but here I go.

在每个页面都显示不同的主题选项的局部视图。这些主题都为页面内元素颜色只是不同的CSS类。我怎样才能让任何访客来选择不同的主题,并将它坚持对所有后续请求和会话,即使用户没有登录。

On each page a have a partial view displaying different theme options. These themes are just different css classes for element colors within the page. How can I allow any visitor to select a different theme, and have it persist on all following requests and sessions, even if the user is not logged in.

我想这一定要做客户端,以及所有我能想到的是像饼干。不幸的是我还没有真正与他们尝试在ASP.NET还和不能认为适当的措辞对自这样一个基本概念的谷歌搜索。

I imagine this must be done client side, and all I could think of is something like cookies. Unfortunately I haven't really experimented with them in ASP.NET yet and cant think of proper wording for a Google search since its such a basic concept.

如果任何人都可以在正确的方向指向我,我会把AP preciate它。谢谢

If anyone can point me in the right direction i'd appreciate it. Thanks

推荐答案

您可以使用被称为资料

You could use a concept known as Profile

通过配置文件,您可以声明自己喜欢暴露给用户的特性和工作原理为匿名用户

With profiles you can declare the properties you would like to expose to your users and this works for anonymous users

基本上的配置文件属性存储在cookie中,因此您可以配置的时候就应该到期,饼干等相关的设置。

Basically the profile properties are stored in cookies, therefore you can configure when they should expire and other cookies-related settings

您的配置文件属性被编译为在顶级项目汇编 - AKA编译生命周期 在ASP.Net,因此它们将通过档案公开为强类型的属性

例如:

<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>

消费在code(Global.asax中在这个例子中)的配置文件

void Application_EndRequest(object sender, EventArgs e)
{
    if (Profile != null)
    {
        Profile.LastVisit = DateTime.Now;
        Profile.Save();
    }
}

此外,ASP.Net,您可以访问属性在JavaScript中,使用Microsoft AJAX组件:

Additionally, ASP.Net lets you access the properties in JavaScript, using Microsoft AJAX components:

<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>

这篇关于如何坚持匿名用户选择(例如:主题选择)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆