asp.net身份验证和授权

本文介绍了一个ASP.NET项目中实现身份验证的过程,包括登录界面的设计、登录逻辑的处理、使用Forms Authentication进行用户验证及权限设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

login.aspx HTML代码

login.aspx.cs代码如下


private void btnLoginBetter_Click(object sender, System.EventArgs e)
  
{
   
if (this.tbName.Text == "admin" && this.tbPass.Text == "admin")
   
{
    FormsAuthenticationTicket ticket 
= new FormsAuthenticationTicket(1,this.tbName.Text,DateTime.Now,DateTime.Now.AddMinutes(30),this.PersistCookie.Checked,"User");//创建一个验证票据
    string cookieStr = FormsAuthentication.Encrypt(ticket);进行加密
    HttpCookie cookie 
= new HttpCookie(FormsAuthentication.FormsCookieName,cookieStr);创建一个cookie,cookie名为web.config设置的名,值为加密后的数据cookieStr,
    
if (this.PersistCookie.Checked)//判断用户是否选中保存cookie
     cookie.Expires = ticket.Expiration;//获取cookie过期时间
    cookie.Path = FormsAuthentication.FormsCookiePath;//设置cookie保存路径
    Response.Cookies.Add(cookie);
    
string strRedirect;
    strRedirect 
= Request["ReturnUrl"];//取出返回url
    if (strRedirect == null)
     strRedirect 
= "Default.aspx";
    Response.Redirect(strRedirect,
true);

   }

   
else
   
{
    Response.Write(
"<script>alert('帐号或密码错误!');self.location.href='02login.aspx'</script>");
   }

  }


 

Default.aspx HTML代码


<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<FONT face="宋体">
                
<asp:Label id="Label1" style="Z-INDEX: 106; LEFT: 224px; POSITION: absolute; TOP: 72px" runat="server">用户名称:</asp:Label>
                
<asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 220px; POSITION: absolute; TOP: 136px" runat="server">身份:</asp:Label>
                
<asp:Label id="lbUser" style="Z-INDEX: 103; LEFT: 350px; POSITION: absolute; TOP: 79px" runat="server"></asp:Label>
                
<asp:Label id="lbSf" style="Z-INDEX: 104; LEFT: 355px; POSITION: absolute; TOP: 133px" runat="server"></asp:Label>
                
<asp:Button id="btnLogout" style="Z-INDEX: 105; LEFT: 261px; POSITION: absolute; TOP: 192px"
                    runat
="server" Text="注销" Width="101px"></asp:Button></FONT>
        
</form>
    
</body>

后置代码


private void Page_Load(object sender, System.EventArgs e)
  
{
   
this.lbUser.Text = User.Identity.Name;
   
if (User.IsInRole("Admin"))
    
this.lbSf.Text = "Admin";
   
else
    
this.lbSf.Text = "User";
  }


  
Web 窗体设计器生成的代码

  
private void btnLogout_Click(object sender, System.EventArgs e)
  
{
   FormsAuthentication.SignOut();
//注销票
   Response.Redirect("login.aspx",true);返回login.aspx页面
  }


webconfig配置如下
    <authentication mode="Forms" >
  <forms name=".SecurityDemo" loginUrl="login.aspx">//.SecurityDemo为cookie名,
  </forms>
    </authentication>

 <authorization>
            <deny users="?"/> //拒绝所有匿名用户
            <allow roles="admins"/>//允许管理级别用户访问
   </authorization>
自我感觉ASP写多了,一般是用session进行判断用户是否合法,但在一个ASP.NET项目中使用身份验证,基本上所有页面都要验证才能访问,可以在web.config页面对指定的页面设置权限,设置代码如下
  <location path="admin">
    <system.web>
      <authorization>
        <deny users="*" />
         <allow roles="paley"/>
      </authorization>
    </system.web>
  </location>
已看资料修如上.对admin文件夹设置权限,拒绝所有用户,允许paley访问

 

转自:http://www.cnblogs.com/paleyyang/archive/2006/10/21/536147.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值