Sanath Pabba
Sanath Pabba

Reputation: 1

My.User.IsInRole is returning false

I was trying to migrate a vb.net application from .net framework 2.0(Visual studio 2008) to .net framework 4.7.0 (Visual studio 2017). Here the problem is, IsInRole method is working fine to check whether the current user is part of roles in visual studio 2008. But when i open the same code in Visual Studio 2015 or 2017, its returning False as the out come. The issue is not only with IsInRole, but also its returning null for My.User.Name in the latest Visual Studio. Can some one please suggest what is the way to over come the issue. PS: we are not supposed to make much code changes. Below are the issues.

VS 2008 Returning True enter image description here

VS 2017 Returning False enter image description here

also i have tried with the below code (where it is also not working correctly)

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

and in one MSDN portal, one person mentioned like its not working for him aswell and told me to raise a bug with Microsoft. but am not sure whether its a bug or my approach is wrong......

Upvotes: -1

Views: 528

Answers (2)

kabinx
kabinx

Reputation: 35

  • Check if the role exists

Some information from MSDN:

The exact behavior of the My.User object depends on the type of the application and on the operating system on which the application runs. For more information, see the User class overview.

Also My.User.IsInRole is only available to Windows Forms.

Upvotes: 0

Ondřej
Ondřej

Reputation: 1673

Try to call My.User.InitializeWithWindowsUser() on application startup.

Upvotes: 0

Related Questions