Search code examples
asp.netvb.netactive-directorywindows-authenticationactive-directory-group

ASP.NET VB.NET - How to populate Active Directory Logged in Current User's Name into Text Box Field


Background: I have a webForm app that registers a user in the database based on the information provided with a web service, auto-generates a random password and username, and e-mails the user a link to take an application based on the marketing company selected.

Question:

  • How do I populate the MarketerName_TextBox field with the currently logged in users name (Is it User.Identity.Name and do I add that line on aspx.vb end or the aspx end?)

Here's a screenshot of the front end: web app screenshot

I've been going off the code from Wrox's Windows Authentication Tutorial but it's not thorough enough for what I'm trying to do.

web.config file:

web.config file (pertinent code displayed only):

 <authentication mode="Windows"/>
   <authorization>

    <allow users="alg\bmccarthy, alg\phoward" />               
    <allow roles="alg\ACOMP_user_Admin" />
    <allow roles="alg\ACOMP_user_AMG" />
    <allow roles="alg\ACOMP_user_BIG" />
    <allow roles="alg\ACOMP_user_NIS" />
    <allow roles="alg\ACOMP_user_GLA" />
    <allow roles="alg\ACOMP_user_PIP" />
    <allow roles="alg\ACOMP_user_PSM" />
    <allow roles="alg\ACOMP_user_PAM" />
    <allow roles="alg\ACOMP_user_ANN" />
    <allow roles="alg\ACOMP_user_AAM" />
    <allow roles="alg\ACOMP_user_MWM" /> 
    <allow roles="alg\ACOMP_user_GIM" />
    <deny users="*" />        
</authorization> 

   <bindings>
   <basicHttpBinding>
    <binding name="BasicHttpBinding_IAcompService" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
          <message clientCredentialType="UserName" algorithmSuite="Default" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
  <client>
  <endpoint address="http://172.17.1.40/aCompService.svc" binding="basicHttpBinding"
    bindingConfiguration="BasicHttpBinding_IAcompService" contract="aComp_ServiceReference.IAcompService"
    name="BasicHttpBinding_IAcompService" />
  </client>
 </system.serviceModel>

default.aspx.vb code w/ the txtMarketerName_TextChanged() and Page_Load() Methods:

Private Sub GetCarriers()
    Dim ac1 As Array
    ac1 = proxy.GetCarrierNames("test", "test")
    For Each item In ac1
        lbCarriers.Items.Add(String.Format("{0} | {1} | {2}", item.CarrierID, item.CarrierNameLong, item.CarrierNameShort))
    Next
End Sub

Private Sub txtMarketerName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMarketerName.TextChanged

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load, Me.Load, Me.Load

    If Not lbCarriers.Items.Count > 0 Then
        GetCarriers()
        GetMarketingCompanies()
    End If

End Sub

default.aspx code where the text box field for the Marketer name is displayed:

<table id="Table1" border="0" cellpadding="6" cellspacing="0" align=center>
    <tr>
        <td class="style1">
            My Name (auto-populated Current Logged In User's Name): </td>
        <td bgcolor="#ffffff" class="style6">
            <asp:TextBox ID="txtMarketerName" runat="server" Width="250px">    
        </asp:TextBox>
        </td>
        <td bgcolor="#ffffff" class="style2">
            <asp:RequiredFieldValidator ID="regValMarketerName" runat="server"
ControlToValidate="txtMarketerName" ErrorMessage="Marketer Name is required" Text="*"
ValidationGroup="Valtxt">
        </asp:RequiredFieldValidator>
        </td>
    </tr>

Thanks for looking!

If you have any helpful links or suggestions, I'll give you an up-vote!


Solution

  • You can set the value of a label or textbox to the current windows user's username in the code behind in the Page_Load evemt like so:

    txtUsername.Text = User.Identity.Name;
    

    Or you can do it in the mark up like so:

    <asp:Label runat="server" ID="lblUsername"><%=User.Identity.Name %></asp:Label>