Return user ID for logged in user. C#, ASP.Net


I am building a project in ASP.NET with C# as the back end. I would like to know how I can return the userID of the current logged in user.
I am not using the built in ASP.NET membership due to web service constraints.
User information is currently stored in a remote SQL database (User_Accounts) with the following table structure:

I am looking to return the value from the UserID column for the current logged in user.

I understand this question is vague, but I really do not know where to begin with this task, so if you require further information please let me know.

Who is Participating?
Ohh I got you..
I guess following links may help you.

The GetUser method retrieves the user information from the data source and creates a MembershipUser object populated with the returned data. If you use one of the GetUser overloads that does not take a username parameter, GetUser returns the information for the current logged-on membership user. The current logged-on membership user is identified by the Name of the user in the current HttpContext.


and add to that , should configure as  
<identity impersonate="true"/>

<authentication mode="Windows"/>

On login screen you can keep a current logged  in user in session and then you  can use that through out the life of your session.
Assumed that you are developing Web application.
idny99Author Commented:
Thanks Cenjoy.
I will look into using this method.

Do you have further explanation about how this can be done using C# and
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

I think what you want to do is turn on windows authentication (an make sure to disable all other forms of authentication).  This way you can tell who the user is (they must be on the same domain as your web server).  See

Create a session variable (This can be done using C# only)
you have log in button click event
on which you will validate username and password.
after validation you can set the logged in userid / his user name in session variable
like session("loggedINuserid") = userid;
or  session("loggedINuserName") = userName;

and then on any page during this session you can retrive this value from session
like asssign value to master page lable as "Logged In user : " +session("loggedINuserName").ToString();

Hope this much info would be sufficent / helpful for you.
Please let me know if some more help required.


idny99Author Commented:
Hello again.

Unfortunately I do not think a session ID will be as useful.
I need the user ID from the database.

I am building a questionnaire web application and would questionnaires to be owned by different users.
For example:

User 1 (userID 212) creates Questionnaire A
Therefore in the database in the Questionnaires table will read as such.

Questionnaire Name = Questionnaire A
Questionnaire Desc = example questionniare
Owner = UserID 212

But before this can be done I need to know the User ID so I can then say which user owns which questionnaire.

I hope this makes sense. Sorry for the trouble.
idny99Author Commented:
I have tried the following code but it is not currently working.
Hopefully this will give you a greater understanding of what I am trying to do.

(the user should already be logged in at this point)
 protected void NewQnrButton_Click(object sender, EventArgs e)
                MembershipUser membershipUser = Membership.GetUser();
                if(null != membershipUser)
                    int userId;
                    if(int.TryParse(membershipUser.ProviderUserKey.ToString(), out userId))
                        int testRet = sendData.InsertQuestionnaire(QuestionnaireName.Text, userId, Int32.Parse(NumberOfQuest.Text));
                        Session["QuestionnaireID"] = testRet;
                        Session["QuestionnaireName"] = QuestionnaireName.Text;

            } // End NewQNRButton_Click

Open in new window

This should allow a user to create a questionnaire which will be owned by themselves. However I am having trouble getting the userId.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.