VB.NET - Comment convertir SID en nom de groupe avec Active Directory
-
27-10-2019 - |
Question
À l'aide de VB.NET, comment convertissez-vous le SID en nom de groupe avec Active Directory?
Exemple: j'ai besoin d'obtenir "groupe_test" et non "S-1-5-32-544"
Le code que j'utilise est:
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
ou quelque chose comme ça?
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
Comment l'adapterais-tu à ce code? (Si l'utilisateur est en groupe xx, affichez le groupe xx sur la liste déroulante)
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
La solution
Voici une manière simple écrite en C #, je pense qu'il n'est pas trop difficile d'adapter:
/* Retreiving object from SID
*/
string SidLDAPURLForm = "LDAP://WM2008R2ENT:389/<SID={0}>";
System.Security.Principal.SecurityIdentifier sidToFind = new System.Security.Principal.SecurityIdentifier("S-1-5-21-3115856885-816991240-3296679909-1106");
DirectoryEntry userEntry = new DirectoryEntry(string.Format(SidLDAPURLForm, sidToFind.Value));
string name = userEntry.Properties["cn"].Value.ToString();
Ici, c'est dans VB .NET grâce à RÉFLECTEUR
Dim SidLDAPURLForm As String = "LDAP://WM2008R2ENT:389/<SID={0}>"
Dim sidToFind As New SecurityIdentifier("S-1-5-21-3115856885-816991240-3296679909-1106")
Dim userEntry As New DirectoryEntry(String.Format(SidLDAPURLForm, sidToFind.Value))
Dim name As String = userEntry.Properties.Item("cn").Value.ToString
---- Édité ----- Voici donc ce que vous voulez, mais c'est la même chose que celle-ci a été précédemment donnée par @biggstrc
Private Shared Sub Main(args As String())
Dim currentUser As WindowsIdentity = WindowsIdentity.GetCurrent()
For Each iRef As IdentityReference In currentUser.Groups
Console.WriteLine(iRef.Translate(GetType(NTAccount)))
Next
End Sub
Autres conseils
Code en C #:
public static string GetGroupNameBySid(string sid)
{
using(var ctx =
new PrincipalContext(ContextType.Domain))
{
using(avr group =
GroupPrincipal.FindByIdentity(
ctx,
IdentityType.Sid,
sid))
{
return group?.SamAccountName;
}
}
}
Vous devez ajouter un assembly System.DirectoryServices.AccountManagement.dll. Si vous avez des problèmes avec la connexion à AD, vous pouvez essayer d'ajouter le nom du serveur AD dans le constructeur de PrincipalContext.
Voici un lien pour convertir un SID en un nom: http://vbdotnet.canbal.com/view.php?SessionID=jef85k%2B%2Bj9pz%2BWZ9HJJicw%2FyEptAdxfcPycovz7js%3D
Fondamentalement, vous obtenez un objet DirectoryEntry que vous pouvez ensuite utiliser pour obtenir le nom. Cependant, si vous cherchez ce que je pense être une méthode plus facile pour ce faire, prenez simplement l'utilisateur actuel et faites une recherche dans l'annonce pour leurs abonnements de groupe. Voici un exemple de la façon de procéder (vous aurez besoin de l'article plus large pour réellement accomplir votre tâche, mais ce code est la réponse spécifique à votre question): http://www.codeproject.com/kb/system/Everythinginad.aspx#39
Désolé pour le fait que le code est en C #. Cependant, vous devriez pouvoir simplement utiliser un convertisseur pour le convertir en VB.NET sans problème.
Obtenez des abonnements de groupe d'utilisateurs de l'utilisateur connecté à ASP.NET en C #
public ArrayList Groups()
{
ArrayList groups = new ArrayList();
foreach (System.Security.Principal.IdentityReference group in
System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
{
groups.Add(group.Translate(typeof
(System.Security.Principal.NTAccount)).ToString());
}
return groups;
}
Obtenez des abonnements de groupe d'utilisateurs de l'utilisateur connecté à l'ASP.NET dans VB.NET en utilisant L'outil de convertisseur du développeur Fusion:
Public Function Groups() As ArrayList
Dim groups__1 As New ArrayList()
For Each group As System.Security.Principal.IdentityReference In System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups
groups__1.Add(group.Translate(GetType(System.Security.Principal.NTAccount)).ToString())
Next
Return groups__1
End Function