国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net實現網站用戶登錄認證

2024-07-10 13:29:41
字體:
來源:轉載
供稿:網友

本文給大家介紹的是.net實現網站用戶登錄認證的方法和實例,都非常的簡單實用,需要的小伙伴可以參考下。

cookie登錄后同域名下的網站保持相同的登錄狀態。

登錄

 

 
  1. private void SetAuthCookie(string userId, bool createPersistentCookie) 
  2.   var ticket = new FormsAuthenticationTicket(2, userId, DateTime.Now, DateTime.Now.AddDays(7), true"", FormsAuthentication.FormsCookiePath); 
  3.   string ticketEncrypted = FormsAuthentication.Encrypt(ticket); 
  4.  
  5.   HttpCookie cookie; 
  6.   if (createPersistentCookie)//是否在設置的過期時間內一直有效 
  7.   { 
  8.     cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted) 
  9.     { 
  10.       HttpOnly = true
  11.       Path = FormsAuthentication.FormsCookiePath, 
  12.       Secure = FormsAuthentication.RequireSSL, 
  13.       Expires = ticket.Expiration, 
  14.       Domain = "cnblogs.com"//這里設置認證的域名,同域名下包括子域名如aa.cnblogs.com或bb.cnblogs.com都保持相同的登錄狀態 
  15.     }; 
  16.   } 
  17.   else 
  18.   { 
  19.     cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted) 
  20.     { 
  21.       HttpOnly = true
  22.       Path = FormsAuthentication.FormsCookiePath, 
  23.       Secure = FormsAuthentication.RequireSSL, 
  24.       //Expires = ticket.Expiration,//無過期時間的,瀏覽器關閉后失效 
  25.       Domain = "cnblogs.com" 
  26.     }; 
  27.   } 
  28.  
  29.   HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName); 
  30.   HttpContext.Current.Response.Cookies.Add(cookie); 

這樣登錄后,在同域名下的任何頁面都可以得到用戶狀態

判斷用戶是否登錄

 

 
  1. public bool IsAuthenticated 
  2.   get 
  3.   { 
  4.     bool isPass = System.Web.HttpContext.Current.User.Identity.IsAuthenticated; 
  5.  
  6.     if (!isPass) 
  7.       SignOut(); 
  8.  
  9.     return isPass; 
  10.   } 

得到當前的用戶名

 

 
  1. public string GetCurrentUserId() 
  2. return _httpContext.User.Identity.Name; 

下面給大家一個具體的實例

CS頁代碼:

 

 
  1. using System; 
  2. using System.Data; 
  3. using System.Configuration; 
  4. using System.Collections; 
  5. using System.Web; 
  6. using System.Web.Security; 
  7. using System.Web.UI; 
  8. using System.Web.UI.WebControls; 
  9. using System.Web.UI.WebControls.WebParts; 
  10. using System.Web.UI.HtmlControls; 
  11. using System.Data.SqlClient; 
  12.  
  13. public partial class Login : System.Web.UI.Page 
  14. protected void Page_Load(object sender, EventArgs e) 
  15.  
  16. protected void Button1_Click(object sender, EventArgs e) 
  17. {  
  18.  
  19. string connString = Convert.ToString(ConfigurationManager.ConnectionStrings["001ConnectionString"]); 
  20. //001ConnectionString是我在webconfig里配置的數據庫連接。 
  21. SqlConnection conn = new SqlConnection(connString);  
  22. string strsql = "select * from User_table where User_name='" + UserName.Text + "' and Password='" + Password.Text + "'"
  23. SqlCommand cmd = new SqlCommand(strsql, conn); 
  24. conn.Open(); 
  25. SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
  26.  
  27. if (dr.Read()) 
  28. {  
  29. Response.Redirect("index.aspx"); 
  30. conn.Close(); 
  31. else 
  32. FailureText.Text = "登陸失敗,請檢查登陸信息!"
  33. conn.Close(); 
  34. Response.Write("<script language=javascript>alert('登陸失敗!.');</script>"); 
  35.  
  36. protected void Button2_Click(object sender, EventArgs e) //文本框重置按鈕 
  37. UserName.Text = ""
  38. Password.Text = ""
  39.  

下面是aspx頁面代碼:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  4.  
  5. <html xmlns=" http://www.w3.org/1999/xhtml" > 
  6. <head runat="server"
  7. <title>無標題頁</title> 
  8. </head> 
  9. <body> 
  10. <form id="form1" runat="server">  
  11. <asp:Panel ID="Panel1" runat="server" Height="101px" Width="231px" Wrap="False"
  12. <table> 
  13. <tr> 
  14. <td align="center" colspan="2"
  15. 用戶登陸</td> 
  16. </tr> 
  17. <tr> 
  18. <td style="width: 89px"
  19. 用戶名:</td> 
  20. <td style="width: 100px"
  21. <asp:TextBox ID="UserName" runat="server" Wrap="False"></asp:TextBox></td> 
  22. </tr> 
  23. <tr> 
  24. <td style="width: 89px"
  25. 密碼:</td> 
  26. <td style="width: 100px"
  27. <asp:TextBox ID="Password" runat="server" TextMode="Password" Width="148px" Wrap="False" ></asp:TextBox></td> 
  28. </tr> 
  29. <tr> 
  30. <td align="center" colspan="2" style="text-align: center"
  31. <asp:Button ID="Button1" runat="server" Text="登陸" Width="50px" OnClick="Button1_Click" /> 
  32. <asp:Button ID="Button2" runat="server" Text="重置" Width="50px" OnClick="Button2_Click" /></td> 
  33. </tr> 
  34. <tr> 
  35. <td align="center" colspan="2"
  36. <asp:Label ID="FailureText" runat="server" Width="77px"></asp:Label></td> 
  37. </tr> 
  38. </table> 
  39. </asp:Panel> 
  40.  
  41. </form> 
  42. </body> 
  43. </html> 


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清河县| 东阿县| 鄂温| 鲁甸县| 巴南区| 巴南区| 沾化县| 宜兰县| 凤城市| 博野县| 普陀区| 专栏| 镇安县| 禹城市| 眉山市| 延吉市| 兰西县| 迭部县| 丹巴县| 邛崃市| 出国| 盖州市| 荣昌县| 金山区| 舟曲县| 衡阳县| 平邑县| 保靖县| 寿宁县| 陕西省| 新化县| 黄陵县| 张家口市| 东乡县| 南江县| 万荣县| 小金县| 沁阳市| 广德县| 邳州市| 盱眙县|