TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Mohd Aamir
NA
68
4k
without login another pages should not be opened on browser
Jan 12 2019 12:49 AM
my problem is that my all the pages are being accessed without login but i want that it should be accessed untill you don't login and it should be first login then access any pages but oncei log out it should not be accessed and it should happen by using cookies
thanks in advance
here is my code..
login.aspx page
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeFile=
"Admin_Loogin.aspx.cs"
Inherits=
"admin_Admin_Loogin"
%>
<!DOCTYPE html>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head runat=
"server"
>
<title></title>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<div>
<div>
<table style=
"width: 70%;"
>
<caption
class
=
"style1"
>
<strong>Login Form</strong>
</caption>
<tr>
<td
class
=
"style2"
></td>
<td></td>
<td></td>
</tr>
<tr>
<td
class
=
"style2"
>Username:</td>
<td>
<asp:TextBox ID=
"TextBox1"
runat=
"server"
></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID=
"RequiredFieldValidator2"
runat=
"server"
ControlToValidate=
"TextBox1"
ErrorMessage=
"Please Enter Your Username"
ForeColor=
"Red"
></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td
class
=
"style2"
>password:</td>
<td>
<asp:TextBox ID=
"TextBox2"
TextMode=
"Password"
runat=
"server"
></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID=
"RequiredFieldValidator1"
runat=
"server"
ControlToValidate=
"TextBox2"
ErrorMessage=
"Please Enter Your word"
ForeColor=
"Red"
></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td
class
=
"style2"
></td>
<td></td>
<td></td>
</tr>
<tr>
<td
class
=
"style2"
></td>
<td>
<asp:Button ID=
"Button1"
runat=
"server"
Text=
"Log In"
OnClick=
"Button1_Click"
/>
</td>
<td>
<asp:Label ID=
"Label1"
runat=
"server"
></asp:Label>
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
cs code for login
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
//using System.Web.SessionState;
public
partial
class
admin_Admin_Loogin : System.Web.UI.Page
{
SqlConnection con =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
"DBCS"
].ToString());
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
SqlCommand cmd =
new
SqlCommand(
"select * from tb_admin_login where Userid=@username and password=@word"
, con);
cmd.Parameters.AddWithValue(
"@username"
, TextBox1.Text);
cmd.Parameters.AddWithValue(
"@word"
, TextBox2.Text);
SqlDataAdapter da =
new
SqlDataAdapter(cmd);
DataTable dt =
new
DataTable();
da.Fill(dt);
con.Open();
int
i = cmd.ExecuteNonQuery();
con.Close();
if
(dt.Rows.Count > 0)
{
Session[
"id"
] = TextBox1.Text;
HttpCookie loginCookie =
new
HttpCookie(
"Login"
);
//Set the Cookie value.
loginCookie.Values[
"Userid"
] = TextBox1.Text;
loginCookie.Values[
"Password"
] = TextBox2.Text;
loginCookie.Path = Request.ApplicationPath;
//Set the Expiry date.
loginCookie.Expires = DateTime.Now.AddDays(1);
//Add the Cookie to Browser.
Response.Cookies.Add(loginCookie);
Response.Redirect(
"DashBoard.aspx"
);
}
else
{
Label1.Text =
"Your username and password is incorrect"
;
Label1.ForeColor = System.Drawing.Color.Red;
}
}
}
master page :
aspx
<%@ Master Language=
"C#"
AutoEventWireup=
"true"
CodeFile=
"MasterPage.master.cs"
Inherits=
"admin_MasterPage"
%>
<!DOCTYPE html>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head runat=
"server"
>
<title></title>
<asp:ContentPlaceHolder ID=
"head"
runat=
"server"
>
</asp:ContentPlaceHolder>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<div>
<p>
<asp:Label ID=
"Label1"
runat=
"server"
></asp:Label>
</p>
<p>
<asp:Button ID=
"Button1"
runat=
"server"
Height=
"47px"
OnClick=
"Button1_Click"
Text=
"Logout"
/>
</p>
<asp:ContentPlaceHolder ID=
"ContentPlaceHolder1"
runat=
"server"
>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
master cs page
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
using
System.Web.Security;
public
partial
class
admin_MasterPage : System.Web.UI.MasterPage
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!
this
.Page.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cookies[
"Userid"
].Expires = DateTime.Now.AddDays(-1);
Response.Cache.SetNoStore();
HttpCookie loginCookie = Request.Cookies[
"Login"
];
if
(loginCookie !=
null
)
{
string
name = loginCookie.Values[
"Name"
];
string
password = loginCookie.Values[
"Password"
];
// User validation based on cookies if required for valid user as might be user can change his password in any cases
// so call your databse user login validation method
//if //(name.ToLower() == "sa" && password.ToLower() == "sa") // user validation code
Label1.Text = (
string
)Session[
"id"
];
}
else
{
Response.Redirect(
"Admin_Loogin.aspx"
);
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
Response.Cookies[
"Userid"
].Expires = DateTime.Now.AddDays(-1);
Response.Redirect(
"Admin_Loogin.aspx"
);
}
}
Reply
Answers (
2
)
printing using .net using window api
How to learn ASP.NET?