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

首頁 > 編程 > .NET > 正文

.net實現(xiàn)網(wǎng)站用戶登錄認證

2024-07-10 12:47:38
字體:
供稿:網(wǎng)友

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è)置的過期時間內(nèi)一直有效  {    cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted)    {      HttpOnly = true,      Path = FormsAuthentication.FormsCookiePath,      Secure = FormsAuthentication.RequireSSL,      Expires = ticket.Expiration,      Domain = "cnblogs.com"http://這里設(shè)置認證的域名,同域名下包括子域名如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,//無過期時間的,瀏覽器關(guān)閉后失效      Domain = "cnblogs.com"    };  }  HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);  HttpContext.Current.Response.Cookies.Add(cookie);}

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

判斷用戶是否登錄

public bool IsAuthenticated{  get  {    bool isPass = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;    if (!isPass)      SignOut();    return isPass;  }}

得到當(dāng)前的用戶名

public string GetCurrentUserId(){   return _httpContext.User.Identity.Name;}

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

CS頁代碼:

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ù)庫連接。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 = "登陸失敗,請檢查登陸信息!";conn.Close();Response.Write("<script language=javascript>alert('登陸失敗!.');</script>");}}protected void Button2_Click(object sender, EventArgs e) //文本框重置按鈕{UserName.Text = "";Password.Text = "";}}            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 渭源县| 西乌| 花垣县| 奇台县| 聂拉木县| 柏乡县| 琼海市| 靖边县| 隆化县| 成武县| 美姑县| 建湖县| 古丈县| 岗巴县| 于田县| 贡山| 南木林县| 建平县| 溧阳市| 大新县| 屯门区| 无为县| 晴隆县| 垦利县| 彰武县| 斗六市| 新巴尔虎右旗| 文登市| 广汉市| 突泉县| 辉县市| 琼中| 大宁县| 孟连| 广汉市| 晋城| 开封市| 蕉岭县| 龙口市| 合川市| 乐安县|