日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

单点登陆的ASP.NET应用程序设计[zt]

發(fā)布時間:2025/5/22 asp.net 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单点登陆的ASP.NET应用程序设计[zt] 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
大致有三種處理方式.
????一,把用戶登陸信息記錄在數(shù)據(jù)庫中,每次登陸去數(shù)據(jù)庫里查找用戶登陸狀態(tài),這種處理方式存在一種特列,如果用戶非正常退出,容易出現(xiàn)死鎖的情況.
????二,把用戶信息記錄在COOKEE中.這種方式?jīng)]有進行研究,具體問題和風險有待研究.
????三,利用SESSION來保存用戶信息.這種方式?HttpApplicationState保存了大量數(shù)據(jù),對性能有一定的風險,暫時20個點登陸,還未出現(xiàn)問題.
????下面就第三種處理方式介紹如下:
????基礎(chǔ)知識:
????HttpContext類:封裝有關(guān)個別 HTTP 請求的所有 HTTP 特定的信息。?
????HttpApplicationState類:啟用 ASP.NET 應用程序中多個會話和請求之間的全局信息共享。
????HttpSessionState類:提供對會話狀態(tài)值以及會話級別設(shè)置和生存期管理方法的訪問
Code
首先,設(shè)置web.config中的<appSettings>??
?????
<appSettings>
?????
<add?key="SQLServerConnStr"?value="uid=sa;password=123;database=Northwind;data?source=(local)"/>?
???
</appSettings>
????和認證方式
????
<authentication?mode="Forms"?>
??
<forms??name=".ASPXAUTH"
??????loginUrl
="WebForm1.aspx"?
??????protection
="All"??
??????timeout
="30"?
??????path
="/"?>
??
??
</forms>
????
</authentication>?
Code
using?System;
using?System.Collections.Specialized;
using?System.Data;
using?System.Data.SqlClient;
using?System.Web;
using?System.Web.Security;
using?System.Collections;
using?System.Security;
using?System.Configuration;


namespace?SingleLoginTest
{
?
///?<summary>
?
///?BLL?的摘要說明。
?
///?</summary>
?public?class?BLL
?{
??
private?static?string?_SqlConn?=?ConfigurationSettings.AppSettings["SQLServerConnStr"].ToString();
??

??
public?BLL()
??{
???
//
???
//?TODO:?在此處添加構(gòu)造函數(shù)邏輯
???
//
???
//_SqlConn?=?ConfigurationSettings.AppSettings["SQLServerConnStr"].ToString();
???

??}

??
///?<summary>
??
///?用戶登陸
??
///?</summary>
??
///?<param?name="userId"></param>
??
///?<returns></returns>
??public?static?bool?Login(string?userId)
??{
???
//初試返回變量
???bool?retcode?=?false;

???
if?(userId?==?"")
????
return?retcode;

???
//查詢此用戶存不存在
???if?(GetUser(userId)?==?false)
???{
????
return?retcode;
???}
???
else
???{
????retcode?
=?true?;
???}

???
//判斷有沒有登陸過
???IsUserExist(?userId);

???HttpContext.Current.Session[
"Name"]=userId;
???FormsAuthentication.RedirectFromLoginPage(userId,?
false);
???FormsAuthentication.SetAuthCookie(userId,?
false);
???
//寫認證
???Hashtable?_hash?=?(Hashtable)HttpContext.Current.Application["online"];
???
if?(_hash?==?null)
???{
????_hash?
=?new?Hashtable();
????HttpContext.Current.Application.Add(
"online",_hash);
???}

???_hash[HttpContext.Current.Session.SessionID]?
=?HttpContext.Current.Session?;
???HttpContext.Current.Application[
"online"]?=?_hash;
??
???
return?retcode;
???
//返回

??}

??
///?<summary>
??
///?判斷用戶是否在線
??
///?</summary>
??
///?<param?name="userId"></param>
??
///?<returns></returns>
??public?static?bool?IsUserExist(string?userId)
??{
???
bool?retcode?=?false;
???Hashtable?_table?
=?(Hashtable)HttpContext.Current.Application["online"];
???
if?(_table?==?null)
????
return?retcode;

???IDictionaryEnumerator?e?
=?_table.GetEnumerator();
???
while?(e.MoveNext())
???{
????System.Web.SessionState.HttpSessionState?session?
=?(System.Web.SessionState.HttpSessionState)e.Value;
????
if?(session?==?null)
?????
continue;
????
string?tmpuserId?=?session["Name"].ToString();
????
if?(?userId?==?tmpuserId?)???//存在用戶
????{
?????
//清除他
?????session.Clear();
?????session.Abandon();
?????_table.Remove(e);
?????HttpContext.Current.Application[
"online"]?=?_table;

?????
string?script?=?"<script?language=javascript>alert('在異地登陸,迫使他下線!')</script>";

?????HttpContext.Current.Response.Write(script);

?????retcode?
=?true;
?????
break;
????}
???}

???
return?retcode;

??}


??
///?<summary>
??
///?注銷
??
///?</summary>
??public?static?void??LoginOut()
??{
???
//?Clear?the?authentication?ticket
???FormsAuthentication.SignOut();
???
//?Clear?the?contents?of?their?session
???HttpContext.Current.Session.Clear();
???
//?Tell?the?system?to?drop?the?session?reference?so?that?it?does?
???
//?not?need?to?be?carried?around?with?the?user
???HttpContext.Current.Session.Abandon();
??}

??
??
///?<summary>
??
///?刪除用戶
??
///?</summary>
??
///?<param?name="userId"></param>
??public?static?void?Remove(string?userId)
??{
???Hashtable?_table?
=?(Hashtable)HttpContext.Current.Application["online"];

???
if?(_table?==?null)
????
return?;

???IDictionaryEnumerator?e?
=?_table.GetEnumerator();
???
while?(e.MoveNext())
???{
????System.Web.SessionState.HttpSessionState?session?
=?(System.Web.SessionState.HttpSessionState)e.Value;
????
string?_userid?=?session["Name"].ToString();
????
if?(_userid?==?userId)
????{
?????session.Clear();
?????session.Abandon();
?????_table.Remove(e);
?????HttpContext.Current.Application[
"online"]?=?_table;
?????
break;
????}
???}

??}

}

轉(zhuǎn)載于:https://www.cnblogs.com/bobofsj11/archive/2009/09/02/1558736.html

總結(jié)

以上是生活随笔為你收集整理的单点登陆的ASP.NET应用程序设计[zt]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。