【问题标题】:securing view state using ViewstateUserKey property使用 ViewstateUserKey 属性保护视图状态
【发布时间】:2014-12-12 04:53:11
【问题描述】:

在阅读有关防止网络表单中的跨站点请求伪造的信息时,许多作者使用如下语句:

使用 ViewState 中的 ViewStateUserKey 属性来防止欺骗性回发。在 Page 派生类的 OnInit 虚方法中添加以下内容(该属性必须在 Page.Init 事件中设置)。

protected override OnInit(EventArgs e) {
 base.OnInit(e); 
 if (User.Identity.IsAuthenticated)
    ViewStateUserKey = Session.SessionID; }

以下使用您选择的唯一值将 Viewstate 键分配给个人。

 (Page.ViewStateUserKey)

这必须在 Page_Init 中应用,因为在加载 Viewstate 之前必须将密钥提供给 ASP.NET。此选项自 ASP.NET 1.1 起就可用

我试图将上述内容放在基本页面中,所有页面都来自该页面但是我得到了像 OnInit must return value 这样的错误

这是我的代码

public class BasePage : System.Web.UI.Page
{

    protected override OnInit(EventArgs e) {
 base.OnInit(e); 
 if (User.Identity.IsAuthenticated)
    ViewStateUserKey = Session.SessionID; }

 }

在此先感谢

【问题讨论】:

    标签: c# asp.net .net viewstate


    【解决方案1】:

    OnInit 声明缺少返回类型 void。

    protected override void OnInit(EventArgs e)
    

    【讨论】:

    • 谢谢......但是我无法理解这一行(该属性必须在 Page.Init 事件中设置)并且该函数必须放在从 base 派生的页面中页面右...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    相关资源
    最近更新 更多