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

Hello,

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:
UserID
UserName
FirstName
LastName
Password
EmailAddress

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.

Thanks
idny99Asked:
Who is Participating?
 
Cenjoy100Commented:
Ohh I got you..
I guess following links may help you.

http://social.technet.microsoft.com/Forums/en/sharepoint2010programming/thread/f58884bd-4a85-4438-9408-8907c1505638

Or
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.

Ref. http://msdn.microsoft.com/en-us/library/40w5063z.aspx
http://msdn.microsoft.com/en-us/library/system.web.security.membership.getuser.aspx

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

<authentication mode="Windows"/>

Thanks.
0
 
Cenjoy100Commented:
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.
0
 
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 ASP.net?
Thanks
0
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.

 
dave4dlCommented:
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 http://weblogs.asp.net/scottgu/archive/2006/07/12/Recipe_3A00_-Enabling-Windows-Authentication-within-an-Intranet-ASP.NET-Web-application.aspx
0
 
Cenjoy100Commented:

Create a session variable (This can be done using C# only)
e.g
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.

:)

0
 
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.
0
 
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)
new_questionnaire.aspx
 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;
                        Response.Redirect("~/buildq/add_questions.aspx");
                    }

                }
                else
                {
                    Response.Redirect("~/login/login.aspx");
                }
              
            } // 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.
0
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.