C# - Find Out User Belongs To Which Groups

Sep 8, 2010

I have a windows user accounts which i just created take XYZ for example.

This XYZ belongs to a User group and a custom group i created in Computer Management --> Local users and groups.

So in properties i see that the user belongs to the 2 groups.

Now i want to get those groups and display them. any suggestions???

i have done this but this is not right as it gives me the roles of SQL (i think)

here is what i did:

after logging in and impersonating i call the function

getUserGroups();

private void getUserGroups()
{
// collect the user domain and identity
string[] arr =
System.Web.HttpContext.Current.Request.
LogonUserIdentity.Name.Split('\');
// update the display to show
// the captured domain and user
if (arr.Length > 0)
{
new GUIUtility().LogMessageToFile("User Name" + arr[0].ToString());
new GUIUtility().LogMessageToFile("User Domain" + arr[1].ToString());
}
// create an arraylist and populate
// it with the list of groups that
// the current user belongs to
ArrayList al = new ArrayList();
al = GetGroups();
// check to see if the user belongs
// to a specific group and create
// a list of all of the user's groups
foreach (string s in al)
{
// add this one to the list
new GUIUtility().LogMessageToFile("Group" + s);
// check to see if the user
// belongs to a specific group
//if (s == "BXSWLT\SomeCustomGroup")
//{
// // change the label to show
// // there was a match
// lblMemberOfGroup.Text = "YES";
//}
}
}
public ArrayList GetGroups()
{
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;
}

the Result i get is:

9/8/2010 5:57:22 PM: User Name NT AUTHORITY.
9/8/2010 5:57:22 PM: User Domain IUSR.
9/8/2010 5:57:22 PM: Group Everyone.
9/8/2010 5:57:22 PM: Group BUILTINUsers.
9/8/2010 5:57:22 PM: Group NT AUTHORITYAuthenticated Users.
9/8/2010 5:57:22 PM: Group NT AUTHORITYThis Organization.
9/8/2010 5:57:22 PM: Group LOCAL.

View 1 Replies


Similar Messages:

How To Check Whether A User Belongs To An AD Group And Nested Groups

Apr 21, 2010

I have an ASP.NET 3.5 application using Windows Authentication and implementing our own RoleProvider.

Problem is we want to restrict access to a set of pages to a few thousand users and rathern than inputing all of those one by one we found out they belong to an AD group.

The answer is simple if the common group we are checking membership against the particular user is a direct member of it but the problem I'm having is that if the group is a member of another group and then subsequently member of another group then my code always returns false.

For example: Say we want to check whether User is a member of group E, but User is not a direct member of *E", she is a member of "A" which a member of "B" which indeed is a member of E, therefore User is a member of *E"

One of the solutions we have is very slow, although it gives the correct answer

using (var context = new PrincipalContext(ContextType.Domain))
{
using (var group = GroupPrincipal.FindByIdentity(context, IdentityType.Name, "DL-COOL-USERS"))
{
var users = group.GetMembers(true); // recursively enumerate
return users.Any(a => a.Name == "userName");
}
}

The original solution and what I was trying to get to work, using .NET 3.5 System.DirectoryServices.AccountManagement and it does work when users are direct members of the group in question is as follows:

public bool IsUserInGroup(string userName, string groupName)
{
var cxt = new PrincipalContext(ContextType.Domain, "DOMAIN");
var user = UserPrincipal.FindByIdentity(cxt, IdentityType.SamAccountName, userName);
if (user == null)
{
return false;
}
var group = GroupPrincipal.FindByIdentity(cxt, groupName);
if (group == null)
{
return false;
}
return user.IsMemberOf(group);
}

The bottom line is, we need to check for membership even though the groups are nested in many levels down.

View 3 Replies

C# - How To Access Active Directory To Get The List Of Groups That A Certain User Belongs To

Aug 24, 2010

In C#, how do i access Active Directory to get the list of groups that a certain user belongs to?

The user details are in the form:

"MYDOMAINmyuser"

I've been following the instructions from here but they only work if i have the user details in the form:

"LDAP://sample.com/CN=MySurname MyFirstname,OU=General,OU=Accounts,DC=sample,DC=com"

So maybe what i'm asking is, how to get from the first, shorter, form to the fully qualified form below?

View 2 Replies

C# - Get Local Groups And Not The Primary Groups For A Domain User

Dec 18, 2010

i have a code to get the groups a user belongs to.

try
{
DirectoryEntry adRoot = new DirectoryEntry(string.Format("WinNT://{0}", Environment.UserDomainName));
DirectoryEntry user = adRoot.Children.Find(completeUserName, "User");
object obGroups = user.Invoke("Groups");
foreach (object ob in (IEnumerable)obGroups)
{
// Create object for each group.
DirectoryEntry obGpEntry = new DirectoryEntry(ob);
listOfMyWindowsGroups.Add(obGpEntry.Name);
}
return true;
}
catch (Exception ex)
{
new GUIUtility().LogMessageToFile("Error in getting User MachineGroups = " + ex);
return false;
}

the above code works fine when i have to find the groups of a local user but

for a domain user it returns a value "Domain User" which is kind of wierd as it is a part of 2 local groups.

Research

I did some finding and got that i am being returned the primary group of the domain user

called "Domain User" group

but what i actually want is the groups of the local machines the domain user is a part of... i cannot get that.. any suggestions

another code using LDAP

string domain = Environment.UserDomainName;
DirectoryEntry DE = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);
DirectorySearcher search = new DirectorySearcher();
search.SearchRoot = DE;
search.Filter = "(SAMAccountName=" + completeUserName + ")"; //Searches active directory for the login name
search.PropertiesToLoad.Add("displayName"); // Once found, get a list of Groups
try
{
SearchResult result = search.FindOne(); // Grab the records and assign them to result
if (result != null)
{
DirectoryEntry theUser = result.GetDirectoryEntry();
theUser.RefreshCache(new string[] { "tokenGroups" });
foreach (byte[] resultBytes in theUser.Properties["tokenGroups"])
{
System.Security.Principal.SecurityIdentifier mySID = new System.Security.Principal.SecurityIdentifier(resultBytes, 0);
DirectorySearcher sidSearcher = new DirectorySearcher();
sidSearcher.SearchRoot = DE;
sidSearcher.Filter = "(objectSid=" + mySID.Value + ")";
sidSearcher.PropertiesToLoad.Add("distinguishedName");
SearchResult sidResult = sidSearcher.FindOne();
if (sidResult != null)
{
listOfMyWindowsGroups.Add((string)sidResult.Properties["distinguishedName"][0]);
}
}
}
else
{
new GUIUtility().LogMessageToFile("no user found");
}
return true;
}
catch (Exception ex)
{
new GUIUtility().LogMessageToFile("Error obtaining group names: " + ex.Message + " Please contact your administrator."); // If an error occurs report it to the user.
return false;
}

