Ever want to create a list of logged in users for a site, maybe for the Admin view or all users?
Here's how:
In your AccountController add a simple class:
public class LoggedInUsers
{
public string MemberName { get; set; }
public DateTime LoggedInTime { get; set; }
public LoggedInUsers( string memberName, DateTime loggedInTime )
{
MemberName = memberName;
LoggedInTime = loggedInTime;
}
}
Then in your AccountController add a:
public static List<LoggedInUsers> OnlineUsers;
Then in your AccountController's [HtmlPost] Login(...) method add this line for authenticated users:
AccountController.OnlineUsers.Add( new LoggedInUsers( details.MemberName, DateTime.Now ) );
This adds a new user to the OnlineUsers list.
To remove a entry from the list:
In your Logout() method (you have one of these, right?):
Add this:
if ( AccountController.OnlineUsers.Count > 0 )
{
If( AccountController.OnlineUsers.Count > 0 )
{
LoggedInUsers member = AccountController.OnlineUsers.First( m => m.MemberName == User.Identity.Name );
AccountController.OnlineUsers.Remove( member );
}
}
Just before this:
FormsAuthentication.SignOut();
There is one other place you'll need to add some code and that is in your global.asax Session_End method like this:
protected static void Session_End( object Sender, EventArgs e )
{
if( AccountController.OnlineUsers.Count > 0 )
{
LoggedInUsers member AccountController.OnlineUsers.Find( m => m.MemberName == HttpContext.Current.User.Identity.Name );
if( member != null )
AccountController.OnlineUsers.Remove( member );
}
}
We need to check here if the member is null because if a user logged out he/she will already be removed from the OnlineUsers list.
Next you need to create a partial view method in your controller:
public PartialViewResult OnlineUsers()
{
List<LoggedInUsers> data = AccountController.OnlineUsers;
return PartialView( data );
}
Now you can create a partial view for logged in users to see:
@model List<My_MSI.Net.Controllers.LoggedInUsers>
@foreach ( My_MSI.Net.Controllers.LoggedInUsers user in Model )
{
@Html.Label( @Html.Lable( String.Format( "{0} logged in at {1}", user.MemberName, user.LoggedInTime.Tostring() ) )<br />
}
Finally in your _Layout.cshtml file (You do have one of these, right?):
<div class="move-right">
@{ Html.RenderAction( "OnlienUsers", "Your controllers name" ); }
</div>
Now you logged in users can see who else is logged in.
If you want to show logged in users you'll have to create this View in your Home controller:
public ActionResult OnlineUsers()
{
List<LoggedInUsers> data = AccountController.OnlineUsers;
return View( data );
}
And a View:
@model List<My_MSI.Net.Controllers.LoggedInUser{@ViewBagTitle = "OnlineUsers";Layout = "~/Views/Shared/_Layout.cshtml";}<h2>OnlineUsers</h2>
@foreach ( My_MSI.Net.Controllers.LoggedInUsers user in Model )
{
@Html.Label( String.Format "{0} logged in at {1}", user.MemberName, user.LoggedInTime.ToString() )<br />
}
Finally add a new menu item for Online Users and link it back to your OnlineUsers() method.
That's it. You've successfully created a list of Online Users that Logged in members can see and a link were the general public can see who's logged in.
No comments:
Post a Comment