我一直在使用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