this works too but i get the same result "Domain Users" . Please can some1 tell me how to get the local machine groups...?

View 2 Replies

How To Find Which Class Belongs To Which DLL

Sep 4, 2010

I am new to .NET dev and I cut and paste a lot of code. One strugel i have is when I I use a class name, I need to find out what I need to add in the reference (using clause at the top) and what DLL to use. Is their a easy way to find out what class is on what DLL. My pc is littered with 500 - 600 .NET dll.

View 4 Replies

Active Directory/LDAP :: Getting The Group Name User Belongs To Error

Oct 28, 2010

I have the following AD Strucure

Domain Controller with the Name like abc.com ........> Groups Folder .....>>>UserGroups >>> Subgroup >>>> List of Users


I did the following but not succeed

[Code]....

Iam getting the Count == 0 at the below line

int groupCount = result.Properties["memberOf"].Count;

View 2 Replies

Forms Data Controls :: Gridview: Filter Display Results Based On What Roll A User Belongs

Oct 20, 2010

I have enabled User Rolls and assigned several Roll names such as 10, 11, 12, etc. I have a SQL Table that contains various fields of information including one named AuthorizedUserRoll, it's nchar(2).

A logged in user should only see the records in Gridview that matches his/her roll.

I'm using Web Forms and VB.

View 1 Replies

Security :: Web.config Authorization Roles With Local Groups With Domain Groups In Them

Sep 29, 2010

I am "DomainMyDomainUserName" If I do this:

[Code]....

I can access the website I've created on my local machine in debug mode; and with my domain account as a local administrator, if I do this:

[Code]....

I can still access the website; and with my domain account in a Domain Group named "DomainDomainLocalSecurityGroup", if I do this:

[Code]....

I can still access the website; HOWEVER, if I create a Local Group on my machine named "LocalMachineGroup" and I add "DomainMyDomainUserName" to this group and I do this:

