2016-04-20 15 views
0

我正在開發ASP.NET webforms。如果用戶直接輸入任何其他頁面的URL,我想將用戶重定向到登錄頁面。我在web.config文件中添加了下面的代碼,它將用戶重定向到登錄頁面,以防用戶嘗試直接訪問任何其他頁面,但存在用戶登錄時出現問題時,應將用戶重定向到welcome.aspx頁面,但它會重新導向到登錄頁面。問題在哪裏?如果用戶直接訪問ASP.NET WebForms中的任何頁面,如何將用戶重定向到登錄頁面

代碼在我用web.config文件....

<authentication mode="Forms"> 
    <forms name=".COOKIE" loginUrl="login.aspx" protection="All" path="/" timeout="480"/> 
</authentication> 
<authorization> 
    <deny users="?"/> 
    <allow users="*"/> 
</authorization> 

和 login.aspx的頁面代碼如下:

foreach (var user in db.usertables) 

     if (user.username == TextBox1.Text && user.password == TextBox2.Text) 
     { 

      { 
       Response.Redirect("welcome.aspx"); 
      } 
     } 
     else 

     { 

      Label1.Visible = true; 

     } 

回答

0

從web.config中

刪除下面的代碼
<authorization> 
<deny users="?"/> 
<allow users="*"/> 
</authorization> 

在您的aspx.cs頁面中,添加以下代碼

if(authenticated) 
{  
    FormsAuthentication.SetAuthCookie(username,false); 
    Response.Redirect("welcome.aspx"); 
} 
+0

which aspx.cs page?是它login.aspx或welcome.aspx? –

+0

login.aspx的頁面代碼如下:的foreach(在db.usertables VAR用戶) 如果(user.username == TextBox1.Text && user.password的== TextBox2.Text) { { 的Response.Redirect( 「welcome.aspx」); } } else { Label1.Visible = true; } –

+0

爲什麼要刪除'''? –

相關問題