如何在MVC 5使用.NET 2.0的身份获得在线用户数 [英] How to get numbers of online users using .NET Identity 2.0 in MVC 5
本文介绍了如何在MVC 5使用.NET 2.0的身份获得在线用户数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
做球员知道如何使用.NET 2.0的身份隶属提供商MVC 5上网用户数量?
Do guys know that how to get numbers of online users using .NET identity 2.0 membership providers in MVC 5 ?
我已经扫描的UserManager的方法是身份的样品给,但没有帮扶。
I have scan the methods in UserManager that Identity sample gives but no helping.
推荐答案
这应该是显示用户数量有所准确。它使用一个高速缓存来存储用户的IP地址,并返回个别的IP的计数。如果两个人都落后于相同的代理会指望它作为一个人。
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();
}
}
确保每个人都被添加到缓存的最佳方式是在构造这个定义一些 BaseController
...
public BaseController() : base()
{
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.UtcNow.AddMinutes(20);
MemoryCache.Default.Add(System.Web.HttpContext.Current.Request.UserHostAddress, "User", policy);
}
这篇关于如何在MVC 5使用.NET 2.0的身份获得在线用户数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文