【问题标题】:VB.Net MasterPage Page.PreLoad Property not found找不到 VB.Net MasterPage Page.PreLoad 属性
【发布时间】:2019-01-31 11:05:50
【问题描述】:

我正在尝试使用 Microsoft .Net ViewStateUserKey 和 Double Submit Cookie 实现跨站点请求伪造 (CSRF)。更多信息请访问link

上面的代码是用 C# 编写的,我正在将它转换成 VB.Net。现在的问题是,在这段代码中有一行

Page.PreLoad += master_Page_PreLoad;

当我尝试在 VB.Net 中转换同一行时,它没有找到任何此类事件 Page.PreLoad

请帮助我该怎么做。

谢谢

【问题讨论】:

  • 解决了吗?遇到同样的问题...

标签: c# vb.net cookies csrf master-pages


【解决方案1】:

转换为 VB 的 C# MasterPage 模板如下所示:

Partial Class MasterPage
    Inherits System.Web.UI.MasterPage

    Private Const AntiXsrfTokenKey As String = "__AntiXsrfToken"
    Private Const AntiXsrfUserNameKey As String = "__AntiXsrfUserName"
    Private _antiXsrfTokenValue As String

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
        Dim requestCookie = Request.Cookies(AntiXsrfTokenKey)
        Dim requestCookieGuidValue As Guid

        If requestCookie IsNot Nothing AndAlso Guid.TryParse(requestCookie.Value, requestCookieGuidValue) Then
            _antiXsrfTokenValue = requestCookie.Value
            Page.ViewStateUserKey = _antiXsrfTokenValue
        Else
            _antiXsrfTokenValue = Guid.NewGuid().ToString("N")
            Page.ViewStateUserKey = _antiXsrfTokenValue
            Dim responseCookie = New HttpCookie(AntiXsrfTokenKey) With {
                .HttpOnly = True,
                .Value = _antiXsrfTokenValue
            }

            If FormsAuthentication.RequireSSL AndAlso Request.IsSecureConnection Then
                responseCookie.Secure = True
            End If

            Response.Cookies.[Set](responseCookie)
        End If

        AddHandler Page.PreLoad, AddressOf master_Page_PreLoad
    End Sub

    Protected Sub master_Page_PreLoad(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            ViewState(AntiXsrfTokenKey) = Page.ViewStateUserKey
            ViewState(AntiXsrfUserNameKey) = If(Context.User.Identity.Name, String.Empty)
        Else

            If CStr(ViewState(AntiXsrfTokenKey)) <> _antiXsrfTokenValue OrElse CStr(ViewState(AntiXsrfUserNameKey)) <> (If(Context.User.Identity.Name, String.Empty)) Then
                Throw New InvalidOperationException("Validation of Anti-XSRF token failed.")
            End If
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub
End Class

我相信您正在寻找的具体线路是AddHandler Page.PreLoad, AddressOf master_Page_PreLoad

为了将来参考,如果您希望将 C# 代码转换为 VB,反之亦然,这里有一个非常棒的 Telerik 工具,可以在这里找到:http://converter.telerik.com/。为了获得我上面发布的代码,我只是简单地通过那里运行了 C# 模板。

【讨论】:

    【解决方案2】:

    你可以直接创建方法,

    Private Sub Page_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            ViewState(AntiXsrfTokenKey) = Page.ViewStateUserKey
            ViewState(AntiXsrfUserNameKey) = If(Context.User.Identity.Name, String.Empty)
        Else
    
            If CStr(ViewState(AntiXsrfTokenKey)) <> _antiXsrfTokenValue OrElse CStr(ViewState(AntiXsrfUserNameKey)) <> (If(Context.User.Identity.Name, String.Empty)) Then
                Throw New InvalidOperationException("Validation of " & "Anti-XSRF token failed.")
            End If
        End If
    
    End Sub
    

    不需要解决这个问题

    Page.PreLoad += master_Page_PreLoad;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 2018-05-06
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多