.net實現(xiàn)網(wǎng)站用戶登錄認證
更新時間:2015年11月25日 08:43:05 投稿:hebedich
本文給大家介紹的是.net實現(xiàn)網(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;
}
}
得到當前的用戶名
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 = "";
}
}
下面是aspx頁面代碼:
<%@ 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>無標題頁</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"> 用戶登陸</td> </tr> <tr> <td style="width: 89px"> 用戶名:</td> <td style="width: 100px"> <asp:TextBox ID="UserName" runat="server" Wrap="False"></asp:TextBox></td> </tr> <tr> <td style="width: 89px"> 密碼:</td> <td style="width: 100px"> <asp:TextBox ID="Password" runat="server" TextMode="Password" Width="148px" Wrap="False" ></asp:TextBox></td> </tr> <tr> <td align="center" colspan="2" style="text-align: center"> <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)文章
Asp.Net 網(wǎng)站性能優(yōu)化之緩字決 (上) 緩沖寫數(shù)據(jù)
通常情況下Asp.Net 網(wǎng)站的底層數(shù)據(jù)存儲都是關(guān)系數(shù)據(jù)庫,關(guān)系數(shù)據(jù)庫資源比較昂貴,而且也很容易造成瓶頸。緩字決文章就是為大家介紹如何有效使用緩存,異步寫緩沖數(shù)據(jù)庫的壓力,從而保證網(wǎng)站的性能。2010-06-06
asp.net 自動將漢字轉(zhuǎn)換成拼音第一個字母
把漢字轉(zhuǎn)換成拼音第一個字母 的實現(xiàn)代碼2009-03-03
ASP.NET2.0服務(wù)器控件之自定義狀態(tài)管理
ASP.NET2.0服務(wù)器控件之自定義狀態(tài)管理...2006-09-09
ASP.NET MVC5網(wǎng)站開發(fā)用戶修改資料和密碼(六)
這篇文章主要介紹了ASP.NET MVC5網(wǎng)站開發(fā)用戶修改資料和密碼,本文即將結(jié)束member區(qū)域的用戶部分,感興趣的小伙伴們可以參考一下2015-09-09
asp.net+jquery ajax無刷新登錄的實現(xiàn)方法
asp.net+jquery ajax無刷新登錄的實現(xiàn)方法,需要的朋友可以參考一下2013-06-06
asp.net使用ODP即oracle連接方式的的防注入登錄驗證程序
這篇文章主要介紹了asp.net使用ODP即oracle連接方式的的防注入登錄驗證程序,需要的朋友可以參考下2014-05-05

