答案 0 :(得分:0)
FormsAuthentication.RedirectFromLoginPage
的{{3}}状态:
将经过身份验证的用户重定向回原始请求的URL或默认URL。
注意单词“ authenticated” ...在第一个代码段中,您正在检查凭据并进行重定向。您正在 not 设置表单身份验证Cookie,就像在第二个片段中一样。我实际上认为组合的代码应如下所示:
protected void btnLogin_Click(object sender, EventArgs e)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
bool rememberUsername = false;
SqlDbAccess db = new SqlDbAccess();
int result = db.ValidateLogin(username, password);
switch (result)
{
case -1:
lblValidationError.Text = "Utilizador / Password incorrecto";
break;
case -2:
lblValidationError.Text = "Conta desactivada";
break;
default:
// not certain what should be passed in here???
string roles = db.GetRoles(username);
var ticket = new FormsAuthenticationTicket
(
1,
username,
DateTime.Now,
DateTime.Now.AddMinutes(20),
rememberUsername,
roles,
FormsAuthentication.FormsCookiePath
);
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
Response.Cookies.Add(cookie);
FormsAuthentication.RedirectFromLoginPage(username, rememberUsername);
break;
}
}