I am having a problem getting HTML input values using C# for a Login page. I have had luck using just a regular ASP element, but not so much with pure HTML.
I am trying to have the C# grab the username input and password from the html form to then send as a string to a MySQL db proc. If the username/password combination does not exist, I am trying to redirect back to the Login.aspx page and if it does "google.com". The username and password are also saved in a session.
My HTML looks like the following:
<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>
And my C# looks like the following:
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();
}
}
idattribute to your input fields.id="un"andid="pw"