I am having an aspx login page(login.aspx) which takes the username and password. On submitting the form, the action attribute redirects to another page(validator.aspx) where I simply want to compare it with database credentials and verify if it is correct. On success, I want to redirect to homepage, else display alert message("Invalid credentials").
Below is the code I have in validator.aspx page: (Please note, I am not using code behind for some reasons. Hence using this aspx page)
<%@ Page Title="" Language="C#" MasterPageFile="~/project/MasterPage.master" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
private SqlConnection conn;
private SqlCommand cmd;
string uname, upass;
protected void page_Load(object sender, EventArgs e)
{
uname = Request.QueryString["name"];
upass = Request.QueryString["pass"];
string connectionString = ConfigurationManager.ConnectionStrings["myconn"].ToString();
conn = new SqlConnection(connectionString);
cmd = new SqlCommand("", conn);
cmd.Parameters.Add("@username", SqlDbType.VarChar);
cmd.Parameters["@username"].Value = uname;
cmd.Parameters.Add("@password", SqlDbType.VarChar);
cmd.Parameters["@password"].Value = upass;
string query = "SELECT * FROM mydb.mytable WHERE username = @username AND password = @password";
try
{
conn.Open();
cmd.CommandText = query;
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
Response.Redirect("homepage.html");
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Invalid Login Credentials');", true);
}
}
finally
{
conn.Close();
}
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
The problem is values are not posting and I get error message that username and password expects a value. Can someone tell the resolution for this? Also I want to know if there are any other approaches for achieving this login verification.