Skip to content

ParallelCodes

  • AngularJS
  • Android
    • Android-Creating Notepad
    • Android Widgets Tutorial
    • Android Send SMS Programmatically with Permissions
    • Making Phone Calls in Android
    • Android JSON Parsing
    • Upload Files from Android to PC Programmatically
    • Android Interview Ques & Ans
  • ASP.NET
  • MVC
  • Xamarin
  • C#
  • WPF
  • CSS Templates

ParallelCodes

  • AngularJS
  • Android
    • Android-Creating Notepad
    • Android Widgets Tutorial
    • Android Send SMS Programmatically with Permissions
    • Making Phone Calls in Android
    • Android JSON Parsing
    • Upload Files from Android to PC Programmatically
    • Android Interview Ques & Ans
  • ASP.NET
  • MVC
  • Xamarin
  • C#
  • WPF
  • CSS Templates

ASP.NET Login page using Sessions

  • by ASH
  • March 8, 2017March 8, 2017
  • 2 Comments
Post Views: 2,986

I have posted couple of posts in last two days regarding sessions in ASP.NET. In this post I’ll be explaining how to create a login page with sessions in ASP.NET and also wiping it out, I mean deleting the sessions in ASP.NET :).

Anyways here are my previous two posts regarding sessions :

  1. ASP.NET Creating and Retrieving Sessions example
  2. ASP.NET Creating and retrieving Sessions with MS SQL server

ASP.NET Login page using Sessions

Make a new page in your project with name frmLogin.aspx and edit it as below :

frmLogin.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmLogin.aspx.cs" Inherits="FrmLogin" %>

<!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>Login Page</title>
</head>
<body>
 <form id="form1" runat="server" style="padding: 5px; font-family: Arial; color: #1c1c1c;">
 <div>
 <h2>
 Enter your credentials</h2>
 <hr />
 Username : &nbsp;&nbsp;
 <asp:TextBox runat="server" ID="txtusername" />
 <br />
 <br />
 Password : &nbsp;&nbsp;&nbsp;
 <asp:TextBox runat="server" ID="txtpassword" TextMode="Password" />
 <br />
 <asp:Button runat="server" Text="Login" ID="btnLogin" OnClick="btnLogin_Click" />&nbsp;&nbsp;
 <asp:Button runat="server" Text="Clear" ID="btnClear" OnClick="btnClear_Click" />
 <br />
 <br />
 <asp:Label runat="server" ID="lblMessage" />
 </div>
 </form>
</body>
</html>

Now edit its code file as below.

FrmLogin.aspx.cs :

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

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

 }
 protected void btnLogin_Click(object sender, EventArgs e)
 {
 SqlConnection con = new SqlConnection("Data Source=hitesh\\sqlexpress;Initial Catalog=parallel;User ID=hitesh;Password=789;");
 con.Open();
 SqlCommand cmd = new SqlCommand("Select * from tblUsers where Username=@Username and Password=@Password", con);
 cmd.Parameters.AddWithValue("@Username", txtusername.Text.ToString());
 cmd.Parameters.AddWithValue("@Password", txtpassword.Text.ToString());
 SqlDataReader reader = cmd.ExecuteReader();
 if (reader.Read())
 {
 Session["userid"] = Convert.ToInt32(reader["userid"].ToString());
 Session["Name"] = reader["Name"].ToString();
 Session["Role"] = reader["Role"].ToString();
 reader.Close();
 cmd.Dispose();
 con.Close();
 Response.Redirect("index.aspx");
 }
 else
 {
 reader.Close();
 cmd.Dispose();
 con.Close();
 lblMessage.Text = "Invalid credentials";
 }
 }
 protected void btnClear_Click(object sender, EventArgs e)
 {
 txtpassword.Text = "";
 txtusername.Text = "";
 }
}
ASP.NET Login page using Sessions 01

ASP.NET Login page using Sessions – frmLogin.aspx design

