2014-10-11 46 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
我建议使用会话并重定向到另一个页面。

相关问题