Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET 2.0配置文件-如何防止为不使用';你不接受饼干吗?_Asp.net_Cookies_Asp.net Profiles - Fatal编程技术网

ASP.NET 2.0配置文件-如何防止为不使用';你不接受饼干吗?

ASP.NET 2.0配置文件-如何防止为不使用';你不接受饼干吗?,asp.net,cookies,asp.net-profiles,Asp.net,Cookies,Asp.net Profiles,我正在使用带有allowAnonymous=“true”的ASP.NET配置文件。我没有使用ASP.NET成员资格。我最近仔细查看了aspnetdb数据库,发现尽管我的站点每天有600-800个独立访问者,但数据库中有4000-5000个“用户” 显然,这里发生的事情是,禁用cookie的用户最终会为每个请求创建一个记录 我的问题:如果客户端不支持cookie或已禁用cookie,如何防止创建用户和配置文件数据库记录?经过大量研究和尝试,我找到了解决此问题的方法。但是,此解决方案不适用于用户发出

我正在使用带有allowAnonymous=“true”的ASP.NET配置文件。我没有使用ASP.NET成员资格。我最近仔细查看了aspnetdb数据库,发现尽管我的站点每天有600-800个独立访问者,但数据库中有4000-5000个“用户”

显然,这里发生的事情是,禁用cookie的用户最终会为每个请求创建一个记录


我的问题:如果客户端不支持cookie或已禁用cookie,如何防止创建用户和配置文件数据库记录?

经过大量研究和尝试,我找到了解决此问题的方法。但是,此解决方案不适用于用户发出的第一个请求,因为无法确定用户是否只支持一个请求中的cookie

我提出了一个聪明的解决方案,它使用AJAX向服务器发出第二个(不可见)请求,以便在第一个请求时将设置写入概要文件,但前提是启用了cookie和JavaScript。当然,如果您没有在初始请求中存储任何配置文件设置,则无需使用此选项。有关完整的详细信息,请参阅我的

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
简单地说,如果配置文件是匿名的,并且还没有从浏览器接收到匿名cookie,则该类阻止将配置文件写入或保存

要使用该类,请将其放入App_Code目录,并向web.config文件中的profile元素添加一个“inherits”属性,如下所示:

<profile defaultProvider="SqlProvider" inherits="AnonymousProfile">
如果用户拒绝匿名cookie,则不需要运行这些代码,但由于新的概要文件类仍将进行检查,因此严格来说并不需要运行这些代码,这样可以节省一些CPU周期

此示例中还包含另一个bug修复程序-如果将属性设置为其已包含的相同值,则原始概要文件类将是“脏的”,这将导致它更新数据库,即使没有任何更改。在修复此问题的重载SetPropertyValue方法中检查此条件

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

参考资料:

经过大量研究和反复试验,我找到了解决这个问题的方法。但是,此解决方案不适用于用户发出的第一个请求,因为无法确定用户是否只支持一个请求中的cookie

我提出了一个聪明的解决方案,它使用AJAX向服务器发出第二个(不可见)请求,以便在第一个请求时将设置写入概要文件,但前提是启用了cookie和JavaScript。当然,如果您没有在初始请求中存储任何配置文件设置,则无需使用此选项。有关完整的详细信息,请参阅我的

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
简单地说,如果配置文件是匿名的,并且还没有从浏览器接收到匿名cookie,则该类阻止将配置文件写入或保存

要使用该类,请将其放入App_Code目录,并向web.config文件中的profile元素添加一个“inherits”属性,如下所示:

<profile defaultProvider="SqlProvider" inherits="AnonymousProfile">
如果用户拒绝匿名cookie,则不需要运行这些代码,但由于新的概要文件类仍将进行检查,因此严格来说并不需要运行这些代码,这样可以节省一些CPU周期

此示例中还包含另一个bug修复程序-如果将属性设置为其已包含的相同值,则原始概要文件类将是“脏的”,这将导致它更新数据库,即使没有任何更改。在修复此问题的重载SetPropertyValue方法中检查此条件

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
参考: