HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Simple tracking online users in ASP.NET

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
trackingsimpleonlinenetusersasp

Problem

I wrote simple online users tracking for my ASP.NET MVC project.

-
In Global.asax I added:

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

    if (currentContext != null)
    {
        if (!currentContext.Request.Browser.Crawler)
        {
            WebsiteVisitor currentVisitor = new WebsiteVisitor(currentContext);
            OnlineVisitorsContainer.Visitors[currentVisitor.SessionId] = 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);
    }
}

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


-
Here is my WebsiteVisitor class:

```
using System;
using System.Collections.Concurrent;
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 Ses

Solution

MY EYES!!!

Seriously, use indentation. I didn't even realize these were ifs on a cursory scan, which would be very dangerous for anyone editing this code (and the others like it):

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"];


Also, use braces. VS 2017 automatically offers to put them in, so you can fix everything in the project at once and just use them in the future.

Not sure which version of C# you are using, but if you are using C# 6 (and you really have no good reason not to), you can simplify this statement:

if (context != null && context.Request != null && context.Session != null)


to

if (context?.Request != null && context.Session != null)


Also, I'd make that (and most of your other ifs) a guard clause and remove the extra level of indentation:

if (context?.Request == null || context.Session == null)
{
    return;
}


Additional indentation levels add complexity, which increases the work you must do to understand the code.

Code Snippets

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 != null && context.Request != null && context.Session != null)
if (context?.Request != null && context.Session != null)
if (context?.Request == null || context.Session == null)
{
    return;
}

Context

StackExchange Code Review Q#119267, answer score: 3

Revisions (0)

No revisions yet.