您好,登錄后才能下訂單哦!
如何在.net項(xiàng)目中實(shí)現(xiàn)一個(gè)網(wǎng)站用戶(hù)登錄認(rèn)證功能?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
cookie登錄后同域名下的網(wǎng)站保持相同的登錄狀態(tài)。
登錄
private void SetAuthCookie(string userId, bool createPersistentCookie) { var ticket = new FormsAuthenticationTicket(2, userId, DateTime.Now, DateTime.Now.AddDays(7), true, "", FormsAuthentication.FormsCookiePath); string ticketEncrypted = FormsAuthentication.Encrypt(ticket); HttpCookie cookie; if (createPersistentCookie)//是否在設(shè)置的過(guò)期時(shí)間內(nèi)一直有效 { cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted) { HttpOnly = true, Path = FormsAuthentication.FormsCookiePath, Secure = FormsAuthentication.RequireSSL, Expires = ticket.Expiration, Domain = "cnblogs.com"//這里設(shè)置認(rèn)證的域名,同域名下包括子域名如aa.cnblogs.com或bb.cnblogs.com都保持相同的登錄狀態(tài) }; } else { cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted) { HttpOnly = true, Path = FormsAuthentication.FormsCookiePath, Secure = FormsAuthentication.RequireSSL, //Expires = ticket.Expiration,//無(wú)過(guò)期時(shí)間的,瀏覽器關(guān)閉后失效 Domain = "cnblogs.com" }; } HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName); HttpContext.Current.Response.Cookies.Add(cookie); }
這樣登錄后,在同域名下的任何頁(yè)面都可以得到用戶(hù)狀態(tài)
判斷用戶(hù)是否登錄
public bool IsAuthenticated { get { bool isPass = System.Web.HttpContext.Current.User.Identity.IsAuthenticated; if (!isPass) SignOut(); return isPass; } }
得到當(dāng)前的用戶(hù)名
public string GetCurrentUserId() { return _httpContext.User.Identity.Name; }
下面給大家一個(gè)具體的實(shí)例
CS頁(yè)代碼:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string connString = Convert.ToString(ConfigurationManager.ConnectionStrings["001ConnectionString"]); //001ConnectionString是我在webconfig里配置的數(shù)據(jù)庫(kù)連接。 SqlConnection conn = new SqlConnection(connString); string strsql = "select * from User_table where User_name='" + UserName.Text + "' and Password='" + Password.Text + "'"; SqlCommand cmd = new SqlCommand(strsql, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (dr.Read()) { Response.Redirect("index.aspx"); conn.Close(); } else { FailureText.Text = "登陸失敗,請(qǐng)檢查登陸信息!"; conn.Close(); Response.Write("<script language=javascript>alert('登陸失敗!.');</script>"); } } protected void Button2_Click(object sender, EventArgs e) //文本框重置按鈕 { UserName.Text = ""; Password.Text = ""; } }
下面是aspx頁(yè)面代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=" http://www.w3.org/1999/xhtml" > <head runat="server"> <title>無(wú)標(biāo)題頁(yè)</title> </head> <body> <form id="form1" runat="server"> <asp:Panel ID="Panel1" runat="server" Height="101px" Width="231px" Wrap="False"> <table> <tr> <td align="center" colspan="2"> 用戶(hù)登陸</td> </tr> <tr> <td > 用戶(hù)名:</td> <td > <asp:TextBox ID="UserName" runat="server" Wrap="False"></asp:TextBox></td> </tr> <tr> <td > 密碼:</td> <td > <asp:TextBox ID="Password" runat="server" TextMode="Password" Width="148px" Wrap="False" ></asp:TextBox></td> </tr> <tr> <td align="center" colspan="2" > <asp:Button ID="Button1" runat="server" Text="登陸" Width="50px" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="重置" Width="50px" OnClick="Button2_Click" /></td> </tr> <tr> <td align="center" colspan="2"> <asp:Label ID="FailureText" runat="server" Width="77px"></asp:Label></td> </tr> </table> </asp:Panel> </form> </body> </html>
關(guān)于如何在.net項(xiàng)目中實(shí)現(xiàn)一個(gè)網(wǎng)站用戶(hù)登錄認(rèn)證功能問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。