相关疑难解决方法(0)

ASP.NET cookie到期时间始终为1/1/0001 12:00 AM

我正在使用以下代码设置cookie过期:


// remove existing cookies.
request.Cookies.Clear();
response.Cookies.Clear();

// ... serialize and encrypt my data ...

// now set the cookie.
HttpCookie cookie = new HttpCookie(AuthCookieName, encrypted);
cookie.Expires = DateTime.Now.Add(TimeSpan.FromHours(CookieTimeOutHours));
cookie.HttpOnly = true;
response.Cookies.Add(cookie);

// redirect to different page
Run Code Online (Sandbox Code Playgroud)

当我在另一页读取cookie超时时,我得到1/1/0001 12:00 AM.如果有人可以帮我解决问题,我会很感激.我正在使用ASP.NET 3.5

好.看完Gulzar的链接后,似乎我无法检查cookie.Expires上的HttpRequest根本没有?因为链接似乎暗示cookie.Expires总是设置为DateTime.MinValue,因为服务器永远不知道客户端计算机上的实际时间?所以这意味着我必须自己将时间存储在cookie中并检查它?我的理解是否正确?

谢谢尚卡尔

asp.net cookies

28
推荐指数
3
解决办法
3万
查看次数

如何在其他用户更改其用户名时强制注销用户?

在我的应用程序中,我使用Forms-Authentication登录并注销用户.

一个功能是管理员可以更改其他用户的用户名.在这种情况下,我需要注销用户名已更改的用户.

如果我不这样做,由于之前设置了cookie,他们可以访问应用程序并收到错误消息(因为他们的用户名不存在,并且有些部分我使用他们的用户名来实现某些功能).

如何强制这些用户使用Forms-Authentication注销?

更新:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        string controller = filterContext.RouteData.Values["controller"].ToString();
        string action     = filterContext.RouteData.Values["action"].ToString(); ;
        // Below returns the previous username, which does not exist anymore in db.
        string userName = HttpContext.Current.User.Identity.Name;

        UnitOfWork unitOfWork = new UnitOfWork();

        if (!unitOfWork.UserRepository.UserExists(userName))
        {
            FormsAuthentication.SignOut();
            filterContext.HttpContext.Session.Clear();
            filterContext.HttpContext.Session.Abandon();
            // I am not using Roles.


        }
        unitOfWork.Dispose();
        base.OnActionExecuting(filterContext);

    }
Run Code Online (Sandbox Code Playgroud)

在我的客户全局过滤器中,我检查用户是否存在,如果不存在,我将其签出.但是,它不起作用.通过工作我的意思是他们通过身份验证并获得应用程序访问权限

提前致谢.

c# asp.net-mvc forms-authentication

13
推荐指数
1
解决办法
1万
查看次数

标签 统计

asp.net ×1

asp.net-mvc ×1

c# ×1

cookies ×1

forms-authentication ×1