Quantcast
Channel: Dynamic Code Blocks
Viewing all articles
Browse latest Browse all 195

.NET 4.6.1 or 4.6.2 seem to break IsInRole()

$
0
0

Upgraded application using IsInRole(), now only returns false (vb.net)

Read to the end before making code changes as there is a more obvious thing to check!
 
To support TLS1.2 for PCI requirements I was upgrading one of the applications to 4.6.1, after deployment behaviour controlled by our active directory groups was broken. It was like no one was a member of any AD groups anymore. First I thought it must be a coincidental screw up by someone in AD. It turns out it was something else…
 
The following code is used to check against a list of security groups to see if the current user belongs to any of them.
 
PublicSharedFunction IsInAdSecurityRole(RoleName() AsString) AsBoolean
Dim aName AsString = Principal.WindowsIdentity.GetCurrent.Name
Dim aDomain AsString = aName.Substring(0, aName.IndexOf("\") + 1)
AppDomain.CurrentDomain.SetPrincipalPolicy(
Principal.PrincipalPolicy.WindowsPrincipal)
For index = 0 To RoleName.Count - 1
If Thread.CurrentPrincipal.IsInRole(aDomain & RoleName(index)) Then
ReturnTrue
EndIf
Next
ReturnFalse
End Function

The code is ancient, has been in our applications for a very long time but on upgrading to .NET framework 4.6.1 it returns false for all roles. Checked casing and ran in debug inspection and yet failed to see why it stopped behaving as it always had before.
 
Unable to figure out what had happened and with a need to get systems running again I imported the namespace System.Security.Principal
then using the following method all seems well again.
 
PublicSharedFunction IsInAdSecurityRole(RoleName() AsString) AsBoolean
Dim currPrincipal AsNew WindowsPrincipal(New WindowsIdentity(Environment.UserName))
For index = 0 To RoleName.Count - 1
If currPrincipal.IsInRole(RoleName(index)) Then
ReturnTrue
EndIf
Next
ReturnFalse
End Function

I used this reference:

My.User.IsInRole() is not working after migrating to 4.6.2 framework in vb.net

 

Authentication mode in project settings Application-defined vs Windows

VB.NET has a setting in the project to say you wish to use application provided authentication method or use the default windows one. This was something that I had totally forgotten existed. It looks like the Authentication mode of the project got changed during the migration. Check the properties of the project, Authentication mode, see if changing it from Application-defined to Windows helps, it did in my case, bringing behaviour back to that which is expected.

 

2018-06-14_12-14-50

Change the drop down combo box to “Windows”

2018-06-14_12-23-38

 

Reference: https://social.msdn.microsoft.com/Forums/en-US/d00b65dd-61d8-4368-b2d2-eaedfc66af40/myusername-is-now-returning-empty-string?forum=vbgeneral


Viewing all articles
Browse latest Browse all 195

Trending Articles