My.user.IsInRole always returning false

前端 未结 1 1394
心在旅途
心在旅途 2021-01-26 17:40

Currently, I\'m working in one migration request, where we need to change the framework from 3.5 to 4.6.2. Here the problem is after changing the framework below method is not s

相关标签:
1条回答
  • 2021-01-26 18:00

    I suspect that the problem may be related to the use of Environment.UserName. Try replacing that with WindowsIdentity.GetCurrent(), thus:

    Shared Function UserInRole(role As String) As Boolean
        Dim currPrincipal As New WindowsPrincipal(WindowsIdentity.GetCurrent())
        Return currPrincipal.IsInRole(role)
    End Function
    

    However, it is worth remembering that User Account Control can get in the way. If you aren't running elevated then the above won't work for the WindowsBuiltInRole.Administrator, possibly others. So that might be an issue as well. So worth seeing if you get different results when you run elevated.

    0 讨论(0)
提交回复
热议问题