connectseekers.com

Wednesday, 18 June 2014

Simple Login Form In Asp.Net Using C#.Net and VB.Net | How to Create Login Form in Asp.Net Using C#

6/18/2014 - By Pranav Singh 1

Security is one of the mail concern web worlds. So login form is one of the pages which we use for authenticating the user type before allowing the user to view the secure page. In this article I will show you how you can create a simple login form in asp.net using c#.net and vb.net.

In this I will also tell you how you can restrict a user to access a secure page without login. In this I have access database OleDbConnection, OleDbDataAdapter.


So for this article first we will create a new asp.net application and add two pages in it. First for login and other for redirect after successful login.

Before that first we will create our table.


Now here the both the page HTML

Login page HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SimpleLogin.aspx.cs" Inherits="ProjectDemo_Asp.et.SimpleLogin" %>

<!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>Simple Login Form In Asp.Net Using C#.Net and VB.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="30%" cellpadding="5" cellspacing="5" border="1" style="border:1 solid black;border-collapse:collapse;">
        <tr>
       <td colspan="2" style="background-color:Gray;color:White;"><b>Login Form</b></td>
       </tr>
       <tr>
       <td align="right">User Id:</td>
       <td> <asp:TextBox ID="txtuserid" runat="server"></asp:TextBox></td>
       </tr>          
        <tr>
       <td align="right">Password :</td>
       <td> <asp:TextBox ID="txtpassword" runat="server"></asp:TextBox></td>          
       </tr>
       <tr>
       <td align="center" colspan="2"><asp:Button ID="btnlogin" runat="server"
               Text="Login" onclick="btnlogin_Click" />
       </td>
       </tr>
       </table>
    </div>
    </form>
</body>
</html>

In above code I have added two textbox one for user id and other for password and button control for login the user.

Secure page HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Success.aspx.cs" Inherits="ProjectDemo_Asp.et.Success" %>

<!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">
    <div>
    <h2>Successfull Login</h2>
    </div>
    </form>
</body>
</html>

Now we will check the code part of our login page

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

namespace ProjectDemo_Asp.et
{
    public partial class SimpleLogin : System.Web.UI.Page
    {
        public string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\bookstore.mdb;Persist Security Info=False;";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            DataTable objdt = new DataTable();
            objdt = ValidateUserDetail(txtuserid.Text, txtpassword.Text);
            if (objdt.Rows.Count > 0)
            {
                /*Invoke session value to mantain usee login to other poages
                 For user authentication in secure page
                 */
                Session["userlogin"] = txtuserid.Text;
                Response.Redirect("Success.aspx");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Correct", "alert('Wrong user id aand password.')", true);
            }
        }
        /// <summary>
        /// Function for binding retribing the data from database
        /// </summary>
        /// <returns></returns>
        public DataTable ValidateUserDetail(string userid, string password)
        {
            DataTable _objdt = new DataTable();
            string querystring = "select * from UserDetail where userid='" + userid + "' and pwd='" + password + "';";
            OleDbConnection _objcon = new OleDbConnection(connectionstring);
            OleDbDataAdapter _objda = new OleDbDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }
    }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.OleDb

Namespace ProjectDemo_Asp.et
    Partial Public Class LightBoxLogin
        Inherits System.Web.UI.Page
        Public connectionstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\bookstore.mdb;Persist Security Info=False;"
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        End Sub

        Protected Sub btnlogin_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim objdt As New DataTable()
            objdt = ValidateUserDetail(txtuserid.Text, txtpassword.Text)
            If objdt.Rows.Count > 0 Then
                'Invoke session value to mantain usee login to other poages

                Page.ClientScript.RegisterStartupScript(Me.[GetType](), "Correct", "alert('Lopgin Successfull.')", True)
            Else
                Page.ClientScript.RegisterStartupScript(Me.[GetType](), "Correct", "alert('Wrong user id aand password.')", True)
            End If
        End Sub
        ''' <summary>
        ''' Function for binding retribing the data from database
        ''' </summary>
        ''' <returns></returns>
        Public Function ValidateUserDetail(ByVal userid As String, ByVal password As String) As DataTable
            Dim _objdt As New DataTable()
            Dim querystring As String = "select * from UserDetail where userid='" + userid + "' and pwd='" + password + "';"
            Dim _objcon As New OleDbConnection(connectionstring)
            Dim _objda As New OleDbDataAdapter(querystring, _objcon)
            _objcon.Open()
            _objda.Fill(_objdt)
            Return _objdt
        End Function
    End Class
End Namespace

In above code we have created a function whose return type is DataTable and passed the userid and password as parameter.  Now check the button click event. In this we have passed the userid and password textbox value. if we are getting value in our data table on that case we will user is valid otherwise we will show message for wrong user id and password.

Now we will check the code of secure page

C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectDemo_Asp.et
{
    public partial class Success : System.Web.UI.Page
    {
        /// <summary>
        /// Secure page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userlogin"] == null)
            {
                Response.Redirect("SimpleLogin.aspx");
            }
        }
    }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace ProjectDemo_Asp.et
    Partial Public Class Success
        Inherits System.Web.UI.Page
        ''' <summary>
        ''' Secure page
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Session("userlogin") = Nothing Then
                Response.Redirect("SimpleLogin.aspx")
            End If
        End Sub
    End Class
End Namespace

In above code we have checked that weather the session is having any value or not. If it’s having value on that case user if allow the access the page otherwise he is a unauthorized to access the page redirect him to login page.

Now run the page. Here is the final output preview



DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

1 comment:

  1. Wow, that's what I was searching for, what a material!

    existing here at this website, thanks admin of this website.


    Feel free to surf to my site ... subway surfers hack ios

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top