0

Web アプリケーションで ASP.NET フォーム認証を使用しています。最近、奇妙な動作を発見しました。実稼働環境ではすべてが正常に機能していました.net 4.0およびIIS 7.ログインユーザーでユーザー名とパスワードを入力してログインすると、突然失われます.HttpContext.Current.User.Identity.Nameこれは毎回発生するわけではありません.私は問題を再現できません私の開発環境では、if(HttpContext.Current.User.Identity.IsAuthenticated)認証チケットのユーザーデータも空ではないことも確認しました.空のみHttpContext.Current.User.Identity.Nameです.plzヘルプ

ログインボタンのコード

protected void LoginButton_Click(object sender, EventArgs e)
    {
        try
        {
            dtUserDetails = new DataTable();
            if (UserRepositoryBL.ValidateUser(txtUserName.Text.Trim(), Password.Text.Trim(), out dtUserDetails))
            {

                AuthUser au = new AuthUser();
                if (dtUserDetails.Rows.Count > 0)
                {
                    DataRow DR = dtUserDetails.Rows[0];
                    au.UserID = Convert.ToInt32(DR["UserID"].ToString());
                    au.UserNo = DR["UserNo"].ToString();
                    au.UserName = DR["UserName"].ToString();
                    au.Password = DR["Password"].ToString();
                }
                string userData = au.ToString();

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(

             2,                             // Version number

             txtUserName.Text.Trim(),      // Username

             DateTime.Now,                  // Issue date

             DateTime.Now.AddMinutes(60), // Expiration date

             false,                         // Persistent?

             userData                 // User data

         );



                string eticket = FormsAuthentication.Encrypt(ticket);

                if (Request.Cookies[txtUserName.Text] != null)
                {
                    //HttpCookie myCookie = new HttpCookie(txtUserName.Text);
                    //myCookie.Expires = DateTime.Now.AddDays(-1d);
                    Request.Cookies[txtUserName.Text].Expires = DateTime.Now.AddDays(-1d);
                    Request.Cookies.Remove(txtUserName.Text);
                }

                HttpCookie cookie = new HttpCookie("SiteCookie", eticket);
               // HttpCookie cookie = new HttpCookie("SiteCookie", eticket);
                cookie.Expires = DateTime.Now.AddMinutes(60);

                FormsAuthentication.SetAuthCookie(txtUserName.Text, false);
               // cookie.Path = FormsAuthentication.FormsCookiePath;
                FormsAuthentication.RenewTicketIfOld(ticket); 

                Response.Cookies.Add(cookie);



                BasePage.ActivityLog("User Login", txtUserName.Text.Trim(), true, Request.RawUrl);
                string url = FormsAuthentication.GetRedirectUrl(txtUserName.Text, false);

                Response.Redirect(url);

                //  FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, false);

            }
            else
            {
                FailureText.Text = "Your login attempt was not successful. Please try again.";
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

web.config

<authentication mode="Forms">
            <forms loginUrl="~/Account/Login.aspx" timeout="60" cookieless="UseCookies" defaultUrl="~/Landing.aspx" protection="All"/>
        </authentication>

        <authorization>
            <deny users="?" />

        </authorization>
4

1 に答える 1

0

セッション時間は 60 分に制限されています。この問題は、セッションの有効期限が切れているユーザーにのみ発生しますか? 開発マシンでこれを再現できないのは、それほど長く待たないからだと説明できるでしょうか?

于 2012-09-09T16:43:32.530 に答える