My.User.CurrentPrincipal在类库中不起作用

时间:2014-12-01 15:48:59

标签: vb.net visual-studio-2012

我正在尝试在使用Windows身份验证的Windows环境中获取当前用户名。代码存在于一个类库中,该库在单独的Visual Studio应用程序中构建和引用:

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

由于

扎克

1 个答案:

答案 0 :(得分:0)

在MS文档中,它说

  

对于Windows应用程序,默认情况下,只有基于Windows应用程序模板构建的项目才会初始化My.User对象。在所有其他Windows项目类型中,您必须通过显式调用My.User方法或将值赋给My.User.InitializeWithWindowsUser来初始化CurrentPrincipal对象。

代码中的修复是:

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