2014-12-01 24 views
0

我想在使用Windows身份驗證的Windows環境中獲取當前用戶名。存在即是建立和引用一個單獨的Visual Studio應用程序中的類庫代碼:My.User.CurrentPrincipal不能在類庫中工作

Function GetUserName() As String 
    If TypeOf My.User.CurrentPrincipal Is 
     Security.Principal.WindowsPrincipal Then 
     ' The application is using Windows authentication. 
     ' The name format is DOMAIN\USERNAME. 
     Dim parts() As String = Split(My.User.Name, "\") 
     Dim username As String = parts(1) 
     Return username 
    Else 
     ' The application is using custom authentication. 
     Return My.User.Name 
    End If 
End Function 

我得到一個錯誤,當它位於類庫。 My.User.CurrentPrincipal返回{System.Security.Principal.GenericPrincipal}並且My.User.Name爲空。當我將完全相同的代碼放入一個全新的Windows窗體應用程序時,它的工作原理是 - My.User.CurrentPrincipal返回{System.Security.Principal.WindowsPrincipal},My.User.Name是用戶的登錄名。

Microsoft文檔建議My.User對象將在類庫中工作。有人知道爲什麼當我將它放入類庫並將其添加爲父應用程序的.dll引用時,我會得到不同的值?

父應用程序是一個類庫,它是Microsoft PowerPoint的加載項。在調用上述代碼(稱爲UsageDataCollection.dll)父應用程序中的代碼是:

Public Class rbnOvaPowerPoint 
    Private DataCollector As UsageDataCollection.DataCollector 
    Private Sub butShare_Click(sender As Object, e As RibbonControlEventArgs) Handles butShare.Click 
     OtherTasks.CreateMailItem() 
    End Sub 
End Class 

然後在一個獨立的模塊:

Module OtherTasks 
    Private DataCollector As New UsageDataCollection.DataCollector 
    Sub CreateMailItem() 
     Dim OutlookApp As Outlook._Application = CreateObject("Outlook.Application") 
     Dim mail As Outlook.MailItem = Nothing 
     Dim mailRecipients As Outlook.Recipients = Nothing 
     Dim mailRecipient As Outlook.Recipient = Nothing 
     DataCollector.UsageStatistics("CreateMailItem") 
     Try 
      mail = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem) 
      mail.Subject = "OvaPowerPoint" 
      mail.Body = "Check out OvaPowerPoint, a custom-built Arup add-in for PowerPoint!" & Strings.Chr(13) & Strings.Chr(13) & "About the Add-In:" & Strings.Chr(13) & "http://wiki.oasys.intranet.arup.com/X-Wiki/index.php/OvaPowerPoint" & Strings.Chr(13) & Strings.Chr(13) & "Installation File:" & Strings.Chr(13) & "\\n-ynas12\Software\Custom%20Applications\Plug-Ins\Microsoft%20PowerPoint\OvaPowerPoint\setup.exe" 
      mail.Display(True) 
     Catch ex As Exception 
      System.Windows.Forms.MessageBox.Show(ex.Message, 
      "An exception is occured in the code of add-in.") 
     Finally 
      If Not IsNothing(mailRecipient) Then System.Runtime.InteropServices.Marshal.ReleaseComObject(mailRecipient) 
      If Not IsNothing(mailRecipients) Then System.Runtime.InteropServices.Marshal.ReleaseComObject(mailRecipients) 
      If Not IsNothing(mail) Then System.Runtime.InteropServices.Marshal.ReleaseComObject(mail) 
     End Try 
    End Sub 
End Module 

而在UsageDataCollection.dll的UsageStatistics子程序看起來像:

Imports System.IO 
Imports System.Text 
    Public Class DataCollector 
    Public Sub UsageStatistics(myAction As String) 
     Dim myAssemblyName As String = System.Reflection.Assembly.GetCallingAssembly.GetName.Name 
     Dim myFilePath As String = "\\n-ywpress01\uploads\UsageData\" & myAssemblyName & ".csv" 
     Using LogFile As New StreamWriter(myFilePath, True) 
      LogFile.WriteLine("[" & DateTime.Now.ToUniversalTime.ToString("yyyy/MM/dd HH':'mm':'ss") & "]" & Chr(44) & GetUserName() & Chr(44) & GetUserLocation() & Chr(44) & myAction) 
      LogFile.Close() 
     End Using 
    End Sub 
End Class 

感謝

扎克

+0

從哪裏被調用這個函數呢? – 2014-12-01 15:57:17

+0

嗨克里斯,現在它是從類庫中調用的,它是Microsoft PowerPoint的加載項。我會編輯我的帖子,以包括一些細節。 – Zak 2014-12-01 16:04:42

+0

我一直在運行一些測試,發現UsageDataCollection.dll的作品,如果它被Windows窗體應用程序引用。當我嘗試在PowerPoint插件(它採用類庫的形式)引用它時發生什麼...... – Zak 2014-12-01 18:46:10

回答

0

在MS文檔,它說

對於Windows應用程序,只能建立在Windows應用程序模板項目初始化默認My.User對象。在所有其他Windows項目類型中,您必須通過明確調用My.User.InitializeWithWindowsUser方法或將值賦予CurrentPrincipal來初始化My.User對象。

在你的代碼的解決方法是:

Function GetUserName() As String 
    My.User.InitializeWithWindowsUser() 'pulls the network credentials into .NET 
    If TypeOf My.User.CurrentPrincipal Is 
     Security.Principal.WindowsPrincipal Then 
     ' The application is using Windows authentication. 
     ' The name format is DOMAIN\USERNAME. 
     Dim parts() As String = Split(My.User.Name, "\") 
     Dim username As String = parts(1) 
     Return username 
    Else 
     ' The application is using custom authentication. 
     Return My.User.Name 
    End If 
End Function