【问题标题】:Getting HTML form input with C#使用 C# 获取 HTML 表单输入
【发布时间】:2018-01-23 19:27:02
【问题描述】:

我在使用 C# 获取登录页面的 HTML 输入值时遇到问题。我很幸运只使用了一个普通的 ASP 元素,但没有那么多纯 HTML。

我试图让 C# 从 html 表单中获取用户名输入和密码,然后作为字符串发送到 MySQL db proc。如果用户名/密码组合不存在,我会尝试重定向回 Login.aspx 页面,如果它确实存在“google.com”。用户名和密码也保存在会话中。

我的 HTML 如下所示:

<form action="Login.aspx.cs" runat="server" method="post">
            <input type="text" name="un" pattern="a[0-9]{6}" required="required" placeholder="a000000"/><br />
            <input type="password" name="pw"  required="required" /><br />
            <asp:Button ID="Button1" Text="text" runat="server" OnClick="Login1_Authenticate" />
</form>

我的 C# 如下所示:

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


public partial class Login : System.Web.UI.Page
{
    int status;
    int role;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {

        //// create an open connection
        SqlConnection conn =
            new SqlConnection("Data Source=MachineName;"
            + "Initial Catalog=DBName;"
            + "User ID=Usr ;Password=Password");

        conn.Open();

        string userName = Page.Request.Form["un"].ToString();
        string pw = Page.Request.Form["pw"].ToString();
        //userName = Convert.ToString(Console.ReadLine());


        //// create a SqlCommand object for this connection
        SqlCommand command = conn.CreateCommand();
        command.CommandText = "EXEC dbo.SP_CA_CHECK_USER @USER_ID = '" + userName + "', @PASSWORD = '" + pw + "'";
        command.CommandType = CommandType.Text;

        //// execute the command that returns a SqlDataReader
        SqlDataReader reader = command.ExecuteReader();

        //// display the results
        while (reader.Read())
        {
            status = reader.GetInt32(0);
        }

        //// close first reader
            reader.Close();


            if (status == 0)
            {
                //login
                Session["userID"] = userName;
                command.CommandText = "EXEC dbo.SP_CA_RETURN_USER_ROLE @USER_ID = '" + userName + "'";
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    role = reader.GetInt32(0);
                }

                Session["roleID"] = role;

                if (Session["userID"] != null)
                {
                    string userID = (string)(Session["userID"]);
                    // string roleID = (string)(Session["roleID"]);
                }
                Response.Redirect("http://www.microsoft.com");
            }
            else
            {
                //wrong username/password
                Response.Redirect("http://www.microsoft.com");
            }



            // close the connection
            reader.Close();
            conn.Close();

    }
}

【问题讨论】:

  • id 属性添加到您的输入字段。 id="un"id="pw"

标签: c# html asp.net forms


【解决方案1】:

你必须做几件事:

  1. 将表单操作更改为Login.aspx,而不是代码隐藏文件 (.cs)

  2. 从表单中删除 runat="server"。它没有害处,但只有当您的 &lt;form&gt; 标记必须在服务器端呈现时才有意义,而事实并非如此:它是纯 HTML。例如,如果某些&lt;form&gt; 属性依赖于服务器代码,这将是有意义的。

  3. 使用简单的&lt;input type="submit" value="Login button caption"&gt; 按钮发布表单。

  4. Page_Load 事件中实现代码,但前提是IsPostback 为真(这意味着正在呈现页面以响应 POST 请求)。

说明:如果您以标准方式将表单发布到.aspx 文件,则其后面的代码将运行。如果第一次请求登录表单,例如在浏览器中键入其 URL,则请求是 GET,服务器必须将空表单发送到浏览器。但是,如果用户点击提交按钮,这是一个 POST 请求,后面的代码必须尝试登录用户。要区分 GET 和 POST,您可以使用 IsPostback 属性:如果为真,则为 POST。如果没有,这是一个获得。您可以直接在 Page_Load 事件中执行此操作。

在用户输入错误的用户名和密码的情况下,您必须返回原始表单以使用户有新的登录机会。当您使用标准 HTML 控件时,没有ViewState。这意味着发布的un 值(用户名)将丢失,除非您在将表单发送给用户之前将其重新设置在服务器上。可以使用&lt;% = %&gt;语法设置input value属性,使输入的用户名返回给浏览器。

考虑到混合纯 HTML 和服务器控件可能会很棘手。如果你走一条路,你最好跟着它走,不要把事情搞混,除非你很清楚自己在做什么。所以,在我的解释中,我只使用了纯标准的 HTML / HTTP 方法。

【讨论】:

    猜你喜欢
    • 2020-01-23
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多