Forms Authentication how to get UserID

  • Thread starter Thread starter ThatsIT.net.au
  • Start date Start date
T

ThatsIT.net.au

I am using Forms Authentication but I want to keep more info on user than
forms authentication keeps.
I thought of 2 ways of doing this
1. To have a linked table holding other data, but for this I need to be able
to get the userID of the logged on user. how do I do this.
2. the other Idea I had was to add more fields to the forms authentication
user table. but I would still need the userID and I'm not sure what
implications this would have.

Any one know how to get the userID of the logged on user or a better way.

thanks
 
ThatsIT.net.au said:
I am using Forms Authentication but I want to keep more info on user than
forms authentication keeps.
I thought of 2 ways of doing this
1. To have a linked table holding other data, but for this I need to be
able to get the userID of the logged on user. how do I do this.
2. the other Idea I had was to add more fields to the forms authentication
user table. but I would still need the userID and I'm not sure what
implications this would have.

Any one know how to get the userID of the logged on user or a better way.

thanks
The standard way would be to create your own Membership Provider and/or User
classes, then you can access any information via the User object.
 
I am using Forms Authentication but I want to keep more info on user than
forms authentication keeps.
I thought of 2 ways of doing this
1. To have a linked table holding other data, but for this I need to be able
to get the userID of the logged on user. how do I do this.
2. the other Idea I had was to add more fields to the forms authentication
user table. but I would still need the userID and I'm not sure what
implications this would have.

Any one know how to get the userID of the logged on user or a better way.

thanks

When the user logs in and is authenticated successfully store the
userID in a Session state variable e.g.

// Suppose you have the credentials stored in web.config
// (otherwise you would use an authentication function of your own
// that verifies against a database).

if(FormsAuthentication.Authenticate(txtUsername.text,
txtPassword.text))
{
Session["userID"] = txtUsername.Text;
FormsAuthentication.ReturnFromLoginUrl(...)
// etc
}

where txtUsername, txtPassword are a TextBoxes for entering the
username and password on the login form.

Then anytime later you can retrieve it thus:

string UserID = (string)Session["UserID"];


If, on the other hand, you are using Membership then the GetUser()
method will retrieve all details of the currently logged in user.
 
Back
Top