Simple online implementation and single-point Login Extension for Asp.net MVC users (I)

Source: Internet
Author: User

First, let's think about how to implement online user statistics. Here I will use IHttpModule to expand our online user module.

Create the required entity class

 

Code

Public class OnlineUser
{
/// <Summary>
/// Logon Username
/// </Summary>
Public string UserName {get; set ;}
 
/// <Summary>
/// Logon Time
/// </Summary>
Public DateTime LoginTime {get; set ;}
/// <Summary>
/// Last activity time
/// </Summary>
Public DateTime LastTime {get; set ;}
/// <Summary>
/// Last activity address
/// </Summary>
Public string LastActionUrl {get; set ;}
/// <Summary>
/// Logon IP Address
/// </Summary>
Public string LoginIp {get; set ;}
/// <Summary>
/// Whether it is a tourist
/// </Summary>
Public bool IsGuest {get; set ;}
/// <Summary>
/// Current session ID
/// </Summary>
Public string SessionID {get; set ;}
}

 

 

Then we can implement our IHttpModule

 

Code

Public class UserOnlineModule: IHttpModule
{
# Region IHttpModule Member

Public static List <Models. OnlineUser> OnlineList = null;
Private System. Timers. Timer updateTimer;
// Online user activity Timeout: minute. Default Value: 10 minutes
Private int timeOut = 10;
// Set the timer trigger period: millisecond. The default value is 1 minute.
Private double timeinteger = 60000;

Public void Init (HttpApplication context)
{
Context. AuthenticateRequest + = new EventHandler (context_AuthenticateRequest );
}

Void context_AuthenticateRequest (object sender, EventArgs e)
{
If (OnlineList = null)
OnlineList = new List <Models. OnlineUser> ();

UpdateTimer = new System. Timers. Timer ();
UpdateTimer. AutoReset = true;
UpdateTimer. Elapsed + = new System. Timers. ElapsedEventHandler (updateTimer_Elapsed );
UpdateTimer. Interval = timeInterval;
UpdateTimer. Start ();
}

Void updateTimer_Elapsed (object sender, System. Timers. ElapsedEventArgs e)
{
UpdateTimer. Stop ();
If (OnlineList. Count> 0)
OnlineList. RemoveAll (p => (DateTime. Now-p. LastTime). Minutes> = timeOut );
UpdateTimer. Interval = timeInterval;
UpdateTimer. Start ();
}

Public void Dispose ()
{

}
# Endregion
}

 

 

 

Yes, this is a simple IHttpModule implementation, but a simple timer is set to execute the RemoveAll operation once every minute. Next we will define an ActionFilter, add pages for online user records (because some pages in the project do not need to be added)

 

Code

Public class OnlineFilterAttribute: ActionFilterAttribute
{
Public override void OnActionExecuting (ActionExecutingContext filterContext)
{
String findName = filterContext. HttpContext. User. Identity. Name;
Bool isGuest = false;

If (filterContext. HttpContext. User. Identity. IsAuthenticated)
FindName = filterContext. HttpContext. User. Identity. Name;
Else
{
FindName = filterContext. HttpContext. Session. SessionID. ToUpper ();
IsGuest = true;
}

Models. OnlineUser userModel = UserOnlineModule. OnlineList. Find (p =>
P. UserName. Equals (findName, StringComparison. CurrentCultureIgnoreCase ));

If (userModel = null)
{
UserModel = new Models. OnlineUser ();
UserModel. UserName = findName;
UserModel. LoginTime = DateTime. Now;
UserModel. LastTime = DateTime. Now;
UserModel. LoginIp = filterContext. HttpContext. Request. UserHostAddress;
UserModel. LastActionUrl = filterContext. HttpContext. Request. Url. PathAndQuery;
UserModel. SessionID = filterContext. HttpContext. Session. SessionID. ToUpper ();
UserModel. IsGuest = isGuest;
UserOnlineModule. OnlineList. Add (userModel );

}
Else
{
UserModel. LastTime = DateTime. Now;
UserModel. LastActionUrl = filterContext. HttpContext. Request. Url. PathAndQuery;

}
}

 

 

By the way, it is very important to forget to register our IHttpModule, open the Web. config page, find the

<Add name = "OnlineList" type = "namespace. UserOnlineModule"/>

 

 

This makes statistics on online users easy to use.

 

[OnlineFilter]
Public ActionResult Index (string id)
{
Return View ();
}

 

You can simply mark the Filter on the Action. If you have a Controller that requires online statistics, you can simply mark one on the entire Controller. It's easy.

The next article will introduce how to use this simple online user to implement Single-point login restrictions. In fact, many friends may already know how to do this.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.