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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.


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.
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"/>


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.