0% found this document useful (0 votes)
5 views

pr-9 Asp

Uploaded by

dhairyajoshi991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

pr-9 Asp

Uploaded by

dhairyajoshi991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

ITM(SLS) BARODA UNIVERSITY

DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY


Diploma Sem – 5(A)

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

<%@ Page Language="C#" AutoEventWireup="true"


CodeBehind="Signup.aspx.cs" Inherits="SignupLoginApp.Signup" %>

<!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

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

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
}

protected void btnLogin_Click(object sender, EventArgs e)


{
// Redirect to the login page
Response.Redirect("Login.aspx");
}

protected void btnSignup_Click(object sender, EventArgs e)


{
string username = txtUsername.Text;
string email = txtEmail.Text;
string password = txtPassword.Text; 45

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

ASP.Net

// Retrieve the connection string from the Web.config file


string connectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnectio
n"].ConnectionString;

// SQL query to insert data into the database


string query = "INSERT INTO Users (Username, Email, Password)
VALUES (@Username, @Email, @Password)";

using (MySqlConnection conn = new MySqlConnection(connectionString))


{
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
// Add parameters to prevent SQL injection
cmd.Parameters.AddWithValue("@Username", username);
cmd.Parameters.AddWithValue("@Email", email);
cmd.Parameters.AddWithValue("@Password", password);

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

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

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

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

ASP.Net
.btn:hover {
background-color: #45a049;
}

Code 4:-Login.aspx

<%@ Page Language="C#" AutoEventWireup="true"


CodeBehind="Login.aspx.cs" Inherits="SignupLoginApp.Login" %>

<!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

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

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)
{

protected void btnLogin_Click(object sender, EventArgs e)


{
string username = txtUsername.Text;
string password = txtPassword.Text;

// Retrieve the connection string from the Web.config file


string connectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnectio
n"].ConnectionString;

// Use MySQL connection and command


using (MySqlConnection conn = new MySqlConnection(connectionString))
{
string query = "SELECT COUNT(*) FROM Users WHERE Username =
@Username AND Password = @Password";

using (MySqlCommand cmd = new MySqlCommand(query, conn))


{
// Add parameters to prevent SQL injection
cmd.Parameters.AddWithValue("@Username", username);
cmd.Parameters.AddWithValue("@Password", password);

try
{
conn.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();

if (count > 0)
{
Session["Username"] = username;
Response.Redirect("Home.aspx"); 49
}

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

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

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

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

<%@ Page Language="C#" AutoEventWireup="true"


CodeBehind="Home.aspx.cs" Inherits="SignupLoginApp.Home" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Home Page</title> 51

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

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>&copy; 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;

// Determine the greeting based on the time of day


string greeting;
if (hour < 10)
{
greeting = "Good Morning";
}
else if (hour < 18) 52
{

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

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

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

ASP.Net
margin-bottom: 20px;
}

footer {
margin-top: 20px;
font-size: 0.9em;
color: #333;
}

Code 10:-Web.config

<?xml version="1.0" encoding="utf-8"?>


<!--
For more information on how to configure your ASP.NET application, please visit
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="DefaultConnection"

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

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

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=\
&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
</configuration> 55

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

ASP.Net
Output:-

Output:-

56

Name:- Pushti parikh Enrollment No:-22C11071


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Sem – 5(A)

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

Name:- Pushti parikh Enrollment No:-22C11071

You might also like