how to store login info in session in asp.net

Posted by Shanky11 under ASP.NET on 3/1/2013 | Points: 10 | Views : 32868 | Status : [Member] | Replies : 10
i have a login page after entering correct info user go to home page .
but by copying url of home page he diectly runs this page without login.
For this i have to stop this action so that he cant be abl;e to go to home page wothout correct credential




Responses

Posted by: Jayakumars on: 3/1/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

try this code login

if (DtLogin.Rows.Count > 0)
{
Session["UserId"] = DtLogin.Rows[0]["UserId"].ToString();
Session["Username"] = DtLogin.Rows[0]["UserName"].ToString();
}

without credential page login

if (Session["Username"].ToString() == "Admin")
{
Response.Redirect("Home.aspx");
}


Mark as answer this help for u

Mark as Answer if its helpful to you

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
hello jaya
i m saying after login whien i goes to welcome page.and copying url after that close that page and open a new tab paste it there i just want to stoop this action

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

You need to stop that action right.
Each page has master page right.
So check whether the user is an authenticated user in the master page.
Every time master page executes right.
So that, you can easily avoid that action..
Example:

If session value is null then dont allow the user to access the page..
Just check the session value in the master page
if(Convert.ToString(Session[LoginId]) == "")
{
Response.Redirect("LoginPage.aspx");
}
This will not allow the user to access the page without the login page.


Thanks & Regards,
Santhi .V

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shameer.Ali87 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
@shanky11,

1. First in your login page, on button click event in code behind,

after checking the user is a valid user or not, i.e.,
// after ([Data_table/Dataset].Rows.Count > 0) ,

store the username in a session value as below..

Session["username"]="txtbx1.Text";


2. And,

then in your home page,

write below code in page load,

 protected void Page_Load(object sender, EventArgs e)

{
// Check if the user is already loged in or not
if ((Session["username"] != null) && (Convert.ToBoolean(Session["username"]) == true))
{
Response.Redirect("main_page.aspx");
}
else
{
Response.Redirect("login.aspx");
}

}


thank you

shameer ali shaik

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
No U didn't get my point
i m saying after login and coming to home page and close hoe page by copying its url and open a new tab it should not open that page again instead it should shows login page if u try to open home page directly

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
String was not recognizegd as a valid Boolean.
See this error is coming

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
hello Sameer
i think u r not getting ;my point
open uor gail a/c
copy it url
close it by logout
paste url in another tab
see what happens again login page will open not yours home page



Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raja_89 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
Hai
We can do this in two ways
1.By Sql code
2.By restricting the copy of an address in an url and forced to redirect to login page [or your home page] by UrlReferrer

Request.UrlReferrer can be used to retrieve the previous page url from where the current page has been redirected.

Code
string strPreviousPage = "";
if (Request.UrlReferrer != null)
{
strPreviousPage = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
}
if(strPreviousPage =="")
{
Response.Redirect("~/Login.aspx");
}

Regards




Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
hello raja
i don't want it to do with sql
instead within coding page itself



Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chandradev819 on: 5/26/2014 [Member] Starter | Points: 25

Up
0
Down
Hi

We can solve that issue like this

http://gopalanmani.blogspot.in/2013/08/avoid-sharing-of-same-session-across.html

Thanks and Regards
Chandradev
My Blog

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response