人本善良

导航

simple demo how to get the list of online users

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;
}
}
}
}

posted on 2016-02-18 15:07  简简单单2018  阅读(245)  评论(0编辑  收藏  举报