2

mvc4でloginとlogOutの機能を作りたいです。ログイン機能で、ログイン Cookie が存在し、空でない場合、ユーザーはサインイン モードになり、それ以外の場合はログイン ページにリダイレクトされます。logOut 関数では、すべての Cookie とセッションがクリアされ、ログイン関数にリダイレクトされますが、ログイン関数にはログイン Cookie が存在します!

ログイン:

public ActionResult Login()
        {
            if (Request.Cookies["login"] != null)
            {
                string login = Request.Cookies["login"].Value.ToString();                

                if (login != string.Empty)
                {
                    //Get from service
                    Service srv = new Service();
                    UserItem userItem = srv.getUserItem(login);                    
                    srv.Close();

                    Session.Timeout = 30;
                    Session["login "] = login;
                    Session["userId"] = userItem.No;
                    Session["firstName"] = userItem.FirstName;
                    Session["lastName"] = userItem.LastName;
                    string loginName = userItem.LoginName;                    

                    FormsAuthentication.SetAuthCookie(loginName, false);

                    return Redirect(“Index”);
                }
                else 
                {
                    Return redirect("http://mySite/SignIn.aspx");
                }
            }
            else
            {
                Return redirect("http://mySite/SignIn.aspx");                    
            }
        }

ログアウト:

public ActionResult LogOut()
        {
            string login = Session["login"].ToString();

            Request.Cookies["login"].Value = "";
            Response.Cookies["login"].Value = "";

            FormsAuthentication.SignOut();
            HttpCookie c = Request.Cookies[FormsAuthentication.FormsCookieName];
            c.Expires = DateTime.Now.AddDays(-1);

            Session.Clear();
            Request.Cookies.Clear();
            Response.Cookies.Clear();

            //FormsAuthentication.Initialize();
            //string strRole = String.Empty;
            //FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, "", DateTime.Now, DateTime.Now.AddMinutes(-30), false, strRole, FormsAuthentication.FormsCookiePath);
            //Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat)));

            //Session.Abandon();

            //// clear authentication cookie
            //HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
            //cookie1.Expires = DateTime.Now.AddYears(-1);
            //Response.Cookies.Add(cookie1);

            //// clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
            //HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
            //cookie2.Expires = DateTime.Now.AddYears(-1);
            //Response.Cookies.Add(cookie2);

            //FormsAuthentication.RedirectToLoginPage();               

            return RedirectToAction("Login", "Usr");
        }

Web.config:

<authentication mode="Forms">
      <forms loginUrl="~/Usr/Login" timeout="30" />
    </authentication>

私はコメントコードを試しています。この行にコメントを付けることもできます:

FormsAuthentication.SignOut();

Cookie の値を "" に設定しても、ログイン ページではこの Cookie の値が古いままです。そして、有効期限を1日後に設定するなど、Cookieをクリアするいくつかの方法を試してみてください。しかし…</p>

ありがとう

4

3 に答える 3

1

この投稿のように、ユーザーが認証されているかどうかを判断するはるかに簡単な方法があります アクション内でユーザーが承認されているかどうかを確認する方法

FormsAuthentication.SetAuthCookie() を呼び出した後、 を呼び出すことができますUser.Identity.IsAuthenticated。独自の Cookie を設定する必要はありません。

このようにすると、 FormsAuthentication.SignOut() は正しい Cookie を破棄します。

于 2013-07-11T11:49:05.660 に答える