using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebApplication.Areas.Admin.Models;

namespace TestOnlie
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string a = null;
//string b = a ?? "测试";
//Response.Write(Global.ht[0]);
//foreach (DictionaryEntry d in Global.ht)
//{
// Response.Write("IP:"+d.Key+" | "+"用户名:"+d.Value.ToString().Split('&')[0].Split('=')[1]+"<br/>");
//}
OnlineVisitorsContainer.VisitorFitler = OnlineVisitorsContainer.Visitors;
FilterTimeOutUsers();
foreach (var d in OnlineVisitorsContainer.VisitorFitler)
{
var context = (WebsiteVisitor) d.Value;
Response.Write("IP:" + context.IpAddress + " | " + "用户名:" + context.AuthUser + "<br/>");
}
}

public void FilterTimeOutUsers()
{
foreach (var d in OnlineVisitorsContainer.Visitors)
{ var context = (WebsiteVisitor) d.Value;
if (DateTime.Now.CompareTo(context.SessionStarted.AddMinutes(10)) > 0)
{

OnlineVisitorsContainer.VisitorFitler.Remove(d.Key);
}
}
}
}

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

using System;

using System.Collections.Generic;
using System.Web;

namespace WebApplication.Areas.Admin.Models
{
public class WebsiteVisitor
{

public string SessionId { get; set; }

public string IpAddress { get; set; }

public string AuthUser { get; set; }

public string UrlReferrer { get; set; }

public string EnterUrl { get; set; }

public string UserAgent { get; set; }

public DateTime SessionStarted { get; set; }

public WebsiteVisitor(HttpContext context)
{
if (context != null && context.Request != null && context.Session != null)
{
this.SessionId = context.Session.SessionID;

this.SessionStarted = DateTime.Now;

//this.UserAgent = String.IsNullOrEmpty(context.Request.UserAgent) ? "" : context.Request.UserAgent;
this.UserAgent = context.Request.UserAgent ?? String.Empty;

this.IpAddress = context.Request.UserHostAddress;

//-------------------------------------------------------------
if (context.Request.IsAuthenticated)
{
this.AuthUser = context.User.Identity.Name;
if (!String.IsNullOrEmpty(context.Request.ServerVariables["REMOTE_USER"]))
this.AuthUser = context.Request.ServerVariables["REMOTE_USER"];
else if (!String.IsNullOrEmpty(context.Request.ServerVariables["AUTH_USER"]))
this.AuthUser = context.Request.ServerVariables["AUTH_USER"];
}

//-------------------------------------------------------------
if (context.Request.UrlReferrer != null)
{
this.UrlReferrer = String.IsNullOrEmpty(context.Request.UrlReferrer.OriginalString) ? "" : context.Request.UrlReferrer.OriginalString;
}

this.EnterUrl = String.IsNullOrEmpty(context.Request.Url.OriginalString) ? "" : context.Request.Url.OriginalString;
}
}
}

/// <summary>
/// Online visitors list
/// </summary>
public static class OnlineVisitorsContainer
{
public static Dictionary<string, WebsiteVisitor> Visitors = new Dictionary<string, WebsiteVisitor>();
public static Dictionary<string, WebsiteVisitor> VisitorFitler = new Dictionary<string, WebsiteVisitor>();

}
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using WebApplication.Areas.Admin.Models;

namespace TestOnlie
{
//public class Global : System.Web.HttpApplication
//{
// public static Hashtable ht=new Hashtable();
// protected void Application_Start(object sender, EventArgs e)
// {

// HttpContext currentContext = HttpContext.Current;
// string UsersPool= currentContext.Request.Params[0];
// ht.Add(currentContext.Request.UserHostAddress, currentContext.Request.Params[0]);
// }

// protected void Session_Start(object sender, EventArgs e)
// {

// }

// protected void Application_BeginRequest(object sender, EventArgs e)
// {

// }

// protected void Application_AuthenticateRequest(object sender, EventArgs e)
// {

// }

// protected void Application_Error(object sender, EventArgs e)
// {

// }

// protected void Session_End(object sender, EventArgs e)
// {

// }

// protected void Application_End(object sender, EventArgs e)
// {

// }
//}

public class Global : System.Web.HttpApplication
{

protected void Session_Start(Object sender, EventArgs e)
{
// get current context
HttpContext currentContext = HttpContext.Current;

if (currentContext != null)
{
if (!currentContext.Request.Browser.Crawler)
{
var currentVisitor = new WebsiteVisitor(currentContext);
OnlineVisitorsContainer.Visitors.Remove(currentVisitor.IpAddress);
OnlineVisitorsContainer.Visitors[currentVisitor.IpAddress] = currentVisitor;

//if (!OnlineVisitorsContainer.Visitors.ContainsKey(currentVisitor.IpAddress))
//{
// OnlineVisitorsContainer.Visitors[currentVisitor.IpAddress] = currentVisitor;
//}
//else
//{
// OnlineVisitorsContainer.Visitors[currentVisitor.IpAddress] = currentVisitor;
//}
}
}
}

protected void Session_End(Object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.

if (this.Session != null)
{
WebsiteVisitor visitor;
//OnlineVisitorsContainer.Visitors.TryRemove(this.Session.SessionID, out visitor);
OnlineVisitorsContainer.Visitors.Remove(this.Session.SessionID);

}
}

protected void Application_PreRequestHandlerExecute(object sender, EventArgs eventArgs)
{
var session = HttpContext.Current.Session;
if (session != null && HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
{
if (OnlineVisitorsContainer.Visitors.ContainsKey(session.SessionID))
OnlineVisitorsContainer.Visitors[session.SessionID].AuthUser = HttpContext.Current.User.Identity.Name;
}
}
}
}

simple demo how to get the list of online users的更多相关文章

  1. Learning WCF:A Simple Demo

    This is a very simple demo which can help you create a wcf applition quickly. Create a Solution Open ...

  2. simple demo of Handlebars.js & jquery.js

    simple demo of Handlebars.js & jquery.js <html> <head> <script src="jquery-1 ...

  3. ionic cordova plugin simple demo

    要用cordova plugin 的话还是需要设置一下的 1. 下载 ng-cordova.js download the zip file here 2. 在index.html 中引用 (cord ...

  4. C# - Delegate Simple Demo

  5. FusionCharts simple demo for (html+js、APS.NET Webform、MVC)

    做GIS或其他内部数据统计项目的应该对FusionCharts也不会太陌生,简单易用已无需多说什么了,只是有时候框架不同,实现起来也稍有差异 引用dll调用FusionCharts类的静态方法Rend ...

  6. Step by Step Do IOS Swift CoreData Simple Demo

    简单介绍 这篇文章记录了在 IOS 中使用 Swift 操作 CoreData 的一些基础性内容,因为缺乏文档,基本上都是自行实验的结果.错漏不可避免,还请谅解. 部分内容借鉴了 Tim Roadle ...

  7. Java Udp Socket Simple Demo

    import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import j ...

  8. setCapture、releasCapture 浅析

    1. setCapture 简介 setCapture可以将鼠标事件锁定在指定的元素上,当元素捕获了鼠标事件后,该事件只能作用在当前元素上. 以下情况会导致事件锁定失败: 当窗口失去焦点时,锁定的事件 ...

  9. Essential controls for web app

    AUTO-COMPLETE/AUTO-SUGGEST Auto-complete using Vaadin Offer auto-suggest or auto-complete to help yo ...

随机推荐

  1. 使用Minify来优化网站性能

    Minify 是用PHP5开发的应用,通过遵循一些Yahoo的优化规则来提高网站的性能.它会合并多个CSS或者JavaScript文件,移除一些不必要的空格和注释,进行gzip压缩,并且会设置浏览器的 ...

  2. Exception→"Source":"EntityFramework" "Message" :"更新条目时出错。有关详细信息,请参阅内部异常。"

    给一个数据库中类型为"datetime"的列赋值为 "DateTime.MinValue"...... 而// ::} But--01到9999-- :: 到 ...

  3. ML_R kNN

    邻近算法 K最近邻(kNN,k-NearestNeighbor)分类算法是数据挖掘分类技术中最简单的方法之一.所谓K最近邻,就是k个最近的邻居的意思,说的是每个样本都可以用它最接近的k个邻居来代表. ...

  4. NHibernate配置

    因为NHibernate被设计为可以在许多不同环境下工作,所以它有很多配置参数.幸运的是,大部分都已经有默认值了. NHibernate.Test.dll包含了一个示例的配置文件app.config, ...

  5. 怎样给WordPress分配更多的内存

    WordPress如果内存不够,你在操作的时候,就会碰到像这样的问题”Allowed memory size of xxxxxx bytes exhausted”(允许的内存 xxxx 字节已经用光了 ...

  6. mysql索引优化

    mysql 索引优化 >mysql一次查询只能使用一个索引.如果要对多个字段使用索引,建立复合索引. >越小的数据类型通常更好:越小的数据类型通常在磁盘.内存和CPU缓存中都需要更少的空间 ...

  7. window使用qt遇到的坑

    1.window上的qt对图片的检测与识别不够完善.往往改了一个ui的背景图片,运行出来显示的却是旧的背景图片. 原因:  由于之前是项目与项目之间整合在一起,然后把ui_*_ui.h的临时文件也放在 ...

  8. 百度浏览器+hao123评价

    1.用户界面: 界面比较简洁,没有多余的没用的东西.在界面上部有天气,比较方便用户查看天气,中间有各个实用性网站和大家通常使用较多的网站,可以比较快的查看.侧栏有些比较有针对性的内容.上部还有可以静音 ...

  9. 据说Linuxer都难忘的25个画面

    导读 虽然对 Linux 正式生日是哪天还有些争论,甚至 Linus Torvalds 认为在 1991 那一年有四个日子都可以算作 Linux 的生日.但是不管怎么说,Linux 已经 25 岁了, ...

  10. 表单select相关

    selectedIndex 属性可设置或返回下拉列表中被选选项的索引号. options[] 返回包含下拉列表中的所有选项的一个数组. add()向下拉列表添加一个选项. blur()从下拉列表移开焦 ...