Asp.Net网页安全管理(验证与授权)
2010-08-19 15:47
316 查看
(1)安全的种类
网络设备,操作系统,数据库,代码安全
存储过程和视图能够进行权限管理
(2)web应用程序安全
稳固的身份验证和授权手段
机密数据的保密性和完整性的安全通信
(3)Asp.net验证控件和脚本验证
Asp.Net验证控件使用方便,服务器端控件
脚本验证客户端验证
(4)哈希算法
哈希算法生成固定长度的哈希值,可唯一标示任意长度字符串的内容,同一个字符串生成同样的哈希值,即使是细微的差异也会生成不同的哈希值
MD5(信息摘要算法):生成128位的哈希值
SHA1(安全哈希算法):生成160位哈希值
(5)代码安全性
1,将数据库连接封装到配置文件中
Windows登陆的数据库连接字符串
string conStr=”Data Source=.;Initial Catalog=Test;Integrated Security=True”
可以把连接字符串写到webconfig的配置文件中
<connectionStrings>
<add name="connstr" connectionString="Provider=Microsoft.Ace.OLEDB.12.0;Data Source=|DataDirectory|/Database.accdb"/>
</connectionStrings>
2,Sql语句参数化
//sql语句
string sql="select * from user where user=@user and pass=@pass";
//参数
SqlParameter[] para=new SqlParameter []{new SqlParameter("@user",user),new SqlParameter("@pass",pass)};
//为sql传递参数
SqlCommand command = new SqlCommand(sql,con);
command.Parameters.Add(para);
//command命令执行sql语句
command.ExecuteReader()
3,存储过程
数据库中写一个存储过程
create procedure login
(
@user varchar(50),
@pass varchar(50)
)
as
select * from user where user=@user and pass=@pass
调用存储过程:
方式1:
SqlCommand com=new SqlCommand(“存储过程名”,con)
com.CommandType=CommandType.StoredProcedurce
Com.Parameters.Add(“@user”,SqlDbType.数据类型).Value=userName.Text;
Com.Parameters.Add(“@pass”,SqlDbType.数据类型).Value=pass.Text;
方法2:
SqlCommand com=new SlqCommand()
Com.Connection=con;
Com.CommandText=”存储过程名”
Com.CommandType=CommandType.StoredProcedure
Com.Parameters.Add(“@user”,SqlDbType.数据类型).Value=userName.Text;
Com.Parameters.Add(“@pass”,SqlDbType.数据类型).Value=pass.Text;
4,请求过程
客户端请求aspx页面,将客户端数据传递给IIS,IIS对客户端进行身份验证,然后将经过身份验证的标记传递给客户端
服务器向客户端传递的是html流
5,身份验证
Windows,Passport,Forms,None
Windows验证:通过计算机用户进行验证
在Iis服务器中进行操作,在自己网站文件夹属性,目录安全性,编辑中去掉匿名访问,选中windows身份验证
人员少的内部网站中使用
6,不允许绕过页面
采用表单验证方式,首先在配置文件中作如下修改:
//验证
<authentication mode=”Forms”>
<forms name=”Test” defaultUrl=”Default.aspx” loginUrl=”Login.aspx” path=”/” protection=“All” timeout=”30”>
</authentication>
Path的值为”/”表示保存在cookie下,protection是否加密,timeout表示cookie的生存时间
//授权
<authorization>
<deny users=”?”/>//表示拒绝匿名访问
</authorization>
<system.web>
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<authentication mode="Forms">
<forms defaultUrl="User/UserDefault.aspx" loginUrl="Login.aspx" name="GouID"></forms>
</authentication>
</system.web>
</location>
<location path="User">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
登陆界面
using System.Web.Security;
if(userName.Text=="admin"&&userPwd=="admin")
{
FormsAuthentication.RedirectFromLoginPage(userName.Text,true)
}
在需要不许绕过的页面中加入代码
protected void Page_Load(object sender, EventArgs e)
{
if (!User.Identity.IsAuthenticated)
{
Response.Write(User.Identity.Name);
Response.Write(User.Identity.AuthenticationType);
}
else
{
Response.Write("Error");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//移除cookie
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
8,表单验证并且不能绕过登陆页面
using System.Web.Security;
1, 在配置文件中设置验证(authentication)和授权(authorization)文件
<authentication mode="Forms">
<forms name="Test" defaultUrl="Default.aspx" loginUrl="Login.aspx" path="/" protection="All" timeout="30"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
参数说明:
Path的值为”/”表示保存在cookie下,protection是否加密,timeout表示cookie的生存时间
deny拒绝,?表示拒绝匿名访问,*表示拒绝所有的用户
2,当页面准备绕过登陆页面直接进入管理员界面
首先管理员界面应该判断用户是否验证,如果已验证则进入界面否则不进入界面
protected void Page_Load(object sender, EventArgs e)
{
if (!User.Identity.IsAuthenticated)
{
//用户验证的名称
Response.Write(User.Identity.Name);
//用户验证的类型
Response.Write(User.Identity.AuthenticationType);
}
else
{
Response.Write("Error");
}
}
3,在登陆页面进行身份验证,然后重定向到原请求页面
if(userName.Text=="admin"&&userPwd.Text=="admin")
{
FormsAuthentication.RedirectFromLoginPage(userName.Text, true);}
9,加密算法
string str=FormsAuthentication.HashPasswordForStoringInConfigFile(userPwd.Text,"MD5");
10,将登陆信息加密
protected void Button1_Click(object sender, EventArgs e)
{
//获取参数
string userName=txtUserName.Text;
string userPass=txtUserPass.Text;
//加密
userName=FormsAuthentication.HashPasswordForStoringInConfigFile(userName,"MD5");
userPass = FormsAuthentication.HashPasswordForStoringInConfigFile(userPass,"MD5");
//创建连接
string conStr = "Data Source=.;Initial Catalog=testtwo;User ID=sa;Pwd=123";
SqlConnection con = new SqlConnection(conStr);
//打开连接
con.Open();
//创建command命令
SqlCommand command = new SqlCommand();
command.CommandText = "login";
command.Connection = con;
command.CommandType = CommandType.StoredProcedure;
//向command命令中添加参数
command.Parameters.Add("@username",SqlDbType.NVarChar).Value=userName;
command.Parameters.Add("@userpass", SqlDbType.NVarChar).Value = userPass;
if (Convert.ToInt32(command.ExecuteScalar()) > 0)
{
Response.Write("<script>alert('登陆成功!')</script>");
Response.Write("userName:"+userName+"<br>");
Response.Write("userPass:" + userPass);
}
}
用 ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; 读取name为“connstr”的连接字符串。
网络设备,操作系统,数据库,代码安全
存储过程和视图能够进行权限管理
(2)web应用程序安全
稳固的身份验证和授权手段
机密数据的保密性和完整性的安全通信
(3)Asp.net验证控件和脚本验证
Asp.Net验证控件使用方便,服务器端控件
脚本验证客户端验证
(4)哈希算法
哈希算法生成固定长度的哈希值,可唯一标示任意长度字符串的内容,同一个字符串生成同样的哈希值,即使是细微的差异也会生成不同的哈希值
MD5(信息摘要算法):生成128位的哈希值
SHA1(安全哈希算法):生成160位哈希值
(5)代码安全性
1,将数据库连接封装到配置文件中
Windows登陆的数据库连接字符串
string conStr=”Data Source=.;Initial Catalog=Test;Integrated Security=True”
可以把连接字符串写到webconfig的配置文件中
<connectionStrings>
<add name="connstr" connectionString="Provider=Microsoft.Ace.OLEDB.12.0;Data Source=|DataDirectory|/Database.accdb"/>
</connectionStrings>
2,Sql语句参数化
//sql语句
string sql="select * from user where user=@user and pass=@pass";
//参数
SqlParameter[] para=new SqlParameter []{new SqlParameter("@user",user),new SqlParameter("@pass",pass)};
//为sql传递参数
SqlCommand command = new SqlCommand(sql,con);
command.Parameters.Add(para);
//command命令执行sql语句
command.ExecuteReader()
3,存储过程
数据库中写一个存储过程
create procedure login
(
@user varchar(50),
@pass varchar(50)
)
as
select * from user where user=@user and pass=@pass
调用存储过程:
方式1:
SqlCommand com=new SqlCommand(“存储过程名”,con)
com.CommandType=CommandType.StoredProcedurce
Com.Parameters.Add(“@user”,SqlDbType.数据类型).Value=userName.Text;
Com.Parameters.Add(“@pass”,SqlDbType.数据类型).Value=pass.Text;
方法2:
SqlCommand com=new SlqCommand()
Com.Connection=con;
Com.CommandText=”存储过程名”
Com.CommandType=CommandType.StoredProcedure
Com.Parameters.Add(“@user”,SqlDbType.数据类型).Value=userName.Text;
Com.Parameters.Add(“@pass”,SqlDbType.数据类型).Value=pass.Text;
4,请求过程
客户端请求aspx页面,将客户端数据传递给IIS,IIS对客户端进行身份验证,然后将经过身份验证的标记传递给客户端
服务器向客户端传递的是html流
5,身份验证
Windows,Passport,Forms,None
Windows验证:通过计算机用户进行验证
在Iis服务器中进行操作,在自己网站文件夹属性,目录安全性,编辑中去掉匿名访问,选中windows身份验证
人员少的内部网站中使用
6,不允许绕过页面
采用表单验证方式,首先在配置文件中作如下修改:
//验证
<authentication mode=”Forms”>
<forms name=”Test” defaultUrl=”Default.aspx” loginUrl=”Login.aspx” path=”/” protection=“All” timeout=”30”>
</authentication>
Path的值为”/”表示保存在cookie下,protection是否加密,timeout表示cookie的生存时间
//授权
<authorization>
<deny users=”?”/>//表示拒绝匿名访问
</authorization>
<system.web>
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<authentication mode="Forms">
<forms defaultUrl="User/UserDefault.aspx" loginUrl="Login.aspx" name="GouID"></forms>
</authentication>
</system.web>
</location>
<location path="User">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
登陆界面
using System.Web.Security;
if(userName.Text=="admin"&&userPwd=="admin")
{
FormsAuthentication.RedirectFromLoginPage(userName.Text,true)
}
在需要不许绕过的页面中加入代码
protected void Page_Load(object sender, EventArgs e)
{
if (!User.Identity.IsAuthenticated)
{
Response.Write(User.Identity.Name);
Response.Write(User.Identity.AuthenticationType);
}
else
{
Response.Write("Error");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//移除cookie
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
8,表单验证并且不能绕过登陆页面
using System.Web.Security;
1, 在配置文件中设置验证(authentication)和授权(authorization)文件
<authentication mode="Forms">
<forms name="Test" defaultUrl="Default.aspx" loginUrl="Login.aspx" path="/" protection="All" timeout="30"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
参数说明:
Path的值为”/”表示保存在cookie下,protection是否加密,timeout表示cookie的生存时间
deny拒绝,?表示拒绝匿名访问,*表示拒绝所有的用户
2,当页面准备绕过登陆页面直接进入管理员界面
首先管理员界面应该判断用户是否验证,如果已验证则进入界面否则不进入界面
protected void Page_Load(object sender, EventArgs e)
{
if (!User.Identity.IsAuthenticated)
{
//用户验证的名称
Response.Write(User.Identity.Name);
//用户验证的类型
Response.Write(User.Identity.AuthenticationType);
}
else
{
Response.Write("Error");
}
}
3,在登陆页面进行身份验证,然后重定向到原请求页面
if(userName.Text=="admin"&&userPwd.Text=="admin")
{
FormsAuthentication.RedirectFromLoginPage(userName.Text, true);}
9,加密算法
string str=FormsAuthentication.HashPasswordForStoringInConfigFile(userPwd.Text,"MD5");
10,将登陆信息加密
protected void Button1_Click(object sender, EventArgs e)
{
//获取参数
string userName=txtUserName.Text;
string userPass=txtUserPass.Text;
//加密
userName=FormsAuthentication.HashPasswordForStoringInConfigFile(userName,"MD5");
userPass = FormsAuthentication.HashPasswordForStoringInConfigFile(userPass,"MD5");
//创建连接
string conStr = "Data Source=.;Initial Catalog=testtwo;User ID=sa;Pwd=123";
SqlConnection con = new SqlConnection(conStr);
//打开连接
con.Open();
//创建command命令
SqlCommand command = new SqlCommand();
command.CommandText = "login";
command.Connection = con;
command.CommandType = CommandType.StoredProcedure;
//向command命令中添加参数
command.Parameters.Add("@username",SqlDbType.NVarChar).Value=userName;
command.Parameters.Add("@userpass", SqlDbType.NVarChar).Value = userPass;
if (Convert.ToInt32(command.ExecuteScalar()) > 0)
{
Response.Write("<script>alert('登陆成功!')</script>");
Response.Write("userName:"+userName+"<br>");
Response.Write("userPass:" + userPass);
}
}
用 ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; 读取name为“connstr”的连接字符串。
相关文章推荐
- ASP.NET基于表单的验证实现网上安全访问,管理(1)
- [.NET 基于角色安全性验证] 之四:ASP.NET 2.0 成员资格和角色管理授权
- ASP.NET安全机制1--身份验证和授权基础篇
- ASP.NET基于表单的验证实现网上安全访问,管理
- 转 ASP.NET基于表单的验证实现网上安全访问,管理(2)
- ASP.NET中启用Windows集成验证,怎样在调用System.DirectoryServices下的组件时传递安全上下文,也就是说当前用户凭据,来实现权限管理
- ASP.NET基于表单的验证实现网上安全访问,管理(2)
- [.NET 基于角色安全性验证] 之四:ASP.NET 2.0 成员资格和角色管理授权
- 《ASP.NET基于表单的验证实现网上安全访问,管理》
- 艾伟:ASP.NET安全问题--Forms验证(后篇)--实战篇
- ASP.NET应用程序的安全方案(一)—身份验证 (转)
- Asp.Net二级域名共享Forms身份验证、下载站/图片站的授权访问控制
- ASP.NET安全问题--Froms验证的具体介绍(中篇)
- Asp.net中基于Forms验证的角色验证授权[原理及流程]
- ASP.NET MVC 随想录——探索ASP.NET Identity 身份验证和基于角色的授权,中级篇
- [转]Asp.net中基于Forms验证的角色验证授权
- ASP.NET应用程序的安全方案(二)—授权
- ASP.Net MVC 生成安全验证码
- (转)ASP.NET安全问题--Froms验证的具体介绍(上篇)
- ASP.NET安全问题--ASP.NET生命周期中的验证以及身份验证模块