LoveToSpod
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("@U serID").Va lue = 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("@N ame").Valu e = txtName.Text
SqlCommand1.Parameters("@C omment").V alue = txtComment.Text
SqlCommand1.Parameters("@D ateTime"). Value = Now()
SqlCommand1.Parameters("@U serID").Va lue = User.Identity.Name
SqlConnection1.Open()
SqlCommand1.ExecuteNonQuer y()
SqlConnection1.Close()
End Sub
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("@U
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("@N
SqlCommand1.Parameters("@C
SqlCommand1.Parameters("@D
SqlCommand1.Parameters("@U
SqlConnection1.Open()
SqlCommand1.ExecuteNonQuer
SqlConnection1.Close()
End Sub
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.I dentity.Na me
HTH
try something like this
Dim strCurrUser As String = HttpContext.Current.User.I
HTH
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.I dentity.Na me
SqlCommand1.Parameters("@N ame").Valu e = txtName.Text
SqlCommand1.Parameters("@C omment").V alue = txtComment.Text
SqlCommand1.Parameters("@D ateTime"). Value = Now()
SqlCommand1.Parameters("@U serID").Va lue = strCurrUser
SqlConnection1.Open()
SqlCommand1.ExecuteNonQuer y()
SqlConnection1.Close()
Response.Redirect("http://tntu082154.gb.tntpost.com/technicalCourier/FeedbackAck.htm")
-------------------------- -------
----------END-CODE-------- --
-------------------------- -------
But the UserID is still not inserted into the database.
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.I
SqlCommand1.Parameters("@N
SqlCommand1.Parameters("@C
SqlCommand1.Parameters("@D
SqlCommand1.Parameters("@U
SqlConnection1.Open()
SqlCommand1.ExecuteNonQuer
SqlConnection1.Close()
Response.Redirect("http://tntu082154.gb.tntpost.com/technicalCourier/FeedbackAck.htm")
--------------------------
----------END-CODE--------
--------------------------
But the UserID is still not inserted into the database.
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. WindowsIde ntity.GetC urrent()
This will open up various properties that could help you here.
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
<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
ASKER
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.FormsA uthenticat ion method
Dim strUserID As String
strUserID = User.Identity.Name
-- It's part of the System.Web.Security.FormsA
Isnt that the first solution I provided you?
Dim strCurrUser As String = HttpContext.Current.User.I dentity.Na me
Sammy
Dim strCurrUser As String = HttpContext.Current.User.I
Sammy
even Edwin provided you with the same solution without declaring the variable
ASKER
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("@U serID").Va lue = 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.I dentity.Na me
in the project, and it still didn't work!! - hence my response - Date: 06/07/2006 10:03AM GMT
Best intentions,
LoveToSpod
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("@U
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.I
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.