using System;
using System.Web;
using MvcMamcache.Models;
namespace MvcMamcache.Common {
public class CheckLoginModule : IHttpModule {
#region IHttpModule 成员
public void Init(HttpApplication context) {
context.AcquireRequestState += new EventHandler(OnRequest);
}
public void OnRequest(object source, EventArgs e) {
HttpApplication application = source as HttpApplication;//得到Application
HttpContext context = application.Context;//得到请求上下文.
Uri url = context.Request.Url;//得到当前请求的URL
if(url.AbsolutePath.ToLower() == "/home/index") {
var httpCookie = context.Request.Cookies["SessionId"];
if(httpCookie == null || Common.MemCacheHelper.GetValue(httpCookie.Value) == null) {//Session会话过期
//是否保留7天
if(context.Request.Cookies["ui1"] == null || context.Request.Cookies["ui2"] == null) {
context.Response.Redirect("/home/login");
} else {
//根据Cookie中保存的用户名与密码至数据库获取UserInfo对象 这里直接new
UserInfo userInfo = new UserInfo() {
LoginId = context.Request.Cookies["ui1"].Value,
LoginPwd = context.Request.Cookies["ui2"].Value
};
var sessionId = Guid.NewGuid().ToString();
context.Response.Cookies["SessionId"].Value = sessionId;
Common.MemCacheHelper.Insert(sessionId, userInfo);
}
}
}
}
public void Dispose() {
throw new NotImplementedException();
}
#endregion
}
}