我很难将用户从登录页面发送到需要身份验证的页面。用户在第一页上进行身份验证,但在第二页上进行身份验证检查后,将立即返回登录状态。这是代码的要点......
页面需要再次验证:
protected void Page_Load(object sender, EventArgs e)
{
/*Make sure the user is authenticated. If not, redirect them to the Login page*/
if (!HttpContext.Current.Request.IsAuthenticated)
else
LabelMsg.Text = "User: " + System.Web.HttpContext.Current.Request.LogonUserIdentity.Name.ToString();
} //end Page_Load()
这是登录代码的要点:
if (GridViewBookList.Rows.Count > 0)
{
string[] roles = new string[] { "admin", "newbooks" };
HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(TextBoxUser.Text), roles);
FormsAuthentication.SetAuthCookie("admin", true);
FormsAuthentication.RedirectFromLoginPage(TextBoxUser.Text, true);
}
else
{
LabelMsg.Text = "Incorrect username or password";
如果有人可以提供任何帮助,我们将不胜感激。
答案 0 :(得分:1)
您的登录信息应如下所示:
private void Login_Click(Object sender, EventArgs e)
{
// Create a custom FormsAuthenticationTicket containing
// application specific data for the user.
string username = UserNameTextBox.Text;
string password = UserPassTextBox.Text;
bool isPersistent = false;
if (Membership.ValidateUser(username, password))
{
string userData = "ApplicationSpecific data for this user.";
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
username,
DateTime.Now,
DateTime.Now.AddMinutes(30),
isPersistent,
userData,
FormsAuthentication.FormsCookiePath);
// Encrypt the ticket.
string encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
// Redirect back to original URL.
Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent));
}
else
{
Msg.Text = "Login failed. Please check your user name and password and try again.";
}
}