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 showing result as expected. My.User.IsInRole() is always returning false.
If My.User.IsInRole(nlRole.InnerText) Then
hasRole = True
Exit For
End If
Also, I tested with below code:
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
But no luck. Can someone please help me with the above issue.
If you expect some more input from my end, please let me know.
I suspect that the problem may be related to the use of
Environment.UserName. Try replacing that withWindowsIdentity.GetCurrent(), thus: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.