今天閒著無聊.想起來了ASP.Net身份驗證.感覺良好.貼出下列代碼:
login.ASPx Html代碼
1<%@ Page language="c#" Codebehind="02Login.ASPx.cs" AutoEventWireup="false" Inherits="身份驗證._02Login" %>
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN" >
3<Html>
4 <HEAD>
5 <title>02Login</title>
6 <meta name="GENERATOR" Content="Microsoft Visual Studio .Net 7.1">
7 <meta name="CODE_LANGUAGE" Content="C#">
8 <meta name="vs_defaultClIEntScript" content="JavaScript">
9 <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/IE5">
10 </HEAD>
11 <body MS_POSITIONING="GridLayout">
12 <form id="Form1" method="post" runat="server">
13 <FONT face="宋體">
14 <TABLE id="Table1"
15 cellSpacing="1" cellPadding="1" width="446" border="1">
16 <TR>
17 <TD>
18 <asp:label id="Label1" runat="server">用戶名稱:</ASP:label></TD>
19 <TD>
20 <asp:textbox id="tbName" runat="server" Width="183px"></ASP:textbox></TD>
21 <TD>
22 <asp:requiredfieldvalidator id="RequiredFIEldValidator1" runat="server" ErrorMessage="用戶名不能為空!" ControlToValidate="tbName"></ASP:requiredfIEldvalidator></TD>
23 </TR>
24 <TR>
25 <TD>
26 <asp:label id="Label2" runat="server">密碼:</ASP:label></TD>
27 <TD>
28 <asp:textbox id="tbPass" runat="server" Width="183px"></ASP:textbox></TD>
29 <TD>
30 <asp:requiredfieldvalidator id="RequiredFIEldValidator2" runat="server" ErrorMessage="密碼不能為空!" ControlToValidate="tbPass"></ASP:requiredfIEldvalidator></TD>
31 </TR>
32 <TR>
33 <TD><FONT face="宋體">是否保存CookIE</FONT></TD>
34 <TD>
35 <asp:checkbox id="PersistCookIE" runat="server"></ASP:checkbox></TD>
36 <TD></TD>
37 </TR>
38 </TABLE>
39 <ASP:button id="btnLoginBetter"
40 runat="server" Width="78px" Text="登錄"></ASP:button>
41 <ASP:HyperLink id="HyperLink1"
42 runat="server" NavigateUrl="Default.aspx">HyperLink</ASP:HyperLink></FONT>
43 </form>
44 </body>
45</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" runat="server">用戶名稱:</ASP:Label>
<asp:Label id="Label2" runat="server">身份:</ASP:Label>
<asp:Label id="lbUser" runat="server"></ASP:Label>
<asp:Label id="lbSf" runat="server"></ASP:Label>
<ASP:Button id="btnLogout"
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 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.Net Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.btnLogout.Click += new System.EventHandler(this.btnLogout_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
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.ASPx">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
如果只有幾個頁面設置如上代碼,感覺還可以接受.但頁面多了豈不是要把人累死呀..
可能是小的項目做多了,大項目沒接觸過.請高手給指點具體用途呀.不甚感激.