AJUDA CONVERTER CÓDIGO
Pessoal alguém pode me ajudar a converter em código de C# para VB.NET ?
http://geekswithblogs.net/jkhines/archive/2009/03/25/article-working-with-windows-group-membership.aspx
4. Finding the Groups a User can Manage
Lastly, once you know what groups your user is in, you may then want to know what groups they have permission to manage. For direct management you[ô]ll want to check the IADsUser.managedObjects property. For direct and inherited management it[ô]s preferable to submit an ADO query and have the domain find out for you.
The algorithm I use constructs an ADO query filter that essentially says “Return every group that is directly managed by my user or any group my user is a member ofâ€. Be aware this misses things like permission settings on OUs, but it[ô]s the best method I[ô]ve found so far.
using System;
using System.Text;
using System.DirectoryServices;
using System.Collections;
string username = [Ô]MyDomain\jkhines[Ô];
//
// perform an AD search for the user object
// assume username is set to MyDomain\MyUser
//
DirectorySearcher ds = new DirectorySearcher();
ds.SearchRoot = new DirectoryEntry([Ô]LDAP://DC=MyDomain,DC=com[Ô]);
ds.SearchScope = SearchScope.Subtree;
ds.Filter = String.Format([Ô](&(objectCategory=person)(sAMAccountName={0}))[Ô],
username.Split([ô]\\[ô])[1]);
ds.PropertiesToLoad.Add([Ô]adsPath
SearchResult userSearch = ds.FindOne();
string userAdsPath = userSearch.Properties[[Ô]adsPath[Ô]][0].ToString();
//
// bind to the user object and get the tokenGroups
//
DirectoryEntry user = new DirectoryEntry(userAdsPath);
user.RefreshCache(new String[] {[Ô]tokenGroups[Ô]});
//
// start construction of a query filter of all the groups the user belongs to
//
StringBuilder filter = new StringBuilder();
filter.AppendFormat([Ô](|(&(objectCategory=group)(managedBy={0}))[Ô],
user.Properties[[Ô]distinguishedName[Ô]][0]);
DirectoryEntry group;
byte[] currentSID;
StringBuilder hexString = new StringBuilder();
IEnumerator groupList = user.Properties[[Ô]tokenGroups[Ô]].GetEnumerator();
while (groupList.MoveNext()) {
//
// convert the byte[] SID to a friendly SID string
//
currentSID = (byte[])groupList.Current;
foreach (byte inByte in currentSID) {
hexString.AppendFormat([Ô]{0:X2}[Ô], inByte);
}
//
// bind to the group to get the distinguishedName
//
try {
group = new DirectoryEntry(String.Format([Ô]LDAP://[Ô],
hexString.ToString()));
filter.AppendFormat([Ô](&(objectCategory=group)(managedBy={0}))[Ô],
group.Properties[[Ô]distinguishedName[Ô]][0]);
}
catch(Exception err) {
// ignore errors if this is a local group
}
hexString.Remove(0, hexString.Length);
}
filter.Append([Ô])[Ô]);
//
// set new searcher filter
//
ds.Filter = filter.ToString();
//
// execute new query and display results
//
foreach(SearchResult groupSearch in ds.FindAll()) {
// use groupSearch.Properties[[Ô]adsPath[Ô]][0];
}
http://geekswithblogs.net/jkhines/archive/2009/03/25/article-working-with-windows-group-membership.aspx
4. Finding the Groups a User can Manage
Lastly, once you know what groups your user is in, you may then want to know what groups they have permission to manage. For direct management you[ô]ll want to check the IADsUser.managedObjects property. For direct and inherited management it[ô]s preferable to submit an ADO query and have the domain find out for you.
The algorithm I use constructs an ADO query filter that essentially says “Return every group that is directly managed by my user or any group my user is a member ofâ€. Be aware this misses things like permission settings on OUs, but it[ô]s the best method I[ô]ve found so far.
using System;
using System.Text;
using System.DirectoryServices;
using System.Collections;
string username = [Ô]MyDomain\jkhines[Ô];
//
// perform an AD search for the user object
// assume username is set to MyDomain\MyUser
//
DirectorySearcher ds = new DirectorySearcher();
ds.SearchRoot = new DirectoryEntry([Ô]LDAP://DC=MyDomain,DC=com[Ô]);
ds.SearchScope = SearchScope.Subtree;
ds.Filter = String.Format([Ô](&(objectCategory=person)(sAMAccountName={0}))[Ô],
username.Split([ô]\\[ô])[1]);
ds.PropertiesToLoad.Add([Ô]adsPath
SearchResult userSearch = ds.FindOne();
string userAdsPath = userSearch.Properties[[Ô]adsPath[Ô]][0].ToString();
//
// bind to the user object and get the tokenGroups
//
DirectoryEntry user = new DirectoryEntry(userAdsPath);
user.RefreshCache(new String[] {[Ô]tokenGroups[Ô]});
//
// start construction of a query filter of all the groups the user belongs to
//
StringBuilder filter = new StringBuilder();
filter.AppendFormat([Ô](|(&(objectCategory=group)(managedBy={0}))[Ô],
user.Properties[[Ô]distinguishedName[Ô]][0]);
DirectoryEntry group;
byte[] currentSID;
StringBuilder hexString = new StringBuilder();
IEnumerator groupList = user.Properties[[Ô]tokenGroups[Ô]].GetEnumerator();
while (groupList.MoveNext()) {
//
// convert the byte[] SID to a friendly SID string
//
currentSID = (byte[])groupList.Current;
foreach (byte inByte in currentSID) {
hexString.AppendFormat([Ô]{0:X2}[Ô], inByte);
}
//
// bind to the group to get the distinguishedName
//
try {
group = new DirectoryEntry(String.Format([Ô]LDAP://[Ô],
hexString.ToString()));
filter.AppendFormat([Ô](&(objectCategory=group)(managedBy={0}))[Ô],
group.Properties[[Ô]distinguishedName[Ô]][0]);
}
catch(Exception err) {
// ignore errors if this is a local group
}
hexString.Remove(0, hexString.Length);
}
filter.Append([Ô])[Ô]);
//
// set new searcher filter
//
ds.Filter = filter.ToString();
//
// execute new query and display results
//
foreach(SearchResult groupSearch in ds.FindAll()) {
// use groupSearch.Properties[[Ô]adsPath[Ô]][0];
}
Boa tarde
Segue link par converter C# para VB.NET
http://converter.telerik.com/
Segue link par converter C# para VB.NET
http://converter.telerik.com/
O framework é o mesmo. São os mesmos tipos de objetos, mesmos métodos.
A única diferença é a mudança de sintaxe.
Pode parecer confuso no começo, até porque essas chaves [Ô]{}[Ô] assustam no começo.
DirectorySearcher ds = new DirectorySearcher();
Aqui estamos instanciando um objeto. Veja que não é tão diferente do VB.
Só que o C# usa: TipoObjeto nomeVariavel = new TipoObjeto();
No VB:
Dim ds As New DirectorySearcher
if no C#:
if (condicao)
{
}
else
{
}
No VB.NET:
If condicao Then
Else
End If
Só com isso você já mata praticamente todo o código.
Abraços!
A única diferença é a mudança de sintaxe.
Pode parecer confuso no começo, até porque essas chaves [Ô]{}[Ô] assustam no começo.
DirectorySearcher ds = new DirectorySearcher();
Aqui estamos instanciando um objeto. Veja que não é tão diferente do VB.
Só que o C# usa: TipoObjeto nomeVariavel = new TipoObjeto();
No VB:
Dim ds As New DirectorySearcher
if no C#:
if (condicao)
{
}
else
{
}
No VB.NET:
If condicao Then
Else
End If
Só com isso você já mata praticamente todo o código.
Abraços!
Tópico encerrado , respostas não são mais permitidas