1、安全的必要性
- 构造特殊特殊的链接地址,普通的链接地址会导致文件的数据泄露
- 数据库泄漏
- 安全防范的首要策略:所有的HTTP访问都要经过IIS,所以限制IIS的安全性是关键
2、ASP.NET支持的四种授权模式
Windows:IIS验证,在内联网环境中非常有用
Passport : 微软集中身份验证,一次登录便可访问所有成员站点,需要收费
Form:窗体验证,验证帐号/密码,Web编程最佳最流行的验证方式
None: 表示ASP.NET自己根本不执行验证,完全依赖 IIS身份验证
3、基于窗体的身份授权模式
- 允许用户访问整个应用程序或其特定资源的一种流行的模式
- IIS 接收请求,但不进行处理,而传递给ASP.NET 应用程序
4、<forms>元素的主要属性
<forms>验证事例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace 第3章_Asp.net安全验证
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string username = this.TextBox1.Text;
string userpwd = this.TextBox2.Text;
if (UserManager.login(username, userpwd))
{
//验证成功之后重定向到首次访问的页面(保存cookie的时间)
FormsAuthentication.RedirectFromLoginPage(username, true);
}
else
{
//服务器端
Response.Write("<script>alert('帐号密码错误')</script>");
}
}
}
}
修改Web.config文件
<authentication mode="Forms">
<forms loginUrl="loogin.aspx" defaultUrl="index.aspx">
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
我们可以将用户名和密码存在Web.config文件中,用来检查用户名和密码是否来自授权用户,代码如下:
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="Login.aspx" defaultUrl="Index.aspx" >
<!--
credentials配置节
passwordFormat:加密方式
Clear:明文
MD5:加密算法,比SHA1性能高
SHA1:加密算法
-->
<credentials passwordFormat="Clear">
<user name="admin" password="admin"/>
<user name="comm" password="comm"/>
</credentials>
</forms>
</authentication>
<authorization>
<allow users="admin,comm"/><!--允许admin和sys账户访问-->
<deny users="?,*"/><!--禁止匿名用户及admin和sys之外的用户访问-->
</authorization>
对密码加密:<credentials>有一个属性passwordFormat,其值可以是Clear,MD5,SHAI。
- Clear:密码存储为明文。用户的密码直接与这个值比较,不需要进一步转换。
- MD5:密码使用散列摘要进行存储。在验证证书时,用户密码使用MD5算法进行散列,再与这个值进行相等比较。不会存储货比较明文密码。这个算法比SHAI的性能好。
- SHAI:密码使用SHAI散列摘要来存储。在验证证书是,用户密码使用SHAI算法进行散列,再与这个值进行相等比较。不会存储或比较明文密码。这个算法的安全性最高。