Wednesday, 9 April 2014

Simple Login and Redirect for ASP.NET and Access

Simple Login and Redirect for ASP.NET and Access:
Aspx Page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" autocomplete="off">
        <div>
            User Name:&nbsp;&nbsp;
            <asp:TextBox ID="UserName" runat="server" /><br />
            Password:&nbsp;&nbsp;&nbsp; &nbsp;
            <asp:TextBox ID="Password" runat="server" TextMode="Password" /><br />
            <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label><br />          
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;          
            <asp:Button ID="Login" runat="server" Text="Log In" OnClick="Login_Click" />
        </div>
    </form>
</body>
</html>

C# Page:
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Login_Click(object sender, EventArgs e)
    {
       
        string connect = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        //string connect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Project\\Practice\\WebSite_Access\\App_Data\\contacts.mdb;Persist Security Info=False";
              
        string query = "Select Count(*) From Users Where username = ? And password = ?";
        int result = 0;
        using (OleDbConnection conn = new OleDbConnection(connect))
        {
            using (OleDbCommand cmd = new OleDbCommand(query, conn))
            {
                cmd.Parameters.AddWithValue("", UserName.Text);
                cmd.Parameters.AddWithValue("", Password.Text);
                conn.Open();
                Session["User"] = UserName.Text;
                result = (int)cmd.ExecuteScalar();
            }
        }
        if (result > 0)
        {
            Response.Redirect("LoggedIn.aspx");
        }
        else
        {
            lblMessage.Text = "Invalid credentials";
        }
    }
}

Web Config:
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <connectionStrings>
    <add name="MyConnectionString"
    connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=&quot;D:\\Project\\Practice\\WebSite_Access\\App_Data\\contacts.mdb&quot;"
        providerName="System.Data.OleDb" />
  </connectionStrings>
 
</configuration>