Asp.net MVC 用户在线简单实现及单点登陆扩展(一)
2010-02-05 17:06
691 查看
首先,让我们想来思考如何实现在线用户的统计,这里我将使用IHttpModule扩展我们的在线用户模块。
创建我们需要的实体类
代码
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;
}
}
对了,忘记一个很重要的事,那就是注册我们的IHttpModule,打开Web.config页面,找到<httpModules>节,添加
<add name="OnlineList" type="命名空间.UserOnlineModule"/>
就这么简单的实现了在线用户的统计,现在来看看使用
[OnlineFilter]
public ActionResult Index(string id)
{
return View();
}
你可以这样简单的在Action上进行Filter的标注,如果你有一个Controller都需要进行在线统计,那么你可以直接在整个Controller上标注一个就可以了。很简单吧。
下一篇将介绍一下利用这个简单的在线用户实现进行单点登陆限制,其实很多朋友可能已经知道该怎么做了。
创建我们需要的实体类
代码
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;
}
}
对了,忘记一个很重要的事,那就是注册我们的IHttpModule,打开Web.config页面,找到<httpModules>节,添加
<add name="OnlineList" type="命名空间.UserOnlineModule"/>
就这么简单的实现了在线用户的统计,现在来看看使用
[OnlineFilter]
public ActionResult Index(string id)
{
return View();
}
你可以这样简单的在Action上进行Filter的标注,如果你有一个Controller都需要进行在线统计,那么你可以直接在整个Controller上标注一个就可以了。很简单吧。
下一篇将介绍一下利用这个简单的在线用户实现进行单点登陆限制,其实很多朋友可能已经知道该怎么做了。
相关文章推荐
- ASP.NET中在线用户统计的简单实现及讨论
- asp.net mvc利用knockoutjs实现登陆并记录用户的内外网IP及所在城市
- ASP.NET中在线用户统计的简单实现及讨论
- ASP.NET中在线用户统计的简单实现及讨论
- Asp.net MVC利用knockoutjs实现登陆并记录用户的内外网IP及所在城市(推荐)
- Asp.net中实现同一用户名不能同时登陆(转)
- Asp.net中实现同一用户名不能同时登陆(转)
- Asp.net mvc验证用户登录之Forms实现-转
- Asp.Net MVC:基于cookie的用户自选视图引擎实现
- asp.net mvc简单实现基于Razor的分页控件
- 使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之用户管理(3)
- ASP.NET实现用户在线检测的类源码(转贴)
- 实现Ajax增强用户体验ASP.NET MVC 3
- ASP.NET(c#)实现防止同一用户同时登陆
- ASP.NET MVC DropDownList扩展,实现foreach@Html.DropDownListFor(p=>p.Type,p.Type,TypeItem)
- Asp.net MVC 自定义ViewEngine的简单实现
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之用户管理(1)
- asp.net在线预览txt文件(简单实现)
- ASP.NET MVC图片上传前预览简单实现
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之用户管理(2)