I have an application where I would like to show list of all the logged in users in a partial view. I know we can do it using memberships, but not sure how to implement it exactly.
my Controller code
[Authorize(Roles = "Admin")]
public ActionResult ActiveUsers()
{
//ViewBag.Message = "Your contact page.";
// MembershipUserCollection users;
// Bind users to ListBox.
List<MembershipUser> onlineUsers = new List<MembershipUser>();
foreach (MembershipUser user in Membership.GetAllUsers())
{
if (user.IsOnline)
{
onlineUsers.Add(user);
HttpRuntime.Cache["onlineUsers"] = onlineUsers;
}
}
return View();
}
View
@if (HttpRuntime.Cache["onlineUsers"] != null)
{
List<string> LoggedOnUsers = (List<string>)HttpRuntime.Cache["onlineUsers"];
if (LoggedOnUsers.Count > 0)
{
<div class="ChatBox">
<ul>
@foreach (string user in LoggedOnUsers)
{
<li>
<div class="r_row">
<div class="r_name">@Html.Encode(user)</div>
</div>
</li>
}
</ul>
</div>
}
}
With the above code when I access the active users page, it threw me following exception
Server Error in '/' Application.
Specified method is not supported.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: Specified method is not supported.
Source Error:
Line 58: // Bind users to ListBox.
Line 59: List<MembershipUser> onlineUsers = new List<MembershipUser>();
Line 60: foreach (MembershipUser user in Membership.GetAllUsers())
Line 61: {
Line 62:
Can I get a better way to do it?