I have an application with login form, and I want after the login to show on a label or something , the username and other information from my database with that user.
For example , after you login with username "admin" , to show me on next form a text "Welcome Administrator!" . Or if I login with username "john", to show me on next form a text "Welcome John Snow!" .
How I can do that ?
You'd have to keep track of a session identifier and use that to track a user through a current session and you could then display that on your GUI through whatever language/api you are using in your application. Alternatively if you don't want to or need to keep track of your user past the login page you could just forward that to the class that handles the next page by allowing the next class to inherit the previous.
However it's hard to get much more specific without more details on your project.
Best way is to put the UserName variable in Session and access it in the next page.
Pseudo Code...
In the login page after authentication check write this (assuming 2nd page is Home.aspx):
Session["USER_NAME"] = userName;
Respinse.Redirect("Home.aspx");
In Home.aspx page_load function, write this to fetch the name from session and display, and redirect user to login page if no session value present.
if(Session["USER_NAME"] != null)
{
lblUserName.Text = Session["USER_NAME"].ToString();
}
else
{
Respinse.Redirect("Login.aspx");
}
Related
I looking for a tutorial of how i can make my wesite toobar show the name of the user after he login so he can just press on his name in the toolbar and move him to his personal page such as your user name and your photo on the top of the forums.
Many thanks
It can be done in several different ways, however this is probably the simplest. Save the username in a session variable:
Session["UserName"] = txtUsername.text;
and in all the other pages , you will need to have a Label that shows the name of the user who is currently logged in. You can get the value from the "session" in this way:
labelUser.Text = Session["UserName"].Tostring();
Do not forget to set the session to null when the user logs out:
Session["Username"] = null
How to redirect on his page, just use again the value of session variable to redirect to his page.
I have 3 parts to my site, Site Master, User Control (inside Site Master) and General page.
When a user logs in they are redirected to the General Page. The User Control is a dropdownlist of different accounts (Auto select first account) that get's their username and runs stored procedure to pull their information into a SiteID session Variable.
Then in the General page I set all the labels to the users information. with this code.
if (Session["SiteID"] != null)
{
SiteID = int.Parse(Session["SiteID"].ToString());
PopulateAccountData();
PopulateAccountInformation2();
PopulateSiteNodes();
PopulateSiteMap();
}
else
{
LabelSiteName.Text = "No Site Selected";
}
The problem is when the page loads for the first time it does not have the Session["SiteID"], I have to hit refresh for everything to load.
I am new to ASP.net so I'm not sure if I'm doing this right, but how do I get everything to load the first time?
Use : IsPostBack
Look : //www.java-samples.com/showtutorial.php?tutorialid=1083
I am developing a application in asp .net 4.0.
Here when I am logged in to my application, go to inner pages, say "Manage Role" and copy the link and click on Logout button it displays my login page. Now i paste that link in another tab and expect it to display me the login page but it does not happen, it shows me Manage Role page.
So please give me some solution, how can i show login page in this case?
you have to use session variables to check whether the user is logged in or not and thenin each of ur pages..in the page load event u have to check whether that session user variable is null or not if null den redirect it to login page
Try using Session Destroy function available in ASP.Net
if you are using C#
try:
Session.Abandon();
If you want to remove a specific item from the session use (MSDN):
Session.Remove("YourItem");
EDIT: If you just want to clear a value you can do:
Session["YourItem"] = null;
If you want to clear all keys do:
Session.Clear();
And Then Check for Session Value on every page you want to restrict only with Session Access(for Logged In User).
In Your page "Manage Role",In Your Page load, Check that user info is available or not in the session.
and When you Click on log out button,First Remove User Information from Session and then Redirect it Login Page.
First Remove All Session on logout button click event
Session.Abandon();
Session["SessionName"] = null;
Session.Clear();
Then Write Code in Global.asax File
Void Application_Error(object sender,EventArgs e)
{
Response.Redirect("LogIn.aspx");
or
Server.Transfer("LogIn.aspx");
}
I have a videoWall and a videoWallDetail page. Currently, this is what I have on the videoWallDetail page:
if (String.IsNullOrEmpty(Request["video_id"])) Response.Redirect("videoWall.aspx");
Users should not enter the detail page this way, but if they know the video id, some might go straight to the detail page by typing it into the url, so this will redirect users back to the videoWall page if they enter a video id in the address bar that is not valid. My question is how do I redirect back to the videoWall page regardless. If a user does not click on a link to get to the detail page of a particular video, they get sent back no matter if they enter a valid id into the address bar.
Thanks in advance!
You could add a Session on the videoWall.aspx page to verify they've come from that page:
videoWall.aspx
Session["fromVideoWall"] = true;
Response.Redirect("videoWallDetail.aspx?video_id=" + videoId.ToString());
videoWallDetail.aspx
if (Session["fromVideoWall"] != null && String.IsNullOrEmpty(Request["video_id"])) Response.Redirect("videoWall.aspx");
Session["fromVideoWall"] = null; //Setting the value back to null ensures the next access must have come from VideoWall.aspx too
1 You can use PostBackUrl method
And in the target page access to PreviousPage and get the data
Link : http://asp-net-example.blogspot.fr/2008/10/postbackurl-example-how-to-submit-page.html
2 You can also rewrite your url with HttpModule
Link : http://msdn.microsoft.com/en-us/library/ms972974.aspx
If i'm not allowed to just enter a known ID into the query string of the videoWallDetail page, then you shouldn't expose that as a query string param. What if I want to bookmark the page, or email someone a URL for them to go straight to? Are those disallowed?
If so, then you need to remove that querystring param. The URL bar should be a perfectly legal way for users to get around. If they can enter a URL to get to a page they shouldn't without a 400 or 500 error, then its YOUR fault as the developer.
Switch over to using a session variable or a form post to confirm they are coming from the proper place if they truly shouldn't be on Page B without having just come from Page A.
I have a working login form in an asp.net application. Standard stuff with a username and password text box and a button to process the login. Works fine.
I have a new requirement to allow the user to input the username and password from a separate plain html page that is not a part of my asp.net application. I plan on achieving this using standard html - form, input, submit button etc. The form action will be the URL of my asp.net login page and its method will be POST.
What I want to do in the C# code behind page of the asp.net login form, presumably in the Page_Load event, is to check if the request for the page contains a username and password value being passed in. If it does then I need to read those values and process the login as if someone had clicked the login button on the asp.net page. If not then I will display the login form as usual.
How do I check for the existence of, and read, the username and password values in the request for my page.
Read the Request.Form NameValueCollection and process your logic accordingly:
NameValueCollection nvc = Request.Form;
string userName, password;
if (!string.IsNullOrEmpty(nvc["txtUserName"]))
{
userName = nvc["txtUserName"];
}
if (!string.IsNullOrEmpty(nvc["txtPassword"]))
{
password = nvc["txtPassword"];
}
//Process login
CheckLogin(userName, password);
... where "txtUserName" and "txtPassword" are the Names of the controls on the posting page.
if (!string.IsNullOrEmpty(Request.Form["username"])) { ... }
username is the name of the input on the submitting page. The password can be obtained the same way. If its not null or empty, it exists, then log in the user (I don't recall the exact steps for ASP.NET Membership, assuming that's what you're using).
NameValueCollection nvclc = Request.Form;
string uName= nvclc ["txtUserName"];
string pswod= nvclc ["txtPassword"];
//try login
CheckLogin(uName, pswod);