2

これが私の最初の質問になります!

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_idCookieの有効期限が「ブラウザを閉じたとき」に設定されているようです。

サーバー側では、アプリプールを午前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を搭載したWindows2012サーバーで実行されています。

さらに追加:

ブラウザで閉じたときにsession_idcookieがまだ設定されていることがわかりました。

cloud.hviidnet.com/image/2X3v2y2e1K1S

奇妙なことに、IISサーバーを調べても、600分に設定されています。

cloud.hviidnet.com/image/1e3J1g2u3p2M

4

2 に答える 2

1

解決策は、「セッション」の使用をすべて削除することでした。代わりに、WebSecurity.CurrentUserIDを使用してデータベースからすべてのデータを取得します。

これが他の誰かに役立つことを願っています!

于 2012-10-16T13:07:21.403 に答える
0

Webサーバーは1つだけですか?複数のサーバーの負荷が分散されている場合、ユーザーが投稿間で異なるサーバーにルーティングされるため、セッションが失われる可能性があります。これにより、ランダムな間隔でセッションが発生する理由が説明されます。

于 2012-10-05T16:22:14.480 に答える