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. Slave_SQL_Running: No mysql同步故障解决方法

    Slave_SQL_Running: No mysql同步故障解决      今天检查数据库发现一台MySQL Slave未和主机同步,查看Slave状态:mysql> show slave s ...

  2. mysql sql语句执行时间查询

    第一种:show profiles 之类的语句来查看 1.查一下profile是不是打开了,默认是不打开的. mysql> show profiles; Empty set (0.02 sec) ...

  3. C#之规格说明书

    1. Statement My Note Problem Statement Version 1.0   Revision History Date Issue Description Author ...

  4. Javascript高级程序设计——函数声明与函数表达式的区别

    在Javascript中,函数是Functioin类型的实例,所以函数也具备属性和方法,因为函数是对象,所以函数名自然就是指向对象的指针啦. 函数可以通过声明语法和表达式来定义: 声明:functio ...

  5. Lvs原理

    官方文档: http://www.linuxvirtualserver.org/zh/lvs1.html http://www.linuxvirtualserver.org/zh/lvs2.html ...

  6. 演示Android百度地图操作功能

    在本文中将演示百度地图的操作功能,包括缩放,旋转,视角切换,点击,双击,长按事件触发的操作以及截图等.百度地图本来就内置有缩放,旋转功能,那么在这里,截图(其实很多手机也自带截图功能)以及点击事件的监 ...

  7. 笔记(一):ES6所改良的javascript“缺陷”

    ES6笔记(一):ES6所改良的javascript“缺陷”   块级作用域 ES5没有块级作用域,只有全局作用域和函数作用域,由于这一点,变量的作用域甚广,所以一进入函数就要马上将它创建出来.这就造 ...

  8. 12个Linux进程管理命令介绍

    导读 执行中的程序称作进程.当程序可以执行文件存放在存储中,并且运行的时候,每个进程会被动态得分配系统资源.内存.安全属性和与之相关的状态.可以有多个进程关联到同一个程序,并同时执行不会互相干扰.操作 ...

  9. win32程序组成

    程序代码+UI资源——RC编译器整合——>EXE档案. UI资源:二进制代码(借助工具产生,并以各种扩展名的文件存在),程序员必须在资源描述文档(.rc)中描述他们. RC编译器(RC.EXE) ...

  10. BZOJ 1057: [ZJOI2007]棋盘制作

    Decsription 给你一个矩阵,求最大了 01相间 的矩阵. Sol DP+悬线法. 这是一个论文啊 <浅谈用极大化思想解决最大子矩形问题>--王知昆. 枚举每一根悬线,记录最左/右 ...