繁体   English   中英

用MVC4 asp.net随机注销

[英]Random logouts with MVC4 asp.net

这将是我的第一个问题!

我在我的mvc4应用和随机发生的登出时遇到问题。

我使用会话来存储我的公司ID和用户ID。

        private void SetSessionData(string UserName)
    {
        Employee data = (from employee in _db.Employees where employee.Email == UserName select employee).First();
        Session.Add("Comp_ID", data.Comp_ID);
        Session.Add("Company", data.Company.Name);
        Session.Add("User_ID", data.ID);
    }

我已将会话(10小时)的超时值设置为600,这甚至可以设置2个地方以确保:

        [AllowAnonymous]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
        {
            //FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); //sørger for at remember me virker!

            SetSessionData(model.UserName);
            Session.Timeout = 600;

            if (model.RememberMe)
            {
                Response.Cookies.Add(new HttpCookie("CookieUserName", model.UserName) { Expires = DateTime.Now.AddDays(30), Value = model.UserName });
                Response.Cookies.Add(new HttpCookie("CookieRememberMe", model.RememberMe.ToString()) { Expires = DateTime.Now.AddDays(30), Value = model.RememberMe.ToString() });//sætter den nye cookie
            }
            else
            {
                Response.Cookies.Set(new HttpCookie("CookieUserName") { Expires = DateTime.Now.AddDays(-1) });
                Response.Cookies.Set(new HttpCookie("CookieRememberMe") { Expires = DateTime.Now.AddDays(-1) });
            }

            if (string.IsNullOrEmpty(returnUrl))
            {
                return RedirectToLocal(returnUrl);
            }
            return RedirectToAction("Index", "Home");
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "Vi har enten ikke brugernavnet eller koden i kartoteket.");
        return View(model);
    }

并在web.config中:

<system.web>
<machineKey validationKey="MyKeyGoesHere" validation="SHA1" decryption="AES" />
<sessionState timeout="600" />
<compilation debug="true" targetFramework="4.5">
  <assemblies>
    <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="600" />
</authentication>

我的cookie似乎已保存了10个小时,并且session_id cookie的过期时间似乎设置为“浏览器关闭时”。

服务器端我已将应用程序池设置为凌晨1点进行回收。

即使设置了所有这些设置,我的用户仍然可以从登录后2分钟到登录后1小时之间的所有内容进行随机注销。

为了应对一些我已经包括的随机半登录状态问题:

@if(Session == null || Session["User_ID"] == null || !WebSecurity.Initialized){
                //Makes sure the session data is cleared and user logged out if session dies.
                try
                {
                    if(Session != null) {Session.Clear();}

                    if (WebSecurity.Initialized){WebSecurity.Logout();}

                    FormsAuthentication.SignOut();

                    //dette er til at stoppe cache.
                    Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.Cache.SetNoStore();

                }catch{ <p>Error Clearing Login Cache</p>}    
            }

我现在已经迷失了脚步,希望那里的老师知道初学者在这里犯的错误!

感谢您提前做出所有回应!

编辑:

我也尝试过这样做: http : //www.windowsitpro.com/article/security-development/create-persistent-id-cookies

(原始链接来自: ASP.NET MVC FormsAuthentication Cookie超时不能增加

但这只是使我的应用程序每次注销后都按一下退出。

该应用程序在带有IIS8的Windows 2012 Server上运行。

更多添加:

我发现在浏览器中关闭后,session_id cookie仍设置为:

cloud.hviidnet.com/image/2X3v2y2e1K1S

奇怪的是,即使我在IIS服务器中查看,它也设置为600分钟:

cloud.hviidnet.com/image/1e3J1g2u3p2M

解决的办法是删除所有对“会话”的使用。 并使用WebSecurity.CurrentUserID从数据库获取所有数据。

希望这对别人有帮助!

您只有一台Web服务器吗? 如果您有多个负载均衡的服务器,则会话可能会丢失,因为用户被路由到帖子之间的其他服务器,这可以解释为什么它随机发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM