473,467 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

c# - app - AD Group membership search crashes app for all users but me!

maxx233
32 New Member
I'm trying to get my program checking Active Directory to see if the user is a member of certain groups. I got it working, tested, etc. So I was very surprised when I deployed the program (just copying the .exe to their computer as I have in the past) and it wouldn't work for anyone!

I've tried it from multiple computers under multiple users, and it only runs under my username, but then works fine from any computer when *I* log in. I had a coworker with the same local and domain privileges and group memberships as me log into a computer and it wouldn't even work under her login! The group-checking function is done under a method called authenticateUser(), and if I just skip the call to that method the program works fine when deployed to all users - so that rules out file-level security. I'm fresh out of ideas and can't seem to find anything online. If anyone knows offhand what might be the problem that'd be great. Is there a way to debug from VS05 under a different user-name? That way I could at least track down more detail on the problem. Only other detail I can think to mention is that this is the first change I've made since migrating to a Vista machine, which I *hate* btw (still with VS05). Let me know if you have any ideas, my code is below:

Expand|Select|Wrap|Line Numbers
  1. private void authenticateUser()
  2.         {
  3.              DirectoryEntry de = new DirectoryEntry();
  4.             de.Path = "WinNT://myDomain/" + SystemInformation.UserName.ToString() + ",user";
  5.             _Teller = de.Properties["FullName"].Value.ToString();
  6.             _Client = SystemInformation.ComputerName.ToString();
  7.             Program._Teller = _Teller;
  8.  
  9. //THE ABOVE WORKS FINE TO FIND THE LOGGED-IN USER'S NAME, HAS BEEN IN PRODUCTION FOR SEVERAL VERSIONS
  10.  
  11.             DirectoryEntry DE = new DirectoryEntry("LDAP://myDomain.com");
  12.             DirectorySearcher search = new DirectorySearcher();
  13.             search.SearchRoot = DE;
  14.             search.Filter = "(givenName=" + SystemInformation.UserName.ToString() + ")";
  15.             search.PropertiesToLoad.Add("memberOf");
  16.  
  17. // THE ABOVE IS NEW, ALONG WITH ANYTHING RELATED BELOW
  18.  
  19.             try
  20.             {
  21.                 SearchResult result = search.FindOne();
  22.  
  23. //I THINK THE ABOVE LINE IS WHERE THE FAILURE ULTIMATELY OCCURS
  24.  
  25.                 int propertyCount = result.Properties["memberOf"].Count;
  26.                 string dn;
  27.                 for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
  28.                 {
  29.                     dn = (string)result.Properties["memberOf"][propertyCounter];
  30.                     try { dn = dn.Replace("CN=", ""); } catch { }
  31.                     try { dn = dn.Replace("OU=", ""); } catch { }
  32.                     try { dn = dn.Remove(dn.IndexOf(",")); } catch { }
  33.                     switch (dn)
  34.                     {
  35.                         case "Auditor":
  36.                             if (_SupervisorLevel < 2) { _SupervisorLevel = 2; }
  37.                             break;
  38.                         case "Cashier":
  39.                             if (_SupervisorLevel < 3) { _SupervisorLevel = 3; }
  40.                             break;
  41.                         case "Supervisor":
  42.                             if (_SupervisorLevel < 5) { _SupervisorLevel = 5; }
  43.                             break;
  44.                         case "Assistant Manager":
  45.                             if (_SupervisorLevel < 7) { _SupervisorLevel = 7; }
  46.                             break;
  47.                         case "Manager":
  48.                             if (_SupervisorLevel < 9) { _SupervisorLevel = 9; }
  49.                             break;
  50.                     }
  51.                 }
  52.             }
  53.             catch (Exception ex)
  54.             {
  55.                 throw new Exception("Error: " + ex.Message);
  56.             }
  57.  
  58.  
  59.             if (_SupervisorLevel == 2)
  60.             {
  61.                 //Do Stuff
  62.  
  63.             }
  64.             if (_SupervisorLevel >= 3)
  65.             {
  66.                 //Do Stuff      
  67.             }
  68.             if (_SupervisorLevel >= 5)
  69.             {
  70.                 //Do Stuff
  71.             }
  72.         }
  73.  
May 19 '08 #1
8 2829
Plater
7,872 Recognized Expert Expert
Are you able to discover which line caused the failure?
What was the exception?

I got "object reference not set to an instance of an object" right around:
int propertyCount = result.Properties["memberOf"].Count;

(I did notice and remember to change the "mydomain" to my actual domain)
May 19 '08 #2
maxx233
32 New Member
Are you able to discover which line caused the failure?
What was the exception?

I got "object reference not set to an instance of an object" right around:
int propertyCount = result.Properties["memberOf"].Count;

(I did notice and remember to change the "mydomain" to my actual domain)
No, I'm not able to figure out where I'm having issues - when I hit F5 to begin debugging it works fine (because I'm logged in as me)... if I take the .exe it produces though and drop it on another computer it will only run when I'm logged into that pc. Is there any way to run debugging as a different user?

I believe that's the same place I'm having issues - when the exe fails on the test machine and i debug that, it says the 'object reference not set' error, but I can't trace down exactly where it's coming from other than my authenticateUser() method.

But I don't understand why running it as a different user with appropriate permissions would cause a hangup there when it works fine for my account.
May 19 '08 #3
Plater
7,872 Recognized Expert Expert
You could make a special "debug" version or your program with Console.Writeline()s (or MessageBox.Show()s) before every line of the function, the last one to show will tell you what line it crashed on?

I also did this (had it return a datatable of all the properties it found)
Expand|Select|Wrap|Line Numbers
  1. private DataTable authenticateUser()
  2. {
  3.     DataTable dt = new DataTable();
  4.     dt.Columns.Add("Name");
  5.     dt.Columns.Add("Value");
  6.   string mydomain = "mydomain";
  7.   int _SupervisorLevel = 0;
  8.   string _Teller = "";
  9.   string _Client = "";
  10.  
  11.   DirectoryEntry de = new DirectoryEntry();
  12.   de.Path = "WinNT://"+mydomain+"/" + SystemInformation.UserName.ToString() + ",user";
  13.   _Teller = de.Properties["FullName"].Value.ToString();
  14.   _Client = SystemInformation.ComputerName.ToString();
  15.   //Program._Teller = _Teller;
  16.  
  17.   DirectoryEntry DE = new DirectoryEntry("LDAP://" + mydomain);
  18.   DirectorySearcher search = new DirectorySearcher();
  19.   search.SearchRoot = DE;
  20.   //search.Filter = "(givenName=" + SystemInformation.UserName.ToString() + ")";
  21.   //search.PropertiesToLoad.Add("memberOf");
  22.  
  23.   try
  24.   {
  25.       SearchResult result = search.FindOne();
  26.       foreach (string PropertyName in result.Properties.PropertyNames)
  27.       {
  28.         DataRow dr = dt.NewRow();
  29.         dr["Name"] = PropertyName;
  30.         string valstring = "";
  31.         ResultPropertyValueCollection rvpc = result.Properties[PropertyName];
  32.         for (int i = 0; i < rvpc.Count; i++)
  33.         {
  34.             valstring += o.ToString() + "\r\n";
  35.         }
  36.         dr["Value"] = valstring;
  37.         dt.Rows.Add(dr);
  38.       }
  39.  
  40.   }
  41.   catch (Exception ex)
  42.   {
  43.       throw new Exception("Error: " + ex.Message);
  44.   }
  45. }
  46.  
Then I looked at it in a DataGridView, there was nothing about "memberof" or "givenName"
May 20 '08 #4
maxx233
32 New Member
You could make a special "debug" version or your program with Console.Writeline()s (or MessageBox.Show()s) before every line of the function, the last one to show will tell you what line it crashed on?

I also did this (had it return a datatable of all the properties it found)
Expand|Select|Wrap|Line Numbers
  1. private DataTable authenticateUser()
  2. {
  3.     DataTable dt = new DataTable();
  4.     dt.Columns.Add("Name");
  5.     dt.Columns.Add("Value");
  6.   string mydomain = "mydomain";
  7.   int _SupervisorLevel = 0;
  8.   string _Teller = "";
  9.   string _Client = "";
  10.  
  11.   DirectoryEntry de = new DirectoryEntry();
  12.   de.Path = "WinNT://"+mydomain+"/" + SystemInformation.UserName.ToString() + ",user";
  13.   _Teller = de.Properties["FullName"].Value.ToString();
  14.   _Client = SystemInformation.ComputerName.ToString();
  15.   //Program._Teller = _Teller;
  16.  
  17.   DirectoryEntry DE = new DirectoryEntry("LDAP://" + mydomain);
  18.   DirectorySearcher search = new DirectorySearcher();
  19.   search.SearchRoot = DE;
  20.   //search.Filter = "(givenName=" + SystemInformation.UserName.ToString() + ")";
  21.   //search.PropertiesToLoad.Add("memberOf");
  22.  
  23.   try
  24.   {
  25.       SearchResult result = search.FindOne();
  26.       foreach (string PropertyName in result.Properties.PropertyNames)
  27.       {
  28.         DataRow dr = dt.NewRow();
  29.         dr["Name"] = PropertyName;
  30.         string valstring = "";
  31.         ResultPropertyValueCollection rvpc = result.Properties[PropertyName];
  32.         for (int i = 0; i < rvpc.Count; i++)
  33.         {
  34.             valstring += o.ToString() + "\r\n";
  35.         }
  36.         dr["Value"] = valstring;
  37.         dt.Rows.Add(dr);
  38.       }
  39.  
  40.   }
  41.   catch (Exception ex)
  42.   {
  43.       throw new Exception("Error: " + ex.Message);
  44.   }
  45. }
  46.  
Then I looked at it in a DataGridView, there was nothing about "memberof" or "givenName"
I got it figured out. It was a stupid mistake on my part (go figure) ;) I somehow misunderstood the definition of givenName - it just happened to work for me because of what my username is and what my name is. That's why it wouldn't work for anyone else though - they weren't so fortunate. What I was really looking for there instead of givenName as my filter, was sAMAccountName - the login account as seen by AD. So after replacing that it works fine - my SystemInformation.Username matches up with the sAMAccountName, it filters by that so it's just looking at my AD object (or the AD object of whoever's logged in and using my app), and then iterates through the groups I belong to (at least the ones I directly belong to) and matches up the best one for this app. Thanks for your help on it, I appreciate it!

Maxx
May 20 '08 #5
Plater
7,872 Recognized Expert Expert
So did the MemberOf thing work for you?
I happen to know I'm in the DomainAdmins group but I saw no group memberships listed?
May 20 '08 #6
maxx233
32 New Member
So did the MemberOf thing work for you?
I happen to know I'm in the DomainAdmins group but I saw no group memberships listed?
Yeah, it's working good for me now. Loads the groups into 'result', and then iterates through them to check for membership. You'd probably have to edit the filtering I'm doing below (see notes in code), but other than that it should be good. Here's the relevant code I've got

Expand|Select|Wrap|Line Numbers
  1.         private void authenticateUser()
  2.         {
  3.             DirectoryEntry DE = new DirectoryEntry("LDAP://myDomain.com");
  4.             DirectorySearcher search = new DirectorySearcher();
  5.             search.SearchRoot = DE;
  6.             search.Filter = "(sAMAccountName=" + SystemInformation.UserName.ToString() + ")";
  7.             search.PropertiesToLoad.Add("memberOf");
  8.  
  9.             try
  10.             {
  11.                 SearchResult result = search.FindOne();
  12.                 int propertyCount = result.Properties["memberOf"].Count;
  13.                 string dn;
  14.                 for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
  15.                 {
  16.                     dn = (string)result.Properties["memberOf"][propertyCounter];
  17.                     try { dn = dn.Replace("CN=", ""); } catch { }
  18.                     try { dn = dn.Replace("OU=", ""); } catch { }
  19.                     try { dn = dn.Remove(dn.IndexOf(",")); } catch { }
  20.  
  21. // THE ABOVE MAY NEED TO BE EDITTED TO REFLECT YOUR ORGANIZATION'S AD STRUCTURE.  IT'S JUST FILTERING OUT ALL THE UNWANTED STRUCTURE/LOCATION INFO FOR THE GROUP NAMES
  22.  
  23.                 }
  24.             }
  25.             catch (Exception ex)
  26.             {
  27.                 throw new Exception("Error: " + ex.Message);
  28.             }
  29. }
May 21 '08 #7
Plater
7,872 Recognized Expert Expert
It did in fact work without me changing anything except inserting my own domain.
Out came all the groups I was registered in. The code knocked it down to only the first group, but stepping through showed me all of them.

Interesting, but now I've forgotten what you used this code for?
May 21 '08 #8
maxx233
32 New Member
It did in fact work without me changing anything except inserting my own domain.
Out came all the groups I was registered in. The code knocked it down to only the first group, but stepping through showed me all of them.

Interesting, but now I've forgotten what you used this code for?
I'm using it for authentication/setting permissions on a program we've got running downstairs.

The department that uses it has fairly high turnover, so I wanted to relieve the pain for everyone involved of having that department's managers maintain an integrated user DB for this program - so instead when we make the users' AD accounts upstairs in IT we now assign them as a member of a group in AD pertaining to their access level in this program (programX user, programX supervisor, programX manager, etc)... Voila! All the user has to do is login to their machine with a single account, and it checks group membership when they run this program, and assigns permissions within the program accordingly. Easy for us to administer, painless for department managers, less confusing for the users, and less paperwork.

Maxx
May 21 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Karen Hill | last post by:
I would like to be able to programatically check for group membership in an access database. For example, I would like to be able to check is someone is in the admin group before the program does...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
0
by: Jan Nielsen | last post by:
In ASP.Net I'm trying to check for some users membership of a group. The user is not nessicerily the user requesting the page, and I do not have the users password. So far I've created the...
2
by: Steve Oswald via DotNetMonster.com | last post by:
Hello! I need to get a list of all members of a specific group (whether or not the currently logged-in user is a member of that group) in a VB.NET codebehind page. I am able to get all the...
7
by: Sameh Ahmed | last post by:
Hello there IsInrole gives ya the means to check if the current or impersonated user belongs to a specific windows role or group. is there a way to do the same without using ADSI to check if...
17
by: TC | last post by:
In the past I always regarded user/group security as fairly tight. It is tricky to implement, but once implemented properly, it can't be cracked except through a dedicated effort. Recently,...
2
by: Bob | last post by:
I am developing an ASP.NET application that needs to archive documents and support the retrieval of them. When the document is stored, the user needs to be able to indicate whether it is a public...
3
by: Glenn | last post by:
My current classic-ASP site has users, projects, roles and the 2.0 membership looks like a perfect fit, but I'm having trouble finding examples of how to have users that belong to different...
4
by: Paul.Pucciarelli | last post by:
So I have some 'groups' which 'users' can join. There is no enrollment limit on these 'groups'. How should I store the list of users enrolled in the group? I'd like to be able to quickly...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.