locked
Show Domain User Name on Web Page RRS feed

  • Question

  • User-269639712 posted

    I've searched and can't find anything about displaying domain user name on a webpage - just on authentication.  My web page has a field that when the form loads, I want it to show the name of the user logged onto that computer.  I does this by looking at the domain user name logged onto that computer, and getting the name from a SQL table that equals that domain name.  It works when I display in in Visual Studio 2010, but after I publish it (IIS), the name is blank.  Here is my vb code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            'Dim windowsIdentity As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent
            'Dim windowsLoginName As System.String = windowsIdentity.Name
            Dim windowsLoginName As System.String = System.Security.Principal.WindowsIdentity.GetCurrent().Name
    
            Dim split As String() = Nothing
            Dim vname As String
    
            Dim sqlConnection As New SqlConnection("Data Source=janetdev;Initial Catalog=TimeSQL;Persist Security Info=True;User ID=sa;Password=password")
            Dim cmd, cmd1, cmd2 As New SqlCommand
            Dim returnValue, returnValue1, returnValue2 As Object
            Dim dt As Date = Today
    
            'Get network login name (name only)
            split = windowsLoginName.Split("\\".ToCharArray())
            vname = split(1)
    
            'Get firstname from table that matches login name
            cmd.CommandText = "SELECT FirstName FROM tblEmployees where login = '" & vname & "'"
            cmd.CommandType = CommandType.Text
            cmd.Connection = sqlConnection
    
            'Get lastname from table that matches login name
            cmd1.CommandText = "SELECT LastName FROM tblEmployees where login = '" & vname & "'"
            cmd1.CommandType = CommandType.Text
            cmd1.Connection = sqlConnection
    
            'Get employeeid from table that matches login name 
            cmd2.CommandText = "SELECT EmployeeID FROM tblEmployees where login = '" & vname & "'"
            cmd2.CommandType = CommandType.Text
            cmd2.Connection = sqlConnection
    
            sqlConnection.Open()
    
            'firstname
            returnValue = cmd.ExecuteScalar()
    
            'lastname
            returnValue1 = cmd1.ExecuteScalar()
    
            'employeeid
            returnValue2 = cmd2.ExecuteScalar()
    
            sqlConnection.Close()
    
            'display firstname and lastname on screen
            TextBox3.Text = returnValue & " " & returnValue1
            TextBox4.Text = returnValue2
    
            'display today's date
            DateRequested.Text = dt
    
            DropDownList3.Enabled = False
            DropDownList2.Enabled = False
            CheckBox1.Enabled = False
    
            'go to date requested as first field
            DateRequested.Focus()
    
        End Sub

    Thanks in advance for any help you can give me.

    Friday, June 27, 2014 11:07 AM

Answers

  • User-718146471 posted

    Check on your IIS to ensure that it is windows authentication turned on, otherwise it may not see the domain username.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 27, 2014 11:16 AM
  • User1728944201 posted

    dim  sUserName as string = HttpContext.Current.User.Identity.Name

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 27, 2014 11:17 AM
  • User-269639712 posted

    Thank you, thank you, thank you.  Not sure which one worked, but it worked!  I did have to change my web.config from:

      <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
      </authentication>

    to

    <authentication mode="Windows"/>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 27, 2014 2:20 PM

All replies

  • User-718146471 posted

    Check on your IIS to ensure that it is windows authentication turned on, otherwise it may not see the domain username.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 27, 2014 11:16 AM
  • User1728944201 posted

    dim  sUserName as string = HttpContext.Current.User.Identity.Name

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 27, 2014 11:17 AM
  • User-269639712 posted

    Thank you, thank you, thank you.  Not sure which one worked, but it worked!  I did have to change my web.config from:

      <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
      </authentication>

    to

    <authentication mode="Windows"/>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 27, 2014 2:20 PM