Link to home
Start Free TrialLog in
Avatar of LoveToSpod
LoveToSpodFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I get UserID within application

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
Avatar of Edwin_C
Edwin_C
Flag of Hong Kong image

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.
what kind of authentication are you using?
try something like this
Dim strCurrUser As String = HttpContext.Current.User.Identity.Name

HTH
Avatar of LoveToSpod

ASKER

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.


Avatar of sroughley
sroughley

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.
Sorry, you might also want to look at: System.Security.Principal.WindowsIdentity.GetCurrent()

This will open up various properties that could help you here.
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
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
Isnt that the first solution I provided you?
Dim strCurrUser As String = HttpContext.Current.User.Identity.Name

Sammy
even Edwin provided you with the same solution without declaring the variable
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


ASKER CERTIFIED SOLUTION
Avatar of GhostMod
GhostMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial