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. Tomcat6.0 管理器配置

    最近忙着毕业答辩,填写材料,好多事情都给耽搁了!一个月都没有继续翻译tomcat,这回有点时间赶紧补上. 这部分,其实对开发者或者tomcat管理者来说,只要会登录页面管理器或者使用写简单的http就 ...

  2. ajax浅析---ScriptManager

    使用ScriptManager控件 它用来处理页面上的所有组件以及页面局部更新,生成相关的客户端代理脚本以便能够在JavaScript中访问Web Service,所有需要支持ASP.NET AJAX ...

  3. protect和private 的区别

    protect和private 的区别 public 表示全局,类内部外部子类都可以访问: private表示私有的,只有本类内部可以使用: protected表示受保护的,只有本类或子类或父类中可以 ...

  4. mysql 表锁问题

    select * from information_schema.INNODB_lock_waits; select * from information_schema.INNODB_locks; S ...

  5. cf#306D. Regular Bridge(图论,构图)

    D. Regular Bridge time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Linux C select函数详解

    select IO复用机制: http://www.cnblogs.com/hjslovewcl/archive/2011/03/16/2314330.html http://blog.csdn.ne ...

  7. CSS使用自定义光标样式-遁地龙卷风

    测试环境是chrome浏览器 Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357. ...

  8. (原)android中的动画(二)

    帧动画的使用需要在xml文件中指定每一帧所对应的图片 animation-list写法如下: <?xml version="1.0" encoding="utf-8 ...

  9. 试用vSphere 6(三):安装vCenter 6(独立数据库)之:vCenter安装与配置

    ------------------------------------------ 一.VMware vSphere 6(RC版)安装配置系列文章: 1.试用vSphere 6(一):安装ESXi ...

  10. vmware vcenter orchestrator configuration提示“用户名密码错误或登录失败超过次数被锁定”

    首次登录,使用默认用户密码登录vmware/vmware vmware vcenter orchestrator configuration提示"用户名密码错误或登录失败超过次数被锁定&qu ...