asp.net里面的身份驗(yàn)證和授權(quán)(2)_.Net教程
推薦:ASP.NET的高級調(diào)試技巧 對于一個(gè)項(xiàng)目來說,你不可能通過設(shè)定起始頁按F5鍵進(jìn)行調(diào)試,原因是:各個(gè)網(wǎng)頁間的關(guān)聯(lián)性太強(qiáng),要驗(yàn)證的的東西也很多。在調(diào)試時(shí)很難進(jìn)行(實(shí)際上在我做的項(xiàng)目中根本不能進(jìn)行)。 那么
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");
//創(chuàng)建一個(gè)驗(yàn)證票據(jù)
string cookieStr = FormsAuthentication.Encrypt(ticket);
//進(jìn)行加密
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,cookieStr);
//創(chuàng)建一個(gè)cookie,cookie名為web.config設(shè)置的名,值為加密后的數(shù)據(jù)cookieStr,
if (this.PersistCookie.Checked)//判斷用戶是否選中保存cookie
cookie.Expires = ticket.Expiration;//獲取cookie過期時(shí)間
cookie.Path = FormsAuthentication.FormsCookiePath;//設(shè)置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('帳號或密碼錯(cuò)誤!');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";
}
分享:結(jié)合JavaScript與ASP.NET Web窗體進(jìn)行程序開發(fā)ASP.NET為Web程序開發(fā)提供了新的范例。其中包括一系列基于服務(wù)器的控件,這些控件類似于HTML窗體中諸如文本框、按鈕等元素。使用這些控件的問題是必須調(diào)用服務(wù)器。JavaScript為很多任務(wù)提供多種
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
- asp.net頁面?zhèn)髦禍y試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過程實(shí)現(xiàn)分頁示例代碼
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- 將非模態(tài)對話框顯示為模態(tài)對話框
- 解析VB.NET中的常量與枚舉功能
- 淺析asp.net驗(yàn)證控件失效原因及解決方法
- 組合.NET數(shù)據(jù)控件構(gòu)建強(qiáng)大用戶接口
- ASP.NET 設(shè)計(jì)中的 N 個(gè)技巧
- ASP.NET中文顯示之兩種解決方法
- Asp.net Ajax--Calendar控件使用
- ASP.NET網(wǎng)站開發(fā)的架構(gòu)設(shè)計(jì)
- 基于asp.net下使用jquery實(shí)現(xiàn)ajax的解決方法
- VS2010、VS2008等項(xiàng)目的默認(rèn)瀏覽器修改方法(圖文)
- 相關(guān)鏈接:
- 教程說明:
.Net教程-asp.net里面的身份驗(yàn)證和授權(quán)(2)。