2011-06-22 50 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