473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Case sensitivity problem in using the Contains method in ResultPropertyValueCollection

I need to check whether a particular user is already a member of an Active
Directory Security Group.

The following code extract works but only if the user distinguished name is
exactly the same as that returned from Active Directory. For example using
'cn=' in the userdn string instead of 'CN=' does not work.

As far as I am aware Active Directory is not case sensitive and it is
therefore difficult to predict the case of a string.

Before I write code to extract all the strings (converted into either upper
or lower case) into a sorted list to be able to search for the user is there
a way to make the Contains method work more predictably?

The ResultPropertyValueCollection (result.Properties["member"]) contains
objects of type String.

DirectoryEntry parententry = new DirectoryEntry(LDAPParentPath);
DirectoryEntry groupentry = parententry.Children.Find(user.Group);
DirectorySearcher ds = new DirectorySearcher(groupentry);
SearchResult result;
ds.PropertiesToLoad.Add("member");
result = ds.FindOne();
if (result != null)
{
String userdn = String.Format("CN={0},{1}", user.User_logon_name,
user.Ldap_container);
if (!result.Properties["member"].Contains(userdn)) // NB Case Sensitive
{
// add user to group
groupentry.Properties["member"].Add(userdn);
groupentry.CommitChanges();
}
}
Mar 14 '07 #1
8 11959
Hello,

The obkect result.Properties["member"] is actually a ArryList object, its
Contains() method is Case sensitive here. Instead of the Contains method, I
think you may loop very items in this arraylist and compare lowercase value
of the items with your "userdn"'s lower case value.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 15 '07 #2
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:e2**************@TK2MSFTNGP05.phx.gbl...
>I need to check whether a particular user is already a member of an Active Directory
Security Group.

The following code extract works but only if the user distinguished name is exactly the
same as that returned from Active Directory. For example using 'cn=' in the userdn string
instead of 'CN=' does not work.

As far as I am aware Active Directory is not case sensitive and it is therefore difficult
to predict the case of a string.

Before I write code to extract all the strings (converted into either upper or lower case)
into a sorted list to be able to search for the user is there a way to make the Contains
method work more predictably?

The ResultPropertyValueCollection (result.Properties["member"]) contains objects of type
String.

DirectoryEntry parententry = new DirectoryEntry(LDAPParentPath);
DirectoryEntry groupentry = parententry.Children.Find(user.Group);
DirectorySearcher ds = new DirectorySearcher(groupentry);
SearchResult result;
ds.PropertiesToLoad.Add("member");
result = ds.FindOne();
if (result != null)
{
String userdn = String.Format("CN={0},{1}", user.User_logon_name,
user.Ldap_container);
if (!result.Properties["member"].Contains(userdn)) // NB Case Sensitive
{
// add user to group
groupentry.Properties["member"].Add(userdn);
groupentry.CommitChanges();
}
}

result.Properties["member"] returns the RDN in a directory attribute in upper case form, so
you need to use uppercased RDN's when comparing.
Howevr, there is no need to make it that complicated.

DirectoryEntry groupentry = parententry.Children.Find(user.Group);
String userdn = String.Format("CN={0}", user.User_logon_name);
DirectoryEntry tmpEntry;
try
{
tmpEntry = container.Children.Find(userdn , user.Ldap_container); //Find is case
agnostic, userdn may look like cn=...
}
finally
{
if(tmpEntry == null)
// no such entry found, add entry to container.
else
tmpEntry.Dispose();
}
.....

Willy.

RDN = "relative distinguished name" like DC, NC, E etc...
Mar 15 '07 #3
Thanks Willy
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:e2**************@TK2MSFTNGP05.phx.gbl...
>>I need to check whether a particular user is already a member of an Active
Directory Security Group.

The following code extract works but only if the user distinguished name
is exactly the same as that returned from Active Directory. For example
using 'cn=' in the userdn string instead of 'CN=' does not work.

As far as I am aware Active Directory is not case sensitive and it is
therefore difficult to predict the case of a string.

Before I write code to extract all the strings (converted into either
upper or lower case) into a sorted list to be able to search for the user
is there a way to make the Contains method work more predictably?

The ResultPropertyValueCollection (result.Properties["member"]) contains
objects of type String.

DirectoryEntry parententry = new DirectoryEntry(LDAPParentPath);
DirectoryEntry groupentry = parententry.Children.Find(user.Group);
DirectorySearcher ds = new DirectorySearcher(groupentry);
SearchResult result;
ds.PropertiesToLoad.Add("member");
result = ds.FindOne();
if (result != null)
{
String userdn = String.Format("CN={0},{1}", user.User_logon_name,
user.Ldap_container);
if (!result.Properties["member"].Contains(userdn)) // NB Case
Sensitive
{
// add user to group
groupentry.Properties["member"].Add(userdn);
groupentry.CommitChanges();
}
}


result.Properties["member"] returns the RDN in a directory attribute in
upper case form, so you need to use uppercased RDN's when comparing.
Howevr, there is no need to make it that complicated.

DirectoryEntry groupentry = parententry.Children.Find(user.Group);
String userdn = String.Format("CN={0}", user.User_logon_name);
DirectoryEntry tmpEntry;
try
{
tmpEntry = container.Children.Find(userdn , user.Ldap_container); //Find
is case agnostic, userdn may look like cn=...
}
finally
{
if(tmpEntry == null)
// no such entry found, add entry to container.
else
tmpEntry.Dispose();
}
....

Willy.

RDN = "relative distinguished name" like DC, NC, E etc...


Mar 16 '07 #4
Thanks

Willys' suggestion looks like it will meet my needs

Chris Noble

"Luke Zhang [MSFT]" <lu******@online.microsoft.comwrote in message
news:KE*************@TK2MSFTNGHUB02.phx.gbl...
Hello,

The obkect result.Properties["member"] is actually a ArryList object, its
Contains() method is Case sensitive here. Instead of the Contains method,
I
think you may loop very items in this arraylist and compare lowercase
value
of the items with your "userdn"'s lower case value.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Mar 16 '07 #5
Willy

I have had a go with this but I am having a problem with the code you
provided in testing for a null instance of tmpEntry.
The line if(tmpEntry == null) will not compile without producing a 'use of
unassigned local variable' error
DirectoryEntry groupentry = parententry.Children.Find(user.Group);
String userdn = String.Format("CN={0}", user.User_logon_name);
DirectoryEntry tmpEntry;
try
{
tmpEntry = container.Children.Find(userdn , user.Ldap_container); //Find
is case agnostic, userdn may look like cn=...
}
finally
{
if(tmpEntry == null)
// no such entry found, add entry to container.
else
tmpEntry.Dispose();
}

Any suggestions

Chris
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:e2**************@TK2MSFTNGP05.phx.gbl...
>>I need to check whether a particular user is already a member of an Active
Directory Security Group.

The following code extract works but only if the user distinguished name
is exactly the same as that returned from Active Directory. For example
using 'cn=' in the userdn string instead of 'CN=' does not work.

As far as I am aware Active Directory is not case sensitive and it is
therefore difficult to predict the case of a string.

Before I write code to extract all the strings (converted into either
upper or lower case) into a sorted list to be able to search for the user
is there a way to make the Contains method work more predictably?

The ResultPropertyValueCollection (result.Properties["member"]) contains
objects of type String.

DirectoryEntry parententry = new DirectoryEntry(LDAPParentPath);
DirectoryEntry groupentry = parententry.Children.Find(user.Group);
DirectorySearcher ds = new DirectorySearcher(groupentry);
SearchResult result;
ds.PropertiesToLoad.Add("member");
result = ds.FindOne();
if (result != null)
{
String userdn = String.Format("CN={0},{1}", user.User_logon_name,
user.Ldap_container);
if (!result.Properties["member"].Contains(userdn)) // NB Case
Sensitive
{
// add user to group
groupentry.Properties["member"].Add(userdn);
groupentry.CommitChanges();
}
}


result.Properties["member"] returns the RDN in a directory attribute in
upper case form, so you need to use uppercased RDN's when comparing.
Howevr, there is no need to make it that complicated.

DirectoryEntry groupentry = parententry.Children.Find(user.Group);
String userdn = String.Format("CN={0}", user.User_logon_name);
DirectoryEntry tmpEntry;
try
{
tmpEntry = container.Children.Find(userdn , user.Ldap_container); //Find
is case agnostic, userdn may look like cn=...
}
finally
{
if(tmpEntry == null)
// no such entry found, add entry to container.
else
tmpEntry.Dispose();
}
....

Willy.

RDN = "relative distinguished name" like DC, NC, E etc...


Apr 18 '07 #6
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:OZ**************@TK2MSFTNGP02.phx.gbl...
Willy

I have had a go with this but I am having a problem with the code you provided in testing
for a null instance of tmpEntry.
The line if(tmpEntry == null) will not compile without producing a 'use of unassigned
local variable' error
DirectoryEntry groupentry = parententry.Children.Find(user.Group);
String userdn = String.Format("CN={0}", user.User_logon_name);
DirectoryEntry tmpEntry;
try
{
tmpEntry = container.Children.Find(userdn , user.Ldap_container); //Find is case
agnostic, userdn may look like cn=...
}
finally
{
if(tmpEntry == null)
// no such entry found, add entry to container.
else
tmpEntry.Dispose();
}

Any suggestions
Sorry my OE compiler did not catch this one ;-).

Make it ...
....
DirectoryEntry tmpEntry = null;;
try
{
...

Willy.

Apr 18 '07 #7
Willy
I spent too long writing C and C++ code and still assume unassigned
variables are automatically null.

My original method worked despite the case sensitivity problem but I am
still having problems with your suggestion.
The problem appears to be in using the line
' tmpEntry = container.Children.Find(userdn , user.Ldap_container); //Find
is case agnostic, userdn may look like cn=...'

No problems with the userdn but looking at the syntax for the Find method
the second parameter should be a schemaClassName.
container is the DirectoryEntry for a Security Group. I have tried "user" as
the schemaClassName but this does not work.
I can't see how this could work as the security group is not really a
container but it has properties one of which is 'members' that contains an
array of user names.
Am I missing something or do I need to back to my original approach and
solve the case problem

Chris
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:OZ**************@TK2MSFTNGP02.phx.gbl...
>Willy

I have had a go with this but I am having a problem with the code you
provided in testing for a null instance of tmpEntry.
The line if(tmpEntry == null) will not compile without producing a 'use
of unassigned local variable' error
DirectoryEntry groupentry = parententry.Children.Find(user.Group);
String userdn = String.Format("CN={0}", user.User_logon_name);
DirectoryEntry tmpEntry;
try
{
tmpEntry = container.Children.Find(userdn , user.Ldap_container);
//Find is case agnostic, userdn may look like cn=...
}
finally
{
if(tmpEntry == null)
// no such entry found, add entry to container.
else
tmpEntry.Dispose();
}

Any suggestions

Sorry my OE compiler did not catch this one ;-).

Make it ...
...
DirectoryEntry tmpEntry = null;;
try
{
...

Willy.

Apr 19 '07 #8
Willy

Problem solved now. I used the Find method (case insensitive) to find the
security group directoryentry.

DirectoryEntry groupentry==null;
try
{
groupentry = parententry.Children.Find(GroupName); // case insensitive
}
finally
{
if (groupentry != null)
{
etc ......

If the groupentry is valid, then to check whether the user is already member
of the security group I can read the distinguishedName property for the user
and then search the member properties of the security group directoryentry

DirectorySearcher ds = new DirectorySearcher(groupentry);
SearchResult result;
ds.PropertiesToLoad.Add("member");
result = ds.FindOne();
if (result != null)
{
String userdn = entry.Properties["distinguishedName"].Value.ToString();
// Correct Case as stored in Active Directory
if (!result.Properties["member"].Contains(userdn)) // NB Case Sensitive
{
// add user to group
groupentry.Properties["member"].Add(userdn);
groupentry.CommitChanges();
}
}

Thanks for your help

Chris

"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:uB**************@TK2MSFTNGP04.phx.gbl...
Willy
I spent too long writing C and C++ code and still assume unassigned
variables are automatically null.

My original method worked despite the case sensitivity problem but I am
still having problems with your suggestion.
The problem appears to be in using the line
' tmpEntry = container.Children.Find(userdn , user.Ldap_container); //Find
is case agnostic, userdn may look like cn=...'

No problems with the userdn but looking at the syntax for the Find method
the second parameter should be a schemaClassName.
container is the DirectoryEntry for a Security Group. I have tried "user"
as the schemaClassName but this does not work.
I can't see how this could work as the security group is not really a
container but it has properties one of which is 'members' that contains an
array of user names.
Am I missing something or do I need to back to my original approach and
solve the case problem

Chris
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:OZ**************@TK2MSFTNGP02.phx.gbl...
>>Willy

I have had a go with this but I am having a problem with the code you
provided in testing for a null instance of tmpEntry.
The line if(tmpEntry == null) will not compile without producing a 'use
of unassigned local variable' error
DirectoryEntry groupentry = parententry.Children.Find(user.Group);
String userdn = String.Format("CN={0}", user.User_logon_name);
DirectoryEntry tmpEntry;
try
{
tmpEntry = container.Children.Find(userdn , user.Ldap_container);
//Find is case agnostic, userdn may look like cn=...
}
finally
{
if(tmpEntry == null)
// no such entry found, add entry to container.
else
tmpEntry.Dispose();
}

Any suggestions

Sorry my OE compiler did not catch this one ;-).

Make it ...
...
DirectoryEntry tmpEntry = null;;
try
{
...

Willy.


Apr 20 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Elliot Temple | last post by:
Hi I have two questions. Could someone explain to me why Python is case sensitive? I find that annoying. Also, why aren't there multiline comments? Would adding them cause a problem of some...
761
by: Neo-LISPer | last post by:
Hey Recently, I researched using C++ for game programming and here is what I found: C++ game developers spend a lot of their time debugging corrupted memory. Few, if any, compilers offer...
16
by: Starwiz | last post by:
I'm a VB.net programmer, and I'm about to start working with two C++ programmers and teach them .net. I've decided to use C# in teaching them, since it's similar enough to VB.net that I can read...
5
by: Jeff S | last post by:
I'm getting started with C# and was just wondering if C# veterans LIKE the fact that the language is case sensitive. If so, how is case sensitivity helpful? Do you create multiple variables,...
14
by: Christian Sell | last post by:
Hello, I am running into a problem with PGs case sensitivity with regard to column and table names. I am using program components that require the object names returned from database metadata...
15
by: gregory_may | last post by:
Is there any options in VS 2005 to better handle case issues in C# (Similar to VB.Net)?
3
by: Anita Potekkat | last post by:
Hello, I had a question regarding Case Sensitivity in 10g & 9i. (1) Does Case Sensitivity in Oracle have to do with data only? Or does it also effect table & column names? For e.g. in a table...
2
by: Lucky | last post by:
Hi guys, I'm having problem with case sensitive collation of SQL Database. one my client is having case sensitive database. While developing the Data Layer i didn't consider this scenario. the...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.