[Code]....

I get an Access Denied error trying to access the website. What I want to do is, have a group on my local machine (for testing), on a test web server, and on a production web server named "MyWebsiteUserAccess" with the same Domain Group (filled with Domain Users allowed access to the site) in all of these local groups; so that when I test the web application on my local machine, on my test web server and on my production web server, I don't have to change the Web.config file to have it work on each AND if the Domain Group name ever has to change, it won't affect access to the website.

What am I overlooking or is this even possible to do this way? I understand that there is a way to do Role Management through ASP.NET but I don't understand that well enough yet to implement that (and will probably go that route once I've done the research on how to best implement it for purpose of access control of this intranet site).

View 2 Replies

Enumerating Groups Using WindowsIdentity.GetCurrent() Returns Old Set Of Groups

Feb 1, 2011

I have a user that when I attempt to enumerate her groups using either System.Security.Principal.WindowsIdentity.GetCurrent().Groups on user's computer
or System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups on the IIS server, I get a list of their old groups, not their new. I have checked the DC that they authenticate to and the ASP.NET page is running inside a SharePoint instance so the LogonUserIdentity most like is being provided from there Kerberos ticket.
Should I be looking up the groups directly from the AD instead of relying on the WindowsIdentity or LogonUserIdentity?

View 1 Replies

How To Get The Groups Of A User In Active Directory

Mar 15, 2011

I use this code to get the groups of the current user. But I want to manually give the user and then get his groups. How can I do this?

[code]....

View 2 Replies

Any Way To Get Domain Level User Groups?

Mar 31, 2011

In asp.net/c#, is there a way to find all of the AD level user groups from a given domain in which the logged in user belongs to? E.g. I have a domain level user and few groups. I want to pass in the domain name and the logged in user's user name and get a list of AD groups of that particular domain.

View 2 Replies

C# - Check For Groups A User Is A Member Of

Sep 9, 2010

i have the code to get the members of a local group example administrators

private void GetUserGrps()
{
using (DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group"))
{
foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
{
using (DirectoryEntry memberEntry = new DirectoryEntry(member))
{
new GUIUtility().LogMessageToFile(memberEntry.Path);
}
}
}

Is there a way to get the groups a local user belongs to using directory services?

without using activedirectory or domain in it because i want for the local machine only and not for a domain.

View 1 Replies

Active Directory/LDAP :: Get AD Groups For User By Username In 4.0

Aug 5, 2010

I've been searching for a solution to get a list of the AD Groups for at specific AD user. I'll need the current user logged in and would like to query by the username. The problem is that all the solutions I find on the web uses System.DirectoryServices - and I can't find the dll in my .Net 4.0 webproject (I only have System.DirectoryServices.AccountManagement) - so I can't use any of the examples that I've found. Is there a difference way to query the AD through 4.0 compared to 2.0? I could really use some examples, articles or just good advise to get moving - I'm kind of stuck at the moment.

View 4 Replies

Active Directory/LDAP :: Getting User Groups / Can't Use UserPrincipal.GetGroups In System

Mar 8, 2010

I am having trouble using UserPrincipal.GetGroups in System.DirectoryServices.AccountManagement.

When I use the following code:

PrincipalSearchResult<Principal> groups = userPrincipal.GetGroups();

foreach (GroupPrincipal g in groups){
Console.WriteLine(g.DisplayName);
}

I get the error "The value for the property AttributeScopeQuery cannot be set" in the foreach loop. I also get the same error when using groups.Count()I have read things that indicate this may be due to having some Windows 2000 domain controllers in the domain and so meaning it does not support ASQ.

What is the best way of doing this? The best I have come up with, since most of the rest of my code is running fine using System.DirectoryServices.AccountManagement is using the GetUnderlyingObject method of the UserPrincipal to get a DirectoryEntry and enumerating the values of the memberOf attribute. Using LINQ the code is quite compact:

var groups = from string groupPath in
((DirectoryEntry)userPrincipal.GetUnderlyingObject()).Properties["memberOf"]
select new DirectoryEntry("LDAP://" + groupPath).Name.Replace("CN=", "");
foreach(groupName in groups){
Console.WriteLine(groupName);
}

View 1 Replies

Active Directory/LDAP :: Get User Groups From Open Ldap Using Vb.net

Apr 1, 2011

I'm trying to get the groups a user is member of from an Open Ldap using vb.net 3.5.

Although so far I have managed to connect to the ldap server and get the user's info like sn, givenname, description, mail, adspath,

I have absolutely no information about the user's groups.

My code is like this:

Dim oEntry as New DirectoryEntry
Dim oSearcher as New DirectorySearcher
oEntry = New DirectoryEntry("LDAP://myServer/ou=test, dc=example, dc=com", username, pwd, AuthenticationTypes.ServerBind)
oSearcher = New DirectorySearcher(oEntry, "(uid=" & uName & ")")
oResult = oSearcher.FindOne()
if not oResult is Nothing then
Dim ADUser as DirectoryEntry
ADUSer = oResult.GetDirectoryEntry()
end if

Well, ADUser has no info on user membership whatsoever.

Am I doing something wrong? Should I use System.DirectoryServices.Protocols as I saw someone mentioning instead

of System.DirectoryServices or is .Protocols just for Framework 2.0?

View 1 Replies

ADO.NET :: Row Belongs To Another Table Data Row?

Dec 12, 2010

I have a problem regarding data row.

What i am trying to do is that i have records that are in ascending order. Now, i have to get a few lowest data rows(based on the average) from the table and add them to a separate table and delete the rows from the previous table.

This is my code where i am getting the top few rows and adding them to the array list (I have also tried DataRow[], DataRowCollection, string[] but i get the same error).

ArrayList delRows = new ArrayList(diff);

View 1 Replies

C# - Get Processes That Belongs To A Particular Session?

Dec 9, 2010

I want to get all the processes that is meant for a particular session.
i.e if there are two persons who are using my web application then i want to get all the processes used by user1 and all the processes used by user2.

I can get all processes by writing Process.GetProcesses() but how to get it in a group.

e.g:

User1: Process342
User1: Process151
User2: Process452
User2: Process674
User2: Process111

View 1 Replies

User Controls :: Fetch Likes Of A Page And Find Whether Particular User Liked It Using Facebook Graph API?

Nov 16, 2013

I'm writing applications on Facebook located on an external website ASP.NET C #. The application should check whether or not a person likes my Fan Page (page) on Facebook, and if he or she doesn’t like it needs to like it before using this application. How this can be done using the Graph API and ASPSnippets Facebook API.

View 1 Replies

Identify Computer And Where It Belongs In Web Application / Website

Jun 29, 2010

simple explanation like:- four blocks all connected to one server hosting a website using iis 5 c# i want if user from block a accesses the home page the page says block A user how are you, same goes for all blocks so how to identify from where the request is coming from in this simple scenerio

View 1 Replies

Data Controls :: Column Named Link Already Belongs To This DataTable

Mar 26, 2016

I am using the C# code you posted in the link below,

[URL]

but when I run it I am getting the error: 

"A column named 'link' already belongs to this DataTable: cannot set a nested table name to the same name."

View 1 Replies

Forms Data Controls :: Get All The Checked Child Nodes Belongs To Specific Parent?

Dec 29, 2010

I have a treeview and all nodes are check box, when user clicks on a button, how can I get all the checked child nodes belongs to specific parent? I wanna something better...

private TreeNodeCollection GetCheckedNodeForParent (string parentNode)
{
TreeNodeCollection treeNodeCollection=new TreeNodeCollection();
foreach (TreeNode node in tvBaseline.FindNode(parentNode).ChildNodes)
{
if (node.Checked)
treeNodeCollection.Add(node);
}
return treeNodeCollection;
}

View 2 Replies

C# - Find User Is Online Or Not?

Jun 10, 2010

In my project i have to implement an interface which shows whether a particular user is online or not as in social networking site(like orkut and facebook)

View 3 Replies

C# - On Page Load - Navigate To A Specific Page Where A Listviewitem Belongs Using Listview And Datapager?

Oct 13, 2010

I have the selected DataKey in session from the ListView. I am able to set the selection back when I comeback to this aspx page containing listview. But when the selected item in the listview belongs to some other page (not the first listview page) then I need to also set the selected listview page to the one, where my item belongs. I use a listview and a datapager (with template paging) How can I find, in which page my item to be selected exists?

View 1 Replies

How To Find The User Location Country

Dec 16, 2010

how should i find the user country when the open the website ?

View 2 Replies

Security :: How To Find Out The Type Of User

May 11, 2010

web.config

[Code]....

i am allowing only tht particular users.In my code how to find out the current user type.And wht is the difference between

[Code]....

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved