我正在mvc.net上的自定义登录页面上工作。我检查这样的登录:
public bool Login(string login, string password, bool persistent)
{
var loginEntity = this.AdminRepository.GetLogin(login, password);
if (loginEntity != null)
{
FormsAuthentication.SetAuthCookie(login, persistent);
HttpContext.Current.Session["AdminId"] = loginEntity.AdminId;
HttpContext.Current.Session["AdminUsername"] = loginEntity.Username;
return true;
}
然后我用过滤器属性装饰任何需要管理员访问权限的控制器:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var ctx = HttpContext.Current;
// check if session is supported
if (ctx.Session != null)
{
var redirectTargetDictionary = new RouteValueDictionary();
// check if a new session id was generated
if (ctx.Session.IsNewSession)
{
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers["Cookie"];
if (((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) || null == sessionCookie)
{
redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("area", "Admin");
redirectTargetDictionary.Add("action", "LogOn");
redirectTargetDictionary.Add("controller", "Home");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
} else if (SessionContext.AdminId == null) {
redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("area", "Admin");
redirectTargetDictionary.Add("action", "LogOn");
redirectTargetDictionary.Add("controller", "Home");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
}
base.OnActionExecuting(filterContext);
}
我看到登录后我有两个cookie:
问题: 问题是,即使我将TRUE设置为“persists”选项(这将设置ASPXAUTH到期时间30分钟 - 这是好的)我的会话[“AdminId”]在我关闭并重新打开浏览器后始终为空。当我最初将“persists”设置为true并关闭然后重新打开browswer窗口时,如何确保我的会话(会话[“AdminId”]和会话[“AdminUsername”])从cookie中被拉入。 感谢
答案 0 :(得分:0)
具有到期时间的cookie将写入磁盘。因此,如果cookie未到期,用户将在下次打开浏览器时登录。
会话cookie仅存储在内存中,并在浏览器关闭后立即丢失。
会话cookie是没有过期日期的cookie。
答案 1 :(得分:0)
我在这里找到了我的解决方案:Is it possible to use .ASPXAUTH for my own logging system?
这就是我所做的:
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
/// <summary>
/// Controller action filter is used to check whether the session is still active. If the session has expired filter redirects to the login screen.
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var ctx = HttpContext.Current;
// check if session is supported
if (ctx.Session != null)
{
// check if a new session id was generated
if (ctx.Session.IsNewSession)
{
var identity = ctx.User.Identity;
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers["Cookie"];
if (((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) || null == sessionCookie)
{
var redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("area", string.Empty);
redirectTargetDictionary.Add("action", "LogOn");
redirectTargetDictionary.Add("controller", "User");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
// Authenticated user, load session info
else if (identity.IsAuthenticated)
{
var loginRepository = new LoginRepository(InversionOfControl.Container.Resolve<IDbContext>());
IAuthenticationService authenticationService = new AuthenticationService(loginRepository);
authenticationService.SetLoginSession(identity.Name);
}
}
else if (SessionContext.LoginId == null)
{
var redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("area", string.Empty);
redirectTargetDictionary.Add("action", "LogOn");
redirectTargetDictionary.Add("controller", "User");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
}
base.OnActionExecuting(filterContext);
}
}