是ASP.net MVC4的新手 我想做的程序可以记录系统登录时间和系统注销时间以及IP地址。
提前致谢....
答案 0 :(得分:1)
录制客户端IP地址使用
HttpContext.Request.UserHostAddress //In Controller
or
HttpContext.Current.Request.UserHostAddress// In Classes
记录登录和退出时间。
创建自定义操作过滤器类并记录下面给出的日期时间。
[CustomFilter(CurrentAction="Login")]
public ActionResult Login()
{
...
}
[CustomFilter(CurrentAction="Logout")]
public ActionResult Logout()
{
...
}
public class CustomFilter : ActionFilterAttribute
{
String CurrentAction {get; set;}
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
// TODO: Add your action filter's tasks here
MyDBEntities storeDB = new MyDBEntities ();
if(CurrentAction.Equals("Login"))
{
LoginDetail log = new LoginDetail ()
{
LoginTime= DateTime.Now,
IP = filterContext.HttpContext.Request.UserHostAddress
};
storeDB.LoginDetails.Add(log);
}
else if(CurrentAction.Equals("Logout"))
{
LoginDetail log = new LoginDetail ()
{
LogoutTime= DateTime.Now,
IP = filterContext.HttpContext.Request.UserHostAddress
};
storeDB.LoginDetails.Add(log);
}
storeDB.SaveChanges();
this.OnActionExecuting(filterContext);
}
}
答案 1 :(得分:0)