婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > .net實(shí)現(xiàn)網(wǎng)站用戶登錄認(rèn)證

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

熱門標(biāo)簽:400電話辦理哪家性價(jià)比高 地圖定位圖標(biāo)標(biāo)注 地圖標(biāo)注的公司有哪些 濮陽外呼電銷系統(tǒng)怎么樣 地圖標(biāo)注專業(yè)團(tuán)隊(duì) 塔城代理外呼系統(tǒng) 代理接電話機(jī)器人如何取消 遂寧市地圖標(biāo)注app 天心智能電銷機(jī)器人

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è)置的過期時(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è)置認(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,//無過期時(shí)間的,瀏覽器關(guān)閉后失效
      Domain = "cnblogs.com"
    };
  }

  HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
  HttpContext.Current.Response.Cookies.Add(cookie);
}

這樣登錄后,在同域名下的任何頁(yè)面都可以得到用戶狀態(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;
}

下面給大家一個(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>無標(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">
用戶登陸/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>

您可能感興趣的文章:
  • ASP.NET MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)
  • C#微信公眾號(hào)與訂閱號(hào)接口開發(fā)示例代碼
  • C#微信開發(fā)之微信公眾號(hào)標(biāo)簽管理功能
  • C#開發(fā)微信公眾號(hào)接口開發(fā)
  • C#微信公眾號(hào)開發(fā)之接收事件推送與消息排重的方法
  • C#實(shí)現(xiàn)微信公眾號(hào)群發(fā)消息(解決一天只能發(fā)一次的限制)實(shí)例分享
  • .NET微信公眾號(hào)開發(fā)之公眾號(hào)消息處理
  • .NET微信公眾號(hào)開發(fā)之查詢自定義菜單
  • .NET微信公眾號(hào)開發(fā)之創(chuàng)建自定義菜單
  • .NET C#使用微信公眾號(hào)登錄網(wǎng)站

標(biāo)簽:宜春 本溪 吉林 河南 婁底 重慶 麗江 汕頭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《.net實(shí)現(xiàn)網(wǎng)站用戶登錄認(rèn)證》,本文關(guān)鍵詞  .net,實(shí)現(xiàn),網(wǎng)站,用戶,登錄,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《.net實(shí)現(xiàn)網(wǎng)站用戶登錄認(rèn)證》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于.net實(shí)現(xiàn)網(wǎng)站用戶登錄認(rèn)證的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 德昌县| 长汀县| 博乐市| 来凤县| 青川县| 修水县| 沈阳市| 铜陵市| 延长县| 高唐县| 西宁市| 察隅县| 青河县| 新乡县| 栖霞市| 德惠市| 公主岭市| 迁西县| 湘阴县| 兖州市| 峨山| 金堂县| 阿鲁科尔沁旗| 博野县| 吴旗县| 新昌县| 镇沅| 苍梧县| 平山县| 常宁市| 北安市| 昭觉县| 方城县| 清苑县| 兴城市| 平乡县| 兰西县| 平利县| 马边| 开阳县| 蓬安县|