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

时间:2011-04-15 21:07:04

标签: asp.net vb.net active-directory windows-authentication active-directory-group

背景:我有一个webForm应用程序,它根据Web服务提供的信息在数据库中注册用户,自动生成随机密码和用户名,并通过电子邮件向用户发送基于以下内容的链接以获取应用程序营销公司选择了。

问题:

  • 如何使用当前登录的用户名填充MarketerName_TextBox字段(是否为User.Identity.Name,我是否在aspx.vb end或aspx端添加该行?)

这是前端的截图: web app screenshot

我一直在关注Wrox's Windows Authentication Tutorial的代码,但对于我正在尝试做的事情来说还不够彻底。

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>

default.aspx.vb代码w / txtMarketerName_TextChanged()和Page_Load()方法:

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>

感谢您的期待!

如果您有任何有用的链接或建议,我会给您一个投票!

2 个答案:

答案 0 :(得分:1)

你可以在Page_Load evemt的后面代码中将标签或文本框的值设置为当前windows用户的用户名,如下所示:

txtUsername.Text = User.Identity.Name;

或者您可以在标记中执行此操作:

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

答案 1 :(得分:0)

这是我在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()。名称与User.Identity.Name

WindowsIdentity.GetCurrent()方法返回一个WindowsIdentity实例,该实例表示运行该线程的标识。 User.Identity对象表示从IIS传递的标识。如果IIS允许用户匿名访问页面,则User.Identity.Name属性将返回空字符串。否则,它将返回IIS验证的用户的帐户名。

User.Identity.Name - 返回:my_domain \ jdoe

System.Environment.UserName返回:jdoe