2015-08-17 194 views
0

我无法将用户从登录页面发送到需要身份验证的页面。用户在第一页上进行身份验证,但在第二页上进行身份验证后,会立即发送到登录页面。下面的代码的要点....Net Forms身份验证身份验证不会出现

页需要再次验证:

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"; 

如果任何人都可以是任何援助,它将不胜感激。

回答

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."; 
     } 
} 

检查此页FormsAuthentication.GetRedirectUrl Method

+0

我已经放弃Membership.ValidateUser(),而是创建了自己的认证码。我是离开基地吗? – librariman

+0

我试着用我自己的代码替换Membership.ValidateUser(),这是一个不行。 – librariman