首页 > 编程 > .NET > 正文

asp.net实现网站用户登录认证

2024-07-10 13:29:41
字体:大 中 小
来源:转载
供稿:网友

本文给大家介绍的是.net实现网站用户登录认证的方法和实例,都非常的简单实用,需要的小伙伴可以参考下。

cookie登录后同域名下的网站保持相同的登录状态。

登录

 

 
  1. private void SetAuthCookie(string userId, bool createPersistentCookie) 
  2. { 
  3.   var ticket = new FormsAuthenticationTicket(2, userId, DateTime.Now, DateTime.Now.AddDays(7), true, "", FormsAuthentication.FormsCookiePath); 
  4.   string ticketEncrypted = FormsAuthentication.Encrypt(ticket); 
  5.  
  6.   HttpCookie cookie; 
  7.   if (createPersistentCookie)//是否在设置的过期时间内一直有效 
  8.   { 
  9.     cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted) 
  10.     { 
  11.       HttpOnly = true, 
  12.       Path = FormsAuthentication.FormsCookiePath, 
  13.       Secure = FormsAuthentication.RequireSSL, 
  14.       Expires = ticket.Expiration, 
  15.       Domain = "cnblogs.com"//这里设置认证的域名,同域名下包括子域名如aa.cnblogs.com或bb.cnblogs.com都保持相同的登录状态 
  16.     }; 
  17.   } 
  18.   else 
  19.   { 
  20.     cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted) 
  21.     { 
  22.       HttpOnly = true, 
  23.       Path = FormsAuthentication.FormsCookiePath, 
  24.       Secure = FormsAuthentication.RequireSSL, 
  25.       //Expires = ticket.Expiration,//无过期时间的,浏览器关闭后失效 
  26.       Domain = "cnblogs.com" 
  27.     }; 
  28.   } 
  29.  
  30.   HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName); 
  31.   HttpContext.Current.Response.Cookies.Add(cookie); 
  32. } 

这样登录后,在同域名下的任何页面都可以得到用户状态

判断用户是否登录

 

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

得到当前的用户名

 

 
  1. public string GetCurrentUserId() 
  2. { 
  3. return _httpContext.User.Identity.Name; 
  4. } 

下面给大家一个具体的实例

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. { 
  15. protected void Page_Load(object sender, EventArgs e) 
  16. { 
  17.  
  18. } 
  19. protected void Button1_Click(object sender, EventArgs e) 
  20. {  
  21.  
  22. string connString = Convert.ToString(ConfigurationManager.ConnectionStrings["001ConnectionString"]); 
  23. //001ConnectionString是我在webconfig里配置的数据库连接。 
  24. SqlConnection conn = new SqlConnection(connString);  
  25. string strsql = "select * from User_table where User_name='" + UserName.Text + "' and Password='" + Password.Text + "'"; 
  26. SqlCommand cmd = new SqlCommand(strsql, conn); 
  27. conn.Open(); 
  28. SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
  29.  
  30. if (dr.Read()) 
  31. {  
  32. Response.Redirect("index.aspx"); 
  33. conn.Close(); 
  34. } 
  35. else 
  36. { 
  37. FailureText.Text = "登陆失败,请检查登陆信息!"; 
  38. conn.Close(); 
  39. Response.Write("<script language=javascript>alert('登陆失败!.');</script>"); 
  40. } 
  41. } 
  42.  
  43. protected void Button2_Click(object sender, EventArgs e) //文本框重置按钮 
  44. { 
  45. UserName.Text = ""; 
  46. Password.Text = ""; 
  47.  
  48. } 
  49. } 

下面是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教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表