Here’s the Database script for Database creation :

Database script :

create database parallel;
use parallel;
create table tblUsers
(
UserId int identity(1,1) not null primary key,
[Name] nvarchar(50) not null,
Username nvarchar(50) not null,
Password nvarchar(50) not null,
Role nvarchar(50) not null
);
insert into tblUsers ([Name],Username,Password,Role) values ('Steve','Steve1','steve','Admin');
insert into tblUsers ([Name],Username,Password,Role) values ('John','John1','john','User');
select * from tblUsers

And now make a new form with name Index.aspx, and edit it as below.

Index.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>

<!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>Homepage</title>
</head>
<body>
 <form id="form1" runat="server" style="padding: 5px; font-family: Arial; font-weight: bold;">
 <div>
 <asp:Label runat="server" ID="lbluserInfo"></asp:Label>
 <br />
 <br />
 <asp:Button ID="btnLogout" runat="server" BackColor="White" BorderStyle="None"
 Font-Bold="true" Text="Logout" Font-Size="16px" ForeColor="Red" OnClick="btnLogout_Click" />
 </div>
 </form>
</body>
</html>

and edit it’s code file as below.

Index.aspx.cs :

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;

public partial class Index : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 if (Session["Name"] == null)
 Response.Redirect("FrmLogin.aspx");
 else
 {
 String userid = Convert.ToString((int)Session["userid"]);
 String username = Session["Name"].ToString();
 String userrole = Session["Role"].ToString();
 
 lbluserInfo.Text = "Welcome, " + username + ". Your userid is " + userid + " and your role is " + userrole + ".";
 }
 }
 protected void btnLogout_Click(object sender, EventArgs e)
 {
 Session["Name"] = null;
 Session["Role"] = null;
 Response.Redirect("FrmLogin.aspx");
 }
}
ASP.NET Login page using Sessions 02

ASP.NET Login page using Sessions – Index.aspx design

On button click event I’m making the session value as null.


Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Skype (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to email a link to a friend (Opens in new window)

Related

Tags:asp.netloginsessions

2 thoughts on “ASP.NET Login page using Sessions”

  1. Keisha December 12, 2019 at 6:06 pm

    Thanks for finally writing about >ASP.NET Login page using Sessions o
    ParallelCodes <Loved it!

    Reply
  2. Pingback: asp.net login session example - bestdatatoday

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get all the latest updates for free on Facebook

Get all the latest updates for free on Facebook
 

ASP.NET, C#
  • C# – Tuples
  • C# Array.Contains() – Incase Sensitive Search – Ignore Case
  • Create Login API using .NET Core | ASP.NET C#
  • jQuery – Get Selected Radio Button value
  • C# Windows Form – Creating PDF documents using iText7 library.
  • ASP.NET C# – Create PDF Invoice using iText7 Library
  • ASP.NET – Creating and Downloading PDF Files
  • ASP.NET Upload Multiple Files using FileUpload
  • ASP.NET – Create and Write Text to File
  • ASP.NET – Insert data using SQL Stored Procedures
  • Create ASP.NET Login page with SQL Database
  • Android Create Bottom Navigation bar
  • ASP.NET Gridview Paging Example
  • ASP.NET MVC – Using JQuery Datepicker
  • ASP.NET MVC – How to Use REST API Webservice
  • ASP.NET MVC – How to Create REST API Webservice
  • Xamarin Forms – How to use WebView
  • ASP.NET – Use SQL Stored Procedure on WebForms
  • Create and Consume REST API webservices in ASP.NET MVC
  • MVC – How to Upload and Download Files in ASP.NET MVC Tutorial
  • ASP.NET How to Upload and Download Files with SQL Database
  • ASP.NET How to Upload Files Save Path to Database
  • ASP.NET – How to download files
  • ASP.NET GridView CSS Designing using Bootstrap
  • How to use JQuery DatePicker on ASP.NET web forms

Neve | Powered by WordPress