Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do I get UserID within application

Posted on 2006-06-07
14
Medium Priority
?
307 Views
Last Modified: 2012-08-13
Hi there,

I have a simple user feedback form on which there are two submission boxes txtName and txtComments. Included in the execution code for the Submit button is the following code:

SqlCommand1.Parameters("@UserID").Value = User.Identity.Name

But when the button is clicked/code is run, nothing is inserted into the table. How do I get the logged on UserID? Preferably with the domain prfeix.

Cheers,

LoveToSpod

Here's the full code for the button click event:

---------------------------------
--------------CODE------------
---------------------------------

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        SqlCommand1.Parameters("@Name").Value = txtName.Text
        SqlCommand1.Parameters("@Comment").Value = txtComment.Text
        SqlCommand1.Parameters("@DateTime").Value = Now()
        SqlCommand1.Parameters("@UserID").Value = User.Identity.Name

        SqlConnection1.Open()
        SqlCommand1.ExecuteNonQuery()
        SqlConnection1.Close()


    End Sub
0
Comment
Question by:LoveToSpod
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 16

Expert Comment

by:Edwin_C
ID: 16850222
Any error is thrown?  If your SqlCommand1 is a simple INSERT statement, it should insert new record in DB even though the parameter @UserID equals "" or nothing.  So if you have "nothing is inserted into the table", you better check the execution of the SqlCommand again.  It is probably not related to the value of User.Identity.Name.
0
 
LVL 27

Expert Comment

by:Sammy
ID: 16850244
what kind of authentication are you using?
try something like this
Dim strCurrUser As String = HttpContext.Current.User.Identity.Name

HTH
0
 

Author Comment

by:LoveToSpod
ID: 16850648
Hi there.
Ans1: My question was ambiguous (sorry), nothing inserted into the table meaning: A record was inserted, but no UserID. (Need to work on my English)  ;)

Ans2: There is no authentication as such, the user is simply accessing this site through the intranet. All users are logged on to a domain with a userID. It was this windows UserID I would like to get a hold of. (As a side comment, in Reporting Services the statement '=User!UserID' works, and this simply draws the UserID from the windows authentication.) I included your code above like:

---------------------------------
--------------CODE------------
---------------------------------
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strCurrUser As String = HttpContext.Current.User.Identity.Name

        SqlCommand1.Parameters("@Name").Value = txtName.Text
        SqlCommand1.Parameters("@Comment").Value = txtComment.Text
        SqlCommand1.Parameters("@DateTime").Value = Now()
        SqlCommand1.Parameters("@UserID").Value = strCurrUser

        SqlConnection1.Open()
        SqlCommand1.ExecuteNonQuery()
        SqlConnection1.Close()
        Response.Redirect("http://tntu082154.gb.tntpost.com/technicalCourier/FeedbackAck.htm")

---------------------------------
----------END-CODE----------
---------------------------------

But the UserID is still not inserted into the database.


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:sroughley
ID: 16850836
Unless you are using impersonation I do not think that is possible without disabling anonymous access to the website and forcing users to login. If you are using impersonation or users are being authenticated at the website then you can use Context.User.Identity.Name.
0
 

Expert Comment

by:sroughley
ID: 16850844
Sorry, you might also want to look at: System.Security.Principal.WindowsIdentity.GetCurrent()

This will open up various properties that could help you here.
0
 
LVL 27

Expert Comment

by:Sammy
ID: 16854501
Your web.config should have
<configuration>
  <system.web>
    <authentication mode="Windows" />
    <identity impersonate="true" />
  </system.web>
</configuration>
and you should turn off "uncheck" Anonymous login in IIS for your website, then your code should work

HTH
0
 

Author Comment

by:LoveToSpod
ID: 16960418
This worked for me in the end with no problems:

        Dim strUserID As String
        strUserID = User.Identity.Name

-- It's part of the System.Web.Security.FormsAuthentication method
0
 
LVL 27

Expert Comment

by:Sammy
ID: 16994358
Isnt that the first solution I provided you?
Dim strCurrUser As String = HttpContext.Current.User.Identity.Name

Sammy
0
 
LVL 27

Expert Comment

by:Sammy
ID: 16994372
even Edwin provided you with the same solution without declaring the variable
0
 

Author Comment

by:LoveToSpod
ID: 17029321
Sorry for the delay, I've been away for the last four days.

This question eminated from following an example in a book, and I expected the solution to work using the method as per above. It suggested using the following webconfig file

<configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>

So naturally when experts are suggesting using windows authentication and impersonation etc, I was a lost! I did however try these solutions, but they ended up becoming more complicated than was required. As with most things, with a lot of fiddling/trial and error,

I replaced this:
SqlCommand1.Parameters("@UserID").Value = User.Identity.Name

with:
        Dim strUserID As String
        strUserID = User.Identity.Name

And it works!

Normally if a question does not perfectly answer my question, it will help considerably and I will say so in a comment, and reward the points. But in this case, the comments at the time didn't help me resolve the problem. Sorry if this offends/annoys anyone, NOT intended :)

Sammy, At the time I tried your line of code:

Dim strCurrUser As String = HttpContext.Current.User.Identity.Name

in the project, and it still didn't work!! - hence my response - Date: 06/07/2006 10:03AM GMT

Best intentions,

LoveToSpod


0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17047142
Closed, 500 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question