簡體   English   中英

嘗試使用CSHTML做一個非常基本的登錄頁面,但是我的if語句無法正常工作

[英]Trying to do a very basic login page using CSHTML but I can't get my if statement to work correctly

@{

    Page.Title = "Log in";

    // Generate page variables
    var db = Database.Open("calcinema");
    var email = "";
    var password = "";
    var getAcc = db.Query("SELECT * FROM Account;");

    if (IsPost && Validation.IsValid()) {
        email = @Request.Form["email"];
        password = @Request.Form["password"];}

        foreach (var a in getAcc) {

            if (email == a.Email && password == a.Password){
                 Response.Redirect("~/Browse.cshtml");}
     }            
}

    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
<table border="0" align="center">
    <tr>
    <td>
        <form name="login" action="" method="get" accept-charset="utf-8">  
        <label for="email">Email</label>  
        <input type="email" name="email" placeholder="yourname@email.com" required value = "@Request.Form["email"]"> 
        <label for="password">Password</label>  
        <input type="password" name="password" placeholder="Password" required value = "@Request.Form["password"]">  
        <input type="submit" value="Login"> 
        </form>  
    </td>
    </tr>
</table>
</body>

頁面加載,但是無論電子郵件和密碼是否有效,都不會發生任何事情。 它只接受輸入,然后將我返回到登錄頁面,文本字段為空,而不是將我重定向到Browse.cshtml頁面。

問題是您表單的方法。 您選擇了GET ,該GET在QueryString中傳遞表單值。 您正在查看Request.Form集合,但其中的值不存在。 將方法更改為POST

您還需要確保正確查詢數據庫,並返回要由代碼檢查的結果。 還有其他問題,您不能設置input type=password的值,並且required屬性不適用於服務器端驗證。

這是一些可以使用的代碼,但是我建議您花一些時間在這里學習教程,並學習Web頁面的基礎。 這是一個很棒的框架,但確實需要一些應用程序才能正確學習。

@{
    Page.Title = "Log in";
    if (IsPost && Validation.IsValid()) {
        var email = Request.Form["email"];
        var password = Request.Form["password"];
        var db = Database.Open("calcinema");
        var result = db.QueryValue("SELECT Count(*) FROM Account WHERE Email = @0 AND Password = @1", email, password);
        if (result > 0){
            Response.Redirect("~/Browse.cshtml");
         }            
    }
}
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
    <table border="0" align="center">
        <tr>
        <td>
            <form name="login" action="" method="post" accept-charset="utf-8">  
            <label for="email">Email</label>  
            <input type="email" name="email" placeholder="yourname@email.com" required value = "@Request.Form["email"]"> 
            <label for="password">Password</label>  
            <input type="password" name="password" placeholder="Password" required>  
            <input type="submit" value="Login"> 
            </form>  
        </td>
        </tr>
    </table>
    </body>

您的問題可能是這樣的:

 email = @Request.Form["email"];
 password = @Request.Form["password"];

由於您已經在服務器端代碼中,因此無需在“ Request前面加上“&”前綴:

 email = Request.Form["email"];
 password = Request.Form["password"];

您真的不應該這樣做。 您應該將這些值作為參數傳遞給SQL命令,然后查看是否存在匹配項:

 var result = db.QueryValue("SELECT COUNT(*) FROM Account WHERE email = @0 AND password = @1", email, password);

當然,適用於SSL,純文本密碼和SQL注入防護措施的各種安全注意事項。

因為您提供了if子句語句。 使用此HTML表單的方法,該語句永遠不會正確。 您需要更改它。

看一看

if (IsPost && Validation.IsValid()) {
    email = @Request.Form["email"];
    password = @Request.Form["password"];
}

foreach (var a in getAcc) {

   if (email == a.Email && password == a.Password) {
      Response.Redirect("~/Browse.cshtml");
   }
}

僅當請求為POST ,才需要執行@Request.Form["email"]代碼,但是您的表單設置為method="get" ,這將始終跳過IF塊。

而且由於跳過了if塊,因此您的代碼將始終具有空字符串值或電子郵件和密碼均具有的初始值。 因此,第二個if塊也不會執行,也不會將您重定向到Browse.cshtml頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM