VB.NET - Come convertire il nome SID in gruppo con Active Directory
-
27-10-2019 - |
Domanda
Utilizzando VB.NET, come si convertisce il nome SID in gruppo con Active Directory?
Esempio: devo ottenere "Group_test" e non "S-1-5-32-544"
Il codice che sto usando è:
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
O qualcosa del genere?
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
Come lo adatteresti a questo codice? (Se l'utente è nel gruppo XX, mostra il gruppo XX nell'elenco a discesa)
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
Soluzione
Ecco un modo semplice scritto in C#, penso che non sia difficile da adattare:
/* 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();
Eccolo in vb .net grazie a RIFLETTORE
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
---- Modificato ----- Quindi ecco quello che vuoi, ma è lo stesso di quello precedentemente dato da @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
Altri suggerimenti
Codice in 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;
}
}
}
È necessario aggiungere System Assembly.DirectoryServices.AccountManagement.dll. Se si hanno problemi con la connessione all'annuncio, è possibile provare ad aggiungere il nome del server AD nel costruttore PrincipalContext.
Ecco un link per come convertire un SID in un nome: http://vbdotnet.canbal.com/view.php?sessionid=jef85k%2b%2bebj9pz%2bwz9hjjicw%2fyeptadxfcpycovz7js%3D
Fondamentalmente, si ottiene un oggetto DirectoryEntry che puoi quindi utilizzare per ottenere il nome. Tuttavia, se stai cercando quello che credo sia un metodo più facile per farlo, prendi l'utente attuale e fai una ricerca nell'annuncio per le iscrizioni al loro gruppo. Ecco un esempio di come farlo (avrai bisogno dell'articolo più grande per realizzare effettivamente il tuo compito, ma questo codice è la risposta specifica alla tua domanda): http://www.codeproject.com/kb/system/everythinginad.aspx#39
Mi dispiace per il fatto che il codice sia in C#. Tuttavia, dovresti essere in grado di utilizzare solo un convertitore per convertirlo in vb.net senza problemi.
Ottieni abbonamenti al gruppo di utenti dell'utente loggato da ASP.NET in 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;
}
Ottieni abbonamenti al gruppo di utenti dell'utente loggato da ASP.NET in VB.NET utilizzando Strumento di convertitore di sviluppatore 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