我试图将 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:我们不应该做太多的代码更改。以下是问题。
我也试过下面的代码(它也不能正常工作)
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 门户网站中,有人提到它对他也不起作用,并告诉我向微软提出一个错误。但不确定它是一个错误还是我的方法是错误的......