asp.net里面的身份驗證和授權(2)_.Net教程
推薦:ASP.NET的高級調試技巧 對于一個項目來說,你不可能通過設定起始頁按F5鍵進行調試,原因是:各個網頁間的關聯性太強,要驗證的的東西也很多。在調試時很難進行(實際上在我做的項目中根本不能進行)。 那么
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";
}
分享:結合JavaScript與ASP.NET Web窗體進行程序開發ASP.NET為Web程序開發提供了新的范例。其中包括一系列基于服務器的控件,這些控件類似于HTML窗體中諸如文本框、按鈕等元素。使用這些控件的問題是必須調用服務器。JavaScript為很多任務提供多種
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發送Email實例(可帶附件)
- js實現廣告漂浮效果的小例子
- asp.net Repeater 數據綁定的具體實現
- Asp.Net 無刷新文件上傳并顯示進度條的實現方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報表的使用方法
- ASP.NET中操作SQL數據庫(連接字符串的配置及獲取)
- asp.net頁面傳值測試實例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲過程實現分頁示例代碼
- 相關鏈接:
- 教程說明:
.Net教程-asp.net里面的身份驗證和授權(2)
。