ASP.NET VB.NET-如何将登录到当前用户名的Active Directory填充到文本框字段中

问题描述

|| 背景:我有一个webForm应用程序,该应用程序根据Web服务提供的信息在数据库注册用户自动生成随机密码和用户名,并通过电子邮件用户发送链接以根据营销公司获取应用程序已选择。 题: 如何用当前登录用户名填充MarketerName_TextBox字段(是否为User.Identity.Name,并在aspx.vb端或aspx端添加该行?) 这是前端的屏幕截图: 我已经关闭了Wrox的Windows身份验证教程中的代码,但是对于我想做的事情还不够彻底。 web.config文件: web.config文件(仅显示相关代码):
 <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>
带txtMarketerName_TextChanged()和Page_Load()方法的default.aspx.vb代码
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代码
<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>
感谢您的光临! 如果您有任何有用的链接或建议,我将给您投票!     

解决方法

您可以在Page_Load evemt后面的代码中将标签或文本框的值设置为当前Windows用户的用户名,如下所示:
txtUsername.Text = User.Identity.Name;
或者,您可以像这样在标记中进行操作:
<asp:Label runat=\"server\" ID=\"lblUsername\"><%=User.Identity.Name %></asp:Label>
    ,这是我最终在deafult.aspx.vb代码隐藏页下使用的内容:
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
        txtMarketerName.Text = WindowsIdentity.GetCurrent.Name
        GetCarriers()
        GetMarketingCompanies()
    End If

End Sub
WindowsIdentity.GetCurrent()。Name与User.Identity.Name WindowsIdentity.GetCurrent()方法返回一个WindowsIdentity实例,该实例表示运行线程的身份。 User.Identity对象表示从IIS传递的身份。如果IIS允许用户匿名访问页面,则User.Identity.Name属性将返回一个空字符串。否则,它将返回由IIS验证的用户的帐户名。 User.Identity.Name-返回:my_domain \\ jdoe System.Environment.UserName返回:jdoe