Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get username of current logged user with this method:
User.Identity.Name


how can i get current logged userId ?
Thanks!
Posted
Comments
Kornfeld Eliyahu Peter 6-May-14 15:58pm    
Where did you stored that user id?
Gridi Kono 6-May-14 16:02pm    
userid is stored in table Users of my database. I am using forms authentication in my project
Kornfeld Eliyahu Peter 6-May-14 16:04pm    
So you can read user id from the DB using user name...
Gridi Kono 6-May-14 16:07pm    
i got username: Label1.Text = User.Identity.Name; and display it in default.aspx via Label . How can i display userid with this way ?

First read user id from your database into some DataTable, than assign the user id to an other label...
C#
Label2.Text = MyDataTable.Rows[0]["user_id"]
 
Share this answer
 
Comments
Gridi Kono 6-May-14 16:18pm    
Thank you Kornfeld Eliyahu Peter
Does exist any function like (or similary) User.Identity.Name to get user_id ?
Kornfeld Eliyahu Peter 7-May-14 2:13am    
User.Identity.Name is filled by the login procedure (by the framework. It uses an object that implements IIdentity interface. You can replace it with an object of yours, that too implements IIdentity, but has some more properties - like id!
You may want to read this, to begin with - http://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity
SQL
No..

You have to read it from database and the use like this


 lblUserID.Text = dt.Rows[0]["user_id"]
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 7-May-14 2:13am    
'No' is only answer when using the default identity object, but one can replace it with his own...
When you are successfully logged in then store required data in Session like this before you redirect to new page

C#
Session["FullName"]= //from data
Session[UserId]= //from data

Response.Redirect("NewPage.aspx");



In newpage or any other page you can get this session back

if(Session["FullName"]!=null)
{
    string name= Session["FullName"].ToString();
    int UserID= Convert.ToInt32(Session["UserId"].ToString());
}
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 7-May-14 2:06am    
IMHO - it's better not to use Session for nothing. The web, by it's nature is stateless, involving state can be problematic, and in my experience is needless...
Fetch userid from database using the corresponding username..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900