我一直在使用C#(v3.5)在ASP.NET中实现Forms Authentication
。ASP.NET窗体身份验证在本地主机服务器中进行身份验证,但不在Web服务器上进行身份验证
我创建了一个简单的登录表单,当用户的电子邮件&密码存储在我的SQL数据库。
当我在本地主机登录时,一切正常,但是当我发布项目并将其上传到我的生产Web服务器时,事情对我来说有点奇怪。
HttpContentxt.Current.User.Identity.IsAuthenticated
变量返回false,即使登录成功(再次,在localhost中一切正常)。
这是下面的登录按钮点击代码(我用我自己的数据访问,管它是无关紧要的代码):
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."
}
}
}
感谢所有帮助者,并为英文错误抱歉。
嗨,很抱歉让一个死的线程活着,但你是什么意思在子句中添加一个名称属性?即时通讯使用MVC 3.0,我将能够登录,但一分钟后点击它会重新将我重定向到登录页面。但在我的本地主机,它完美的作品..任何帮助表示赞赏。谢谢! –
gdubs
2012-07-09 03:07:47