Asp.Net网页安全管理(验证与授权)

来源:互联网 发布:园林用广联达什么软件 编辑:程序博客网 时间:2024/05/16 15:31

1)安全的种类

网络设备,操作系统,数据库,代码安全

 

存储过程和视图能够进行权限管理

 

2web应用程序安全

稳固的身份验证和授权手段

机密数据的保密性和完整性的安全通信

 

3Asp.net验证控件和脚本验证

AspNet验证控件使用方便,服务器端控件

脚本验证客户端验证

 

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>

 

2Sql语句参数化

 

            //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  varchar50,

@pass  varchar50

)

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页面,将客户端数据传递给IISIIS对客户端进行身份验证,然后将经过身份验证的标记传递给客户端

服务器向客户端传递的是html

 

 

 

 

 

 

 

 

 

 

5,身份验证

WindowsPassportFormsNone

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”的连接字符串。