ASP.NET 2.0概况 - 我如何prevent记录从谁不接受Cookie的用户正在创建? [英] ASP.NET 2.0 Profiles - How do I prevent records from being created for users who don't accept cookies?

查看:173
本文介绍了ASP.NET 2.0概况 - 我如何prevent记录从谁不接受Cookie的用户正在创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET配置文件与使用AllowAnonymous =真。我不使用ASP.NET成员资格。我最近花了很难看的aspnetdb的数据库,并注意到,虽然我的网站每天获得600-800独立访问者,有4000-5000用户,在数据库中创建。

I am using ASP.NET profiles with allowAnonymous="true". I am NOT using ASP.NET membership. I recently took a hard look at the aspnetdb database and noticed that although my site gets 600-800 unique visitors daily, there are 4000-5000 "users" being created in the database.

显然,这里发生了什么是饼干禁用落得为每个请求创建记录的用户。

Clearly what is happening here is the users with cookies disabled end up creating a record for every request.

我的问题:如何从如果客户端不支持cookie,或者有残疾他们正在创建prevent用户和个人资料数据库记录

My question: How do I prevent user and profile database records from being created if the client doesn't support cookies or has them disabled?

推荐答案

大量的调查研究和反复试验后,我发现了一个解决这个问题。然而,这种解决方案不为该用户进行,因为它不能确定,如果用户从只有一个请求支持Cookie第一请求工作。

After a lot of research and trial and error, I discovered a solution to this issue. However, this solution does not work for the first request that the user makes because it is impossible to determine if a user supports cookies from only one request.

我想出了一个使用AJAX进行第二次(不可见)请求到服务器,以写入设置第一个请求配置文件的聪明的解决方案,但前提是饼干和JavaScript已启用。当然,如果你不是在初始请求存储任何配置文件设置,这是没有必要使用这个。看到我的<一个href=\"http://stackoverflow.com/questions/3884840/are-there-more-efficient-alternatives-to-asp-net-2-0-anonymous-profiles\">other帖子完整的详细信息。

I came up with a clever solution that uses AJAX to make a second (invisible) request to the server in order to write settings to the profile on the first request, but only if cookies and JavaScript are enabled. Of course, if you are not storing any profile settings on the initial request, it is not necessary to use this. See my other post for complete details.

Imports System.Web.Configuration

Public MustInherit Class AnonymousProfile
    Inherits ProfileBase

    Public Shared ReadOnly Property AnonymousCookieName() As String
        Get
            Dim anon As AnonymousIdentificationSection = CType(ConfigurationManager.GetSection("system.web/anonymousIdentification"), AnonymousIdentificationSection)
            Return anon.CookieName
        End Get
    End Property

    Public Shared ReadOnly Property IsAnonymousCookieStored() As Boolean
        Get
            Dim cookies As String = HttpContext.Current.Request.Headers("Cookie")
            Return Not String.IsNullOrEmpty(cookies) AndAlso cookies.IndexOf(AnonymousCookieName) > -1
        End Get
    End Property

    Private ReadOnly Property IsWritable() As Boolean
        Get
            Return IsAnonymous = False OrElse IsAnonymous AndAlso IsAnonymousCookieStored
        End Get
    End Property

    Private Function IsObjectMatch(ByVal obj1 As Object, ByVal obj2 As Object) As Boolean
        If Not obj1 Is Nothing Then
            Return obj1.Equals(obj2)
        Else
            If obj2 Is Nothing Then
                Return True
            Else
                Return False
            End If
        End If
    End Function

    Public Overloads Sub SetPropertyValue(ByVal propertyName As String, ByVal propertyValue As Object)
        If (Not IsObjectMatch(Me.GetPropertyValue(propertyName), propertyValue)) AndAlso IsWritable Then
            MyBase.SetPropertyValue(propertyName, propertyValue)
        End If
    End Sub

    Public Overrides Sub Save()
        If IsWritable Then
            MyBase.Save()
        End If
    End Sub

End Class

简单地说,这个类prevents被写入或保存配置文件如果配置文件是匿名的,匿名的cookie尚未从浏览器尚未收到。

Simply put, this class prevents the profile from being written to or saved if the profile is anonymous and the anonymous cookie hasn't been received from the browser yet.

为了使用这个类,把它放到APP_ code目录,并添加如下的继承属性在web.config文件的配置文件元素:

In order to use this class, put it into the App_Code directory and add an "inherits" attribute to the profile element in the web.config file as follows:

<profile defaultProvider="SqlProvider" inherits="AnonymousProfile">

然后

ASP.NET会从这个时候自动生成它继承ProfileCommon类。

ASP.NET will then inherit the ProfileCommon class from this one when it is automatically generated.

注意AnonymousCookieName和IsAnonymousCookieStored性能进行了公开共享,使他们能够在其他地方使用的项目是这样的:

Note that the AnonymousCookieName and IsAnonymousCookieStored properties were made public shared so they can be used anywhere else in the project like this:

If AnonymousProfile.IsAnonymousCookieStored Then
   'It is safe to write to the profile here
End If

这可以节省从执行code。如果用户拒绝匿名的cookie,这并不需要运行一些CPU周期,但不是严格必要的,因为新的配置类将反正检查。

This can save some CPU cycles from executing code that doesn't need to run if the user rejected the anonymous cookie, but isn't strictly necessary because the new profile class will check anyway.

也有另一种错误修复包括在此示例中 - 原始配置文件类将是脏,如果你设置一个属性来它已经包含相同的值,这将导致它更新,即使没有任何改变数据库。有在其中修复此问题重载SetPropertyValue方法这种情况的检查。

There is also another bug fix included in this sample - the original profile class will be "Dirty" if you set a property to the same value it already contains, which will cause it to update the database even though nothing has changed. There is a check for this condition in the overloaded SetPropertyValue method which fixes this problem.

Public Overloads Sub SetPropertyValue(ByVal propertyName As String, ByVal propertyValue As Object)
    If (Not IsObjectMatch(Me.GetPropertyValue(propertyName), propertyValue)) AndAlso IsWritable Then
        MyBase.SetPropertyValue(propertyName, propertyValue)
    End If
End Sub

参考: ProfileBase类(MSDN)

这篇关于ASP.NET 2.0概况 - 我如何prevent记录从谁不接受Cookie的用户正在创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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