Trying to get LOGON_USER into a text field

Morning,

I ma sure this is easy but asp.net is new for me. Anyway I’m trying to collect a username and put it into a SQL database. I collected the username just fine:



<%  Dim strLogonUsr As String
    Dim iPos As String
    strLogonUsr = RTrim(Request.ServerVariables("LOGON_USER"))
    iPos = Len(strLogonUsr) - InStr(1, strLogonUsr, "\\", 1)
    strLogonUsr = Right(strLogonUsr, iPos)
 %>

<p>Logon_User: <%Response.Write(Request.ServerVariables("LOGON_USER"))%></p>
<p>UserName: <%Response.Write(strLogonUsr)%></p>

So then I put it in the parameters:


<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:InventoryConnectionString %>" 
            DeleteCommand="DELETE FROM [tbl_Inventory_Data] WHERE [Inv_Key] = @Inv_Key" 
            InsertCommand="INSERT INTO [tbl_Inventory_Data] ([Location], [PartNumber], [Quantity], [userName]) VALUES (@Location, @PartNumber, @Quantity, @strLogonUsr)" 
            SelectCommand="SELECT [Inv_Key], [Location], [PartNumber], [Quantity], [userName] FROM [tbl_Inventory_Data]"             
            UpdateCommand="UPDATE [tbl_Inventory_Data] SET [Location] = @Location, [PartNumber] = @PartNumber, [Quantity] = @Quantity, [userName] = @strLogonUsr WHERE [Inv_Key] = @Inv_Key">
    
     
            <DeleteParameters>
                <asp:Parameter Name="Inv_Key" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Location" Type="String" />
                <asp:Parameter Name="PartNumber" Type="String" />
                <asp:Parameter Name="Quantity" Type="Double" />
                <asp:Parameter Name="strLogonUsr" Type="String" /> 
                <asp:Parameter Name="Inv_Key" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="Location" Type="String" />
                <asp:Parameter Name="PartNumber" Type="String" />
                <asp:Parameter Name="Quantity" Type="Double" />
                <asp:Parameter Name="strLogonUsr" Type="String"/> 
            </InsertParameters>
        </asp:SqlDataSource>

Then I tried numerous ways of trying to put it into a text box with no luck:


<asp:TextBox ID="userNameTextBox" runat="server" 
                             Text=<%Response.Write(Request.ServerVariables("LOGON_USER"))%> />

Can someone tell me what goes in the text box? And if I have the variable set right?

Thanks for any help.

Laura

That is a very strange way to get the logged in user. In C# I use: User.Identity.Name.

That said, if you have a runat server control, you cannot insert the text like this and need to do it from the code behind:

userNameTextBox.Text = Request.ServerVariables(“LOGON_USER”).ToString()

I hope this helps, as I am not 100% sure on VB

Thanks for that, but I tried putting that in the page load like so:


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load userNameTextBox.Text = Request.ServerVariables("LOGON_USER").ToString()
End Sub

and put the textbox like this:


<td>
<asp:TextBox ID="userNameTextBox" runat="server" Text=' ' />
</td>

and nothing shows up. I am guessing I still don’t understand.

Thanks though.

Laura

Problem is you are looking in the wrong place. If you are using windows authentication here, you should be looking at the http context’s User property, not in the Http server variables.

Like I said I am a total beginner at asp.net so I’m afraid I don’t know what you mean. How do I take what you said and either attach it to a variable as a default value or put it in the text box? I was all excited I actually got it to load let alone getting all tricky :slight_smile:

Thanks

Laura


userNameTextBox.Text = Context.User.Identity.Name

Thanks for that. I put the code you sent in my page load event like this:


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        userNameTextBox.Text = Context.User.Identity.Name
    End Sub

And then I went to the text box and changed it this:

<asp:TextBox ID="userNameTextBox" runat="server" 
                            Text=' ' />

and I still dont get anything in the text box.

Thanks

Laura

How are people logging into the site?

Windows authentication is set up on the IIS server for the website.

Is anonymous access turned off?

Yes.