-1

我试图将 vb.net 应用程序从 .net 框架 2.0(Visual Studio 2008)迁移到 .net 框架 4.7.0(Visual Studio 2017)。这里的问题是,IsInRole检查当前用户是否是 Visual Studio 2008 中角色的一部分的方法工作正常。但是当我在 Visual Studio 2015 或 2017 中打开相同的代码时,它False会作为结果返回。问题不仅在于IsInRole,还在于它My.User.Name在最新的 Visual Studio 中返回 null 。有人可以建议解决这个问题的方法吗?PS:我们不应该做太多的代码更改。以下是问题。

VS 2008回归True 在此处输入图像描述

VS 2017回归False 在此处输入图像描述

我也试过下面的代码(它也不能正常工作)

Imports System.Security.Principal

Class PrincipalCheck
    Shared Function UserInRole(role As String) As Boolean
        Dim currPrincipal As New WindowsPrincipal(New WindowsIdentity(Environment.UserName))
        Return currPrincipal.IsInRole(role)
    End Function
End Class

Public Sub StartCheck()
    MsgBox(PrincipalCheck.UserInRole("MyDomain\MyGroup"))
End Sub

在一个 MSDN 门户网站中,有人提到它对他也不起作用,并告诉我向微软提出一个错误。但不确定它是一个错误还是我的方法是错误的......

4

2 回答 2

0

尝试My.User.InitializeWithWindowsUser()在应用程序启动时调用。

于 2018-03-26T08:32:58.630 回答
0
  • 检查角色是否存在

来自 MSDN 的一些信息:

My.User 对象的确切行为取决于应用程序的类型和运行应用程序的操作系统。有关详细信息,请参阅用户类概述。

My.User.IsInRole 也仅适用于 Windows 窗体。

于 2018-03-26T11:31:30.383 回答