如何确保ASP.NET代码中用户修改行为的安全性?

2026-03-30 12:091阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计450个文字,预计阅读时间需要2分钟。

如何确保ASP.NET代码中用户修改行为的安全性?

我正在玩认证和授权,准备一些任务。创建了两个页面:Login.aspx和Default.aspx。在配置文件中,为表单设置了身份验证,拒绝未经身份验证的用户访问:`authentication mode=Forms forms na=true`。

我正在玩认证和授权以准备一些任务.我创建了两个页面:Login.aspx和Default.aspx.在配置文件中,我为表单设置了身份验证,并拒绝了未经身份验证的用户访问:

<authentication mode="Forms"> <forms name="aaa" defaultUrl="~/Login.aspx" /> </authentication> <authorization> <deny users="?"/> </authorization>

然后我写了一些简单的代码来验证我在Login.aspx中的用户:

protected void Page_Load(object sender, EventArgs e) { GenericIdentity identity = new GenericIdentity("aga", "bbb"); Context.User = new GenericPrincipal(identity, new String[] { "User" }); ; Response.Redirect("~/Default.aspx"); }

当我运行它时,重定向不会发生.而是一遍又一遍地调用Login.aspx,因为用户未经过身份验证(每次加载时Context.User.Identity.IsAuthenticated都为false).我究竟做错了什么?

Context.User仅设置当前请求的主体.重定向发生后,当前请求结束,新的请求再次以未覆盖的主体开始(显然未经过身份验证).因此,设置Context.User实际上不会对任何内容进行身份验证.

使用FormsAuthentication.SetAuthCookie()会将用户的cookie设置为FormsAuthentication提供程序接受的有效值,或将该令牌放入URL中.您可以重定向到您的心脏内容,因为cookie显然会与用户一起用于将来的请求.

来自MSDN(em添加):

With forms authentication, you can use the SetAuthCookie method when you want to authenticate a user but still retain control of the navigation with redirects.

如何确保ASP.NET代码中用户修改行为的安全性?

如上所述,这不一定需要cookie – 名称有点误导,因为如果FormsAuthentication处于无cookie模式,它仍将通过URL工作:

The SetAuthCookie method adds a forms-authentication ticket to either the cookies collection, or to the URL if CookiesSupported is false.

本文共计450个文字,预计阅读时间需要2分钟。

如何确保ASP.NET代码中用户修改行为的安全性?

我正在玩认证和授权,准备一些任务。创建了两个页面:Login.aspx和Default.aspx。在配置文件中,为表单设置了身份验证,拒绝未经身份验证的用户访问:`authentication mode=Forms forms na=true`。

我正在玩认证和授权以准备一些任务.我创建了两个页面:Login.aspx和Default.aspx.在配置文件中,我为表单设置了身份验证,并拒绝了未经身份验证的用户访问:

<authentication mode="Forms"> <forms name="aaa" defaultUrl="~/Login.aspx" /> </authentication> <authorization> <deny users="?"/> </authorization>

然后我写了一些简单的代码来验证我在Login.aspx中的用户:

protected void Page_Load(object sender, EventArgs e) { GenericIdentity identity = new GenericIdentity("aga", "bbb"); Context.User = new GenericPrincipal(identity, new String[] { "User" }); ; Response.Redirect("~/Default.aspx"); }

当我运行它时,重定向不会发生.而是一遍又一遍地调用Login.aspx,因为用户未经过身份验证(每次加载时Context.User.Identity.IsAuthenticated都为false).我究竟做错了什么?

Context.User仅设置当前请求的主体.重定向发生后,当前请求结束,新的请求再次以未覆盖的主体开始(显然未经过身份验证).因此,设置Context.User实际上不会对任何内容进行身份验证.

使用FormsAuthentication.SetAuthCookie()会将用户的cookie设置为FormsAuthentication提供程序接受的有效值,或将该令牌放入URL中.您可以重定向到您的心脏内容,因为cookie显然会与用户一起用于将来的请求.

来自MSDN(em添加):

With forms authentication, you can use the SetAuthCookie method when you want to authenticate a user but still retain control of the navigation with redirects.

如何确保ASP.NET代码中用户修改行为的安全性?

如上所述,这不一定需要cookie – 名称有点误导,因为如果FormsAuthentication处于无cookie模式,它仍将通过URL工作:

The SetAuthCookie method adds a forms-authentication ticket to either the cookies collection, or to the URL if CookiesSupported is false.