2014-10-11 14 views
0

我開始學習Asp.net使用C# Visual Studio。我正在創建一個simple Login Form,我有一點問題。我已經Google搜索了,但我找不到答案,所以我想在這裏嘗試,也許我可以找到答案。Asp.net在表單動作執行前檢查數據庫

問題是當我提交表單按鈕時,它應該驗證輸入是否在數據庫中找到。如果找到輸入,它應該執行表單動作,否則什麼也不做。


LoginForm.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoginForm.aspx.cs" Inherits="MajelFinals.LoginForm" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server" action="Success.aspx" lang="en" method="post" 
    name="frmLogin" submitdisabledcontrols="False"> 
    <div style="height: 252px"> 

     Login Form<br /> 
     <br /> 
     User ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="txtUserID" runat="server"></asp:TextBox> 
&nbsp;&nbsp;&nbsp; 
     <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
      ControlToValidate="txtUserID" ErrorMessage="* Required Input" 
      Font-Size="X-Small" ForeColor="Red"></asp:RequiredFieldValidator> 
     <br /> 
     <br /> 
     Password&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox> 
&nbsp;&nbsp;&nbsp; 
     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
      ControlToValidate="txtPassword" ErrorMessage="* Required Password" 
      Font-Size="X-Small" ForeColor="Red"></asp:RequiredFieldValidator> 
     <br /> 
     <br /> 
     <asp:Button ID="btnSignIn" runat="server" onclick="signIn_Click" 
      Text="Sign In" /> 

    </div> 
    </form> 
</body> 
</html> 

LoginForm.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace ProjectProject 
{ 
    public partial class LoginForm : System.Web.UI.Page 
    { 
     private Login login; 

     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void signIn_Click(object sender, EventArgs e) 
     { 
      UserLogin user = new UserLogin(); 
      user.userID = txtUserID.Text; 
      user.userPass = txtPassword.Text; 

      if (login.validateEntry(user)) //CHECKING IF FOUND IN THE DATABASE 
      { 
        // CONTINUE 
      } 
      //STAY 
     } 
    } 
} 

回答

1

你通常與登入功能打交道時擁有多種選擇。
通常,當嘗試log-in時,成功登錄後,用戶將重定向到爲另一種形式。 你可以做既可以使用(read more here)

Response.Redirect("you page to process user actions, etc"); 

或尚未更好(Read more here),

Server.Transfer("YourPage.aspx", true); 

然後做你願意的話,通常在這一步,你需要有一個會議準備,所以在那些你正在重定向的頁面上,你可以在登錄的用戶和某個意外(或故意)試圖在沒有登錄的情況下看到內容的用戶之間做出區別。在這種情況下,當某個失敗的人停留在日誌中時
只要他輸入了錯誤的登錄信息就可以進入頁面憑據。

您也可以使用會話並避免重定向,並通過該會話知道何時執行合法操作。
當用戶輸入一個有效的用戶名或密碼,爲其創建一個會話並刷新該頁面時,含義爲page_load,您檢查當前用戶的會話是否存在,並執行任何您需要的操作做。
會話實例:

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (HttpContext.Current.Session["ID"]!= null) 
     { 
      lblmsg.Text = "You are loged in"; 
     } 
    } 

    Dictionary<string, string> db = new Dictionary<string, string>(){ 
     {"Ali","XXX"}, 
     {"Alex","YYY"}, 
     {"Mina","zzz"}, 
     {"Admin","123"} 
    }; 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     //simulating your database search 
     if (db.Contains(new KeyValuePair<string,string>(txtBoxUser.Text,txtBoxPassword.Text))) 
     { 
      HttpContext.Current.Session["ID"] = txtBoxUser.Text;    
     }else 
     { 
      lblmsg.Text = "Not Logged in "; 
     } 
    } 

你也可以嘗試信號在同一頁,約一個有效的登錄,使用postget方法(post是更安全的,但發送一些信息到同一頁我不要爲此推薦它)。
當用戶輸入有效的用戶名和密碼時,您會將其身份(例如其他隱藏信息(只有您知道如何創建和閱讀,以避免被其他用戶模擬)回到此頁面。
再次在page_load上注意這一點,如果找到有效值,則表示您已成功登錄,然後您可以繼續執行您需要執行的操作。
爲了這個工作,你還需要有一個會話或提供一些機制來模擬你的頁面需要認證的一個或每個方法或部分,需要使用這種方法,這在我的op pinion中很麻煩並且不實用。

對於POST方法,請參見this
我建議使用會話並重定向到另一個頁面。

相關問題