ASP.NET---安全模式

1.        安全的必要性

  • 构造特殊的链接地址,导致文件内的数据泄露
  • 数据库泄露
  • 安全防范的首要策略:所有的HTTP访问都要经过IIS,所以限制IIS的安全性是关键

2.        ASP.NET的安全模式

            ASP.NET支持的4种授权方法:

                a)        Windows:IIS验证,在内联网环境中非常有用

                b)        Passport:微软集中式身份验证,一次登录便可访问所有成员站点,需要收费

                c)        Form:窗体验证,验证帐号/密码,Web编程最佳最流行的验证方式

                d)        None:表示ASP.NET自己根本不执行身份验证,完全依赖IIS身份验证

  •     窗体身份验证
    <authenticationmode="Forms">

      <formsloginUrl="login.aspx"defaultUrl="Default.aspx">

        <credentialspasswordFormat="Clear">

          <username="admin"password="admin"/>

          <username="liu"password="123456"/>

        </credentials>

      </forms>

</authentication>

3.        基于窗体的身份授权模式

  • 允许用户访问整个应用程序或其特定资源的一种流行的模式
  • IIS接收请求,但不进行处理,而传递给ASP.NET应用程序
    

<froms>元素的主要属性

属性

name

这是赋予 cookie的名字,该cookie用于在请求之间保存用户。该默认值是 .ASPXAUTH

loginUrl

如果没有找到有效的验证 cookie,就指定请求重定向的URL

protection

指定要应用于验证cookie的保护级别,它有4个设置

All:应用程序使用数据有效性验证和加密机制来保护 cookie。这是默认设置。

None:不加密 cookie

Encryption:加密 cookie,但不对它进行数据有效性验证。

Validation:进行数据有效性验证,但不加密 cookie

path

指定应用程序所存储cookie的路径。在大多数情况下应用/,它是默认设置

timeout        

指定cookie过期的时间(分钟),其默认值为30分钟

cookieless

制定在进行验证和授权过程中,基于窗体的身份验证过程是否使用cookie

defaultUrl

指定默认的URL

domain

指定要与窗体身份验证一起发送的域名

1)        新建网站,建立如图所示的目标结构

 

2)        在Login.aspx中,添加“登录”按钮的单击事件。代码如下:

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 WebApplication1
{	
    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 tbname = TextBox1.Text;
     	        string tbpwd = TextBox2.Text;
if (tbname == "admin" && tbpwd == "admin")
            {
                FormsAuthentication.RedirectFromLoginPage(tbname, false);
            }
        }
    }
}

3)        修改Web.config文件加入以下代码:

<authenticationmode="Forms">

     <formsloginUrl="login.aspx"defaultUrl="Default.aspx">

       <credentialspasswordFormat="Clear">

         <username="admin"password="admin"/>

         <username="liu"password="123456"/>

                 </credentials>

     </forms>

   </authentication>

1.        对密码进行加密

        1)Clear:密码存储为明文。用户的密码直接与这个值比较。

       2)MD5:密码使用散列摘要进行存储。使用MD5算法进行散列,再与这个值进行相等比较。这个算法比  

SHA1的性能好。

       3)SHA1:密码使用SHA1散列摘要来存储。在验证证书时,用户密码使用SHA1算法进行散列,再与这个值

进行相等比较。这个算法的安全性最高。

MD5为例:

    存储散列的密码方法如下:

                Response.Write(FormsAuthentication.HashPasswordForStoringInConfigFile("admin", "MD5")); //21232F297A57A5A743894A0E4A801FC3
                Response.Write(FormsAuthentication.HashPasswordForStoringInConfigFile("123456", "MD5")); //E10ADC3949BA59ABBE56E057F20F883E
  • 修改Login.aspx中代码:
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 WebApplication1

{

    public partial class Login :System.Web.UI.Page

    {

        protected voidPage_Load(object sender, EventArgs e)

        {

        }

        protected void Button1_Click(object sender, EventArgse)

        {

           string tbname = TextBox1.Text;

           string tbpwd = TextBox2.Text;

           if (FormsAuthentication.Authenticate(tbname,tbpwd))

           {

               FormsAuthentication.RedirectFromLoginPage(tbname,false);

           }

        }

    }

}
  • 修改Web.config中代码:
    <authenticationmode="Forms">

     <formsloginUrl="login.aspx"defaultUrl="Default.aspx">

       <credentialspasswordFormat="MD5">

         <username="admin"password="21232F297A57A5A743894A0E4A801FC3"/>

         <username="liu"password="E10ADC3949BA59ABBE56E057F20F883E"/>

       </credentials>

     </forms>        

   </authentication>

2.        <authorization>对用户进行授权

A.       <authorization>配置

  • deny阻止访问用户;allow允许访问用户
  • ?代表匿名用户,*代表任意用户
  • 多个用户之间用“,”隔开

在Web.config中添加如下代码:

  • 对管理员授权
<location  path="admin">

   <system.web>

     <authorization>

       <allowusers="admin"/>

       <denyusers="*"/>

     </authorization>

   </system.web>

 </location>
  • 对普通用户授权
<location  path="users">

   <system.web>

     <authorization>

       <allowusers="liu"/>

       <denyusers="*"/>

     </authorization>

   </system.web>

 </location>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值