2011-06-22 87 views
3

我似乎不能成立的認證系統在asp.net 我有一個登錄系統代碼:asp.net登錄問題

protected void btnlogin_Click(object sender, EventArgs e) 
{ 
    PageUser myUser = new PageUser(); 
    if (myUser.AuthenticateUser(txtUsername.Text, txtPassword.Text)) 
    { 
     // entry found 

     HttpCookie myCookie; 

     DateTime now = DateTime.Now; 

     myCookie = new HttpCookie("UserName"); 
     myCookie.Value = myUser.UserName; 
     myCookie.Expires = now.AddMinutes(30); 
     Response.Cookies.Add(myCookie); 

     myCookie = new HttpCookie("LoginID"); 
     myCookie.Value = myUser.UserLoginID.ToString(); 
     myCookie.Expires = now.AddMinutes(30); 
     Response.Cookies.Add(myCookie); 

     lblResult.Visible = false; 

     FormsAuthentication.SetAuthCookie(myUser.UserName + " " + myUser.UserLoginID.ToString(), true); 

     Response.Redirect("AdminView.aspx"); 
    } 
    else 
    { 
     // entry not found 
     lblResult.Text = "<b>Invalid logon attempt<b>"; 
     lblResult.ForeColor = System.Drawing.Color.FromName("Red"); 
     lblResult.Visible = true; 
    } 
} 

的身份驗證方法工作正常,但是當我還沒有登錄它即使該用戶沒有登錄,仍然可以讓我重新導向AdminView。 代碼我有困難有:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 

    } 
    string userName = ""; 
    string[] splits; 
    try 
    { 
     if (this.Page.User.Identity.IsAuthenticated) 
     { 
      splits = this.Page.User.Identity.Name.Split(new char[1] { ' ' }); 
      userName = splits[0] + " " + splits[1]; 

     } 
     else 
     { 
      Response.Redirect("default.aspx"); 
     } 
     txtLoggedInUser.Text += " - " + userName; 
    } 
    catch 
    { 
     Response.Redirect("default.aspx"); 
    } 
} 

我不知道該怎麼寫代碼,以便當他們嘗試訪問該管理頁面,將重定向一個人回到登錄頁面。

回答

0

要將未經身份驗證的用戶限制爲AdminView.aspx頁面,您必須將以下內容添加到web.config文件的configuration部分。

<location path="AdminView.aspx"> 
<system.web> 
    <authorization> 
     <deny users="?"/>    
    </authorization> 
</system.web> 

<deny users="?"/>的意思是在unauthenticated用戶將無法訪問該文件/文件夾AdminView.aspx

+0

謝謝:)這有助於 – Angie