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. assign与weak区别(面试)

    weak 比 assign 多了一个功能就是当属性所指向的对象消失的时候(也就是内存引用计数为0)会自动赋值为 nil ,这样再向 weak 修饰的属性发送消息就不会导致野指针操作crash. 可能不 ...

  2. mac安装mongodb

    一,安装方法1 ,下载mongodb 1,官网下载mongodb程序 https://www.mongodb.org/downloads#production​ 2,解压后启动mongodb服务 下载 ...

  3. IGV软件

    它支持各种各样的数据类型,包括基于芯片测序.二代测序数据和基因组注释数据等.整合基因组浏览器(IGV,Integrative Genomics Viewer)进行可视化浏览,它支持各种各式的数据类型, ...

  4. 实体ip 虚拟ip 固定ip 动态ip

    实体 IP:在网络的世界里,为了要辨识每一部计算机的位置,因此有了计算机 IP 位址的定义.一个 IP 就好似一个门牌!例如,你要去微软的网站的话,就要去『 207.46.197.101 』这个 IP ...

  5. plsql 建立oracle作业

    --.plsql中学习job --学习job --建表 create table test_job(para_date date); commit; insert into test_job valu ...

  6. CMake Error: your CXX compiler: "" was not found

    [root@amax src]# cmake . -- The CXX compiler identification is unknown CMake Error at /usr/local/sha ...

  7. Eclipse的link方式安装JBPM6插件(JBPM学习之一)

    1. 首先下载最新的JAVA开发最受欢迎的Eclipse IDE工具,下载地址:http://www.eclipse.org/downloads/ 2. 然后去JBPM社区去下载最新的JBPM6,下载 ...

  8. 在framework中打包xib

    废话不多说,直接上图 1.Copy Bundle Resources 中加入相关xib 2.这里是重点,调用的时候不能直接写 [[NSBundle mainBundle] loadNibNamed:@ ...

  9. Android学习笔记(二十)——自定义内容提供器

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 如果我们想要实现跨程序共享数据的功能,官方推荐的方式就是使用内容提供器,可以通过新建一个类去继承 Conten ...

  10. HDU——PKU题目分类

    HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...