0

背景:我有一個webForm應用程序,它根據Web服務提供的信息在數據庫中註冊用戶,生成一個隨機密碼和用戶名,並通過電子郵件向用戶發送一個鏈接,以根據所選擇的營銷公司提交申請。ASP.NET VB.NET - 如何將登錄到當前用戶名的Active Directory填充到文本框中字段

問:

  • 如何填充MarketerName_TextBox場與用戶名(是User.Identity.Name當前登錄並添加我在aspx.vb年底該行或ASPX結束? )

這裏的前端的截圖: web app screenshot

我已經從Wrox's Windows Authentication Tutorial都會響起的代碼,但它是不夠徹底的什麼,我想d O操作。

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代碼瓦特/ 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> 

感謝您的期待!

如果您有任何有用的鏈接或建議,我會給你一個投票!

+1

你在標題提及並使用Windows身份驗證的問題,而是你的web.config使用FormsAuthentication供應商。此網站是否適用於Intranet?你能顯示整個web.config嗎? – 2011-04-18 15:21:18

+0

@Zach,感謝您的回覆!我只是在那裏添加了web.config的其餘部分。我使用ASP.NET v VB.NET VS2010模板編寫了此項目,因此我可能需要對webconfig進行一些額外的更改才能使勝利獲得認證。我應該改變什麼? – 2011-04-18 15:55:07

+0

因此,您的意圖是讓網站成爲您的域中擁有Windows帳戶的用戶訪問的INTRANET網站? – 2011-04-18 16:04:55

回答

1

您可以在Page_Load後面設置一個標籤或文本框,以當前的Windows用戶的用戶名的值在代碼evemt像這樣:

txtUsername.Text = User.Identity.Name; 

或者您也可以在標記像這樣做:

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

感謝您的回覆!如果你有機會,請查看我的其他活動目錄會員相關的問題在這裏:http://stackoverflow.com/questions/5731040/ – 2011-04-20 14:16:12

+0

我看到有時使用windowsIdentity.Name。那和User.Identity.Name有什麼區別? – 2011-04-20 15:22:08

+0

WindowsIdentity.GetCurrent.Name和User.Identity.Name之間有什麼區別? – 2011-04-20 18:16:02

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()。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

相關問題