pr-9 Asp
pr-9 Asp
ASP.Net
Practical-9
Aim:- Create a login page in your web application. Login page must have user
name and password fields. If user enters correct ID, Password, he must be
redirected to the homepage of your website.
Theory:-
1. Designing the User Interface (UI):-
The login page typically includes:
Username Field: An input field for the user to enter their username.
Password Field: An input field for the user to enter their password,
usually with the type="password" attribute to mask the input.
Login Button: A button to submit the login form.
2.Setting up the backend:-
The backend is responsible for verifying the users credentials.
• Database Setup: Storing user credentials (username and password)
securely in a database.
• Server-Side Logic: Writing code to handle the login request, verify
credentials, and manage sessions.
Code 1:-Signup.aspx
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Signup Page</title>
<link href="Styles/Signup.css" rel="stylesheet" />
</head>
<body>
<h2>Signup</h2>
<form id="signupForm" runat="server">
<div class="form-group">
<label for="username">Username:</label>
<asp:TextBox ID="txtUsername" runat="server"
CssClass="input-field"></asp:TextBox>
</div>
<div class="form-group">
<label for="email">Email:</label> 44
ASP.Net
<asp:TextBox ID="txtEmail" runat="server"
CssClass="input-field"></asp:TextBox>
</div>
<div class="form-group">
<label for="password">Password:</label>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"
CssClass="input-field"></asp:TextBox>
</div>
<div class="form-actions">
<asp:Button ID="btnSignup" runat="server" Text="Signup"
OnClick="btnSignup_Click" CssClass="btn" />
</div>
<div class="form-actions">
<asp:Button ID="btnLogin" runat="server" Text="Login"
OnClick="btnLogin_Click" CssClass="btn" />
</div>
</form>
</body>
</html>
Code 2:-Signup.aspx.cs
using System;
using MySql.Data.MySqlClient; // Import MySQL library
namespace SignupLoginApp
{
public partial class Signup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Your existing Page_Load code
}
ASP.Net
try
{
conn.Open();
cmd.ExecuteNonQuery();
// Optionally show a success message or redirect
Response.Redirect("Login.aspx");
}
catch (Exception ex)
{
// Handle exceptions (e.g., show an error message)
Response.Write($"Error: {ex.Message}");
}
}
}
}
}
}
Code 3:-Signup.css
body {
font-family: Arial, sans-serif;
background-color: #808080;
margin: 0;
padding: 0;
display: flex;
justify-content: center; 46
ASP.Net
align-items: center;
height: 100vh;
}
h2 {
text-align: center;
}
form {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 300px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.input-field {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.form-actions {
text-align: center;
margin-top: 15px;
}
.btn {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
} 47
ASP.Net
.btn:hover {
background-color: #45a049;
}
Code 4:-Login.aspx
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Page</title>
<link href="Styles/Login.css" rel="stylesheet" />
</head>
<body>
<h2>Login</h2>
<form id="loginForm" runat="server">
<div class="form-group">
<label for="username">Username:</label>
<asp:TextBox ID="txtUsername" runat="server"
CssClass="input-field"></asp:TextBox>
</div>
<div class="form-group">
<label for="password">Password:</label>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"
CssClass="input-field"></asp:TextBox>
</div>
<div class="form-actions">
<asp:Button ID="btnLogin" runat="server" Text="Login"
OnClick="btnLogin_Click" CssClass="btn" />
</div>
<div class="form-actions">
<asp:Button ID="btnSignup" runat="server" Text="Signup"
OnClick="btnSignup_Click" CssClass="btn" />
</div>
</form>
</body>
</html>
Code 5:-Login.aspx.cs
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic; 48
ASP.Net
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SignupLoginApp
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
conn.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
if (count > 0)
{
Session["Username"] = username;
Response.Redirect("Home.aspx"); 49
}
ASP.Net
else
{
Response.Write("<script>alert('Invalid username or
password');</script>");
}
}
catch (Exception ex)
{
// Handle exceptions (e.g., show an error message)
Response.Write($"<script>alert('Error: {ex.Message}');</script>");
}
}
}
}
protected void btnSignup_Click(object sender, EventArgs e)
{
// Redirect to the signup page
Response.Redirect("Signup.aspx");
}
}
}
Code 6:-Login.css
body {
font-family: Arial, sans-serif;
background-color:#808080;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
h2 {
text-align: center;
}
form {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 300px;
} 50
ASP.Net
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.input-field {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.form-actions {
text-align: center;
margin-top: 15px;
}
.btn {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
margin-bottom: 10px;
}
.btn:hover {
background-color: #45a049;
}
Code 7:-Homepage.aspx
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Home Page</title> 51
ASP.Net
<link href="Styles/Home.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<h1>Welcome, <asp:Label ID="lblGreeting"
runat="server"></asp:Label></h1>
<div class="content">
<p>This is the homepage of your application. You can add more content
here.</p>
</div>
<footer>
<p>© 2024 dhairya joshi</p>
</footer>
</div>
</body>
</html>
Code 8:-Homepage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SignupLoginApp
{
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Get the current hour
int hour = DateTime.Now.Hour;
ASP.Net
greeting = "Good Afternoon";
}
else
{
greeting = "Good Evening";
}
// Display the greeting along with the username (for demo purposes,
using a static username)
lblGreeting.Text = $"{greeting}, User!";
}
}
}
}
Code 9:-Home.css
body {
font-family: Arial, sans-serif;
background: linear-gradient(to right, #ff7e5f, #feb47b);
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: #fff;
}
.container {
background-color: #ff7e5f;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
width: 80%;
max-width: 800px;
text-align: center;
}
h1 {
margin-bottom: 20px;
font-size: 2.5em;
color: #333;
}
.content { 53
ASP.Net
margin-bottom: 20px;
}
footer {
margin-top: 20px;
font-size: 0.9em;
color: #333;
}
Code 10:-Web.config
connectionString="Server=localhost;Database=mydatabase;User=root;Password=
1846;"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
<pages>
<namespaces>
<add namespace="System.Web.Optimization" />
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms"
namespace="Microsoft.AspNet.Web.Optimization.WebForms"
tagPrefix="webopt" />
</controls>
</pages>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime"
publicKeyToken="eb42632606e9261f" /> 54
ASP.Net
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Web.Infrastructure"
publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json"
publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease"
publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930"
newVersion="1.6.5135.21930" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvid
er, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4"
compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider,
Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4"
compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\
"Web\" /optionInfer+" />
</compilers>
</system.codedom>
</configuration> 55
ASP.Net
Output:-
Output:-
56
ASP.Net
Output:-
Output:-
Conclusion:- Creating a secure and functional login page involves careful planning
and implementation of both frontend and backend components. By following best
practices for security and ensuring proper validation and session management, you
can build a robust authentication system for your web application.
58