Using VB.NET, How do you Convert the sid to Group Name with Active Directory?
example: I need to get "group_test" and not "S-1-5-32-544"
The code I'm using is:
Public ReadOnly Property Groups As IdentityReferenceCollection
Get
Dim irc As IdentityReferenceCollection
Dim ir As IdentityReference
irc = WindowsIdentity.GetCurrent().Groups
Dim strGroupName As String
For Each ir In irc
Dim mktGroup As IdentityReference = ir.Translate(GetType(NTAccount))
MsgBox(mktGroup.Value)
Debug.WriteLine(mktGroup.Value)
strGroupName = mktGroup.Value.ToString
Next
Return irc
End Get
End Property
or something like this?
currentUser = WindowsIdentity.GetCurrent()
For Each refGroup As IdentityReference In currentUser.Groups
Dim acc As NTAccount = TryCast(refGroup.Translate(GetType(NTAccount)), NTAccount)
If AdminGroupName = acc.Value Then
ret = "999"
End If
If UsersGroupName = acc.Value Then
ret = "1"
End If
how would u adapt it to this code? (if user is in xx group, show xx group on drop down list)
For Each UserGroup In WindowsIdentity.GetCurrent().Groups
If mktGroup.Value = "BIG" Then
Dim Company = ac1.Cast(Of MarketingCompany).Where(Function(ac) ac.MarketingCompanyShort = "BIG").FirstOrDefault
If Company IsNot Nothing Then
marketingCo.Items.Add(String.Format("{0} | {1}", Company.MarketingCompanyShort, Company.MarketingCompanyName))
End If
End If
Next
Here is a simple way writen in C#, I think it's not to hard to adapt :
Here it is in VB .NET thanks to REFLECTOR
---- EDITED ----- So here is what you wants, but it's the same as that was previously given by @BiggsTRC
Code in C#:
You must add assembly System.DirectoryServices.AccountManagement.dll. If you have any troubles with connection to AD, you can try adding AD server name in PrincipalContext constructor.
Here is a link for how to convert a SID to a name: http://vbdotnet.canbal.com/view.php?sessionid=JEf85K%2B%2BeBj9Pz%2BWz9hJJicW%2FYEPtADXfcpYCovZ7js%3D
Basically, you get a DirectoryEntry object back which you can then use to get the name. However, if you are looking for what I believe to be an easier method to do this, just take the current user and do a lookup in AD for their group memberships. Here is an example of how to do that (you will need the larger article to actually accomplish your task but this code is the specific answer to your question): http://www.codeproject.com/KB/system/everythingInAD.aspx#39
Sorry about the fact that the code is in C#. However, you should be able to just use a converter to convert it to VB.NET without a problem.
Get User Group Memberships of the Logged in User from ASP.NET in C#
Get User Group Memberships of the Logged in User from ASP.NET in VB.NET using Developer Fusion's Converter Tool: