how can i track a logged in user

Posted by Mongz under ASP.NET on 9/16/2009 | Views : 2781 | Status : [Member] | Replies : 5
Hi once again.
I need a strategy to keep track of individual user that logged in, e.g if the user want to update his/her profile then i want to user that strategy on a where clause to retrieve or update the users profile.
Im confused about this, your help will be appreciated.




Responses

Posted by: Rtpharry on: 9/16/2009 [Member] [MVP] Bronze

Up
0
Down
Hey,

You should be using the built in asp.net features of authentication, membership, profiles etc.

If you are then you can get the currently logged in user in a really simple way:

string username = System.Web.HttpContext.Current.User.Identity.Name;


I also have some code which lets you create a custom parameter if you are using <asp:Parameter> derived params in your code?

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

Posted by: Neeks on: 9/22/2009 [Member] Bronze

Up
0
Down
You can store the ID of the user in Session so that you can access it through out your application

int userId = 12; //Your Id from the Query

Session["userId"] = userId


Now you can access it directly as

insertFunction(......, userId)
{

}

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

Posted by: Mongz on: 9/23/2009 [Member] Starter

Up
0
Down
I tried to use that technique of sessions, im passing it from login page and using it on another page, now i recieve this error "Object reference not set to an instance of an object."

here im passing it
Session["UserName"] = Login1.UserName;

then accessing on different page like this
username = Session["UserName"].ToString();

Please help me on how can i access it.

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

Posted by: Mouli on: 9/23/2009 [Member] Starter

Up
0
Down
Login1.UserName is Null. Thats why you are getting that error. Check what value you are getting in the Login1.UserName property. Can you tell me how exactly you retrieve the credentials from the login page in this page?



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

Posted by: Rtpharry on: 9/24/2009 [Member] [MVP] Bronze

Up
0
Down
This is why I suggested using asp.net membership because you set up the database, configure your provider in the web.config and then it all just works :)

http://msdn.microsoft.com/en-us/library/ms998347.aspx

You can just use the snippet I posted before to get the logged in users name from any page, you can also just drop all the login related controls into your pages and they work with zero configuration and zero code.

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

Login to post response