ASP.NET Forms Authentication在localhost服务器中进行身份验证,但在Web服务器上进行身份验证

gee*_*vee 0 c# asp.net forms-authentication

我一直Forms Authentication在用C#(v3.5)实现ASP.NET.

当用户的电子邮件和密码存储在我的SQL数据库中时,我创建了一个简单的登录表单.

当我登录本地主机时,一切正常,但是当我发布项目并将其上传到我的生产Web服务器时,事情对我来说有点奇怪.

HttpContentxt.Current.User.Identity.IsAuthenticated变量返回false,即使登录为全成(并再次,在本地主机一切正常).

这是以下登录按钮点击代码(我使用自己的DataAccess,忽略它的无关代码):

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        Page.Validate("Login");
        if (Page.IsValid)
        {
            string email = txtEmail.Text;
            string passwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "MD5");
            WebFactory.DataAccess.Users.Data userData = new WebFactory.DataAccess.Users.Data(ConnectionString);
            userData.Load(new WebFactory.DataAccess.Users.Item[] {
                new WebFactory.DataAccess.Users.Item(WebFactory.DataAccess.Users.Columns.Email, email),
                new WebFactory.DataAccess.Users.Item(WebFactory.DataAccess.Users.Columns.Password, passwd)
            });
            if (userData.HasData) // Login Success
            {
                if (!cbRememberMe.Checked)
                {
                    FormsAuthentication.SetAuthCookie(userData.Id.ToString(), false);
                }
                else
                {
                    FormsAuthentication.Initialize();
                    DateTime expires = DateTime.Now.AddDays(20);
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                        userData.Id.ToString(),
                        DateTime.Now,
                        expires,
                        true,
                        String.Empty,
                        FormsAuthentication.FormsCookiePath);

                    string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    authCookie.Expires = expires;
                    Response.Cookies.Add(authCookie);
                }
                lblStatus.Text = "";
                if (Common.QS.HasRefUrl)
                {
                    Response.Redirect(Common.QS.RefUrl);
                }
                else
                {
                    Common.UserTools.RedirectLoggedInUser(userData.Id);
                }
            }
            else // Login failed
            {
                lblStatus.Text = "Email or password is wrong. please try again."
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

感谢所有帮助者,并为英语错误感到抱歉.

gee*_*vee 5

谢谢大家,我解决了这个问题.

我只需要name<forms>子句中输入一个属性,现在一切都很完美.

再次感谢!