How to get numbers of online users using .NET Identity 2.0 in MVC 5

Do guys know that how to get numbers of online users using .NET identity 2.0 membership providers in MVC 5 ?

I have scan the methods in UserManager that Identity sample gives but no helping.


This should be somewhat accurate at displaying the user count. It uses a cache to store the user's IP address and returns a count of individual IPs. If two people are behind the same proxy it will count it as one person.

using System.Runtime.Caching;

public int UsersOnlineCount
{
    get
    {
        return MemoryCache.Default.Where(kv => kv.Value.ToString() == "User").Count();
    }
}

The best way of making sure everyone gets added to the cache is to define some BaseController with this in the constructor...

public BaseController() : base() 
{
    CacheItemPolicy policy = new CacheItemPolicy();
    policy.AbsoluteExpiration = DateTime.UtcNow.AddMinutes(20);

    MemoryCache.Default.Add(System.Web.HttpContext.Current.Request.UserHostAddress, "User", policy);
}
链接地址: http://www.djcxy.com/p/78688.html

上一篇: 等待Task.Run(()=> semaphore.WaitOne())有什么问题吗?

下一篇: 如何在MVC 5中使用.NET Identity 2.0获取在线用户的数量