2011-02-17 59 views
2

我有一個使用Windows身份驗證的.Net 4網絡應用程序,所有的工作都很好,但是有一個微不足道的錯誤讓我瘋狂!從vb.net的LoggedInTemplate中剝離域名

Visual Studio中拼下顯示誰在

<LoggedInTemplate> 
    Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>! 
    [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="http://somewhere/default.aspx"/> ] 
</LoggedInTemplate> 

的問題是,雖然,這是顯示爲<domain>/user而不僅僅是這我假設它是從

拉動該值的用戶名登錄
System.Threading.Thread.CurrentPrincipal.Identity.Name 

但是我無法用編程方式來修改這個?

回答

1

一個簡單的解決方法是在模板中的登錄名控件位置使用標籤。您可以從User.Identity.Name中剝離域,並將剝離的版本放入標籤中。我也有這個問題,我用標籤(至少這是我記得)

+0

我喜歡Rob的實現/答案,但對於簡單的應用程序,這是我採取了你的建議。謝謝 – Dean 2011-02-28 09:48:02

0

自定義輸出的唯一內置方法是LoginName.FormatString屬性,不幸的是,所有這些都允許您提供用戶名嵌入的格式字符串,例如「Welcome,DOMAIN \ NAME」,而不僅僅是「DOMAIN \名稱」。

我能看到的唯一選擇是重新實現LoginName控件,無論是批發還是繼承LoginName並重新實​​現RenderContents方法以適當地設置用戶名。從反射器,對於該當前代碼(.NET 2.0)是:

Protected Friend Overrides Sub RenderContents(ByVal writer As HtmlTextWriter) 
    Dim userName As String = Me.UserName 
    If Not String.IsNullOrEmpty(userName) Then 
     userName = HttpUtility.HtmlEncode(userName) 
     Dim formatString As String = Me.FormatString 
     If (formatString.Length = 0) Then 
      writer.Write(userName) 
     Else 
      Try 
       writer.Write(String.Format(CultureInfo.CurrentCulture, formatString, New Object() { userName })) 
      Catch exception As FormatException 
       Throw New FormatException(SR.GetString("LoginName_InvalidFormatString"), exception) 
      End Try 
     End If 
    End If 
End Sub 

UserName是LoginName將類,經由另一個稱爲LoginUtil內部類從HttpContext.Current.User獲取用戶名的Friend ReadOnly屬性。