0

I'm going to show my ignorance about how using statements work in c# I think.

I am trying to write a function that takes in unique identifiers for a user in active directory and returns that user. I then want to make changes to the user and commit them.

I suspect this isn't working because I'm returning in a using block.

Here is the basic idea:

public static DirectoryEntry GetADUser( string prop1Value, string prop2Value )
{
    using( var rootDE = new DirectoryEntry(LDAPPath, User, Password))
    {
        using( var searcher = new DirectorySearcher(rootDE))
        {
            searcher.Filter = string.Format("(&(prop1={0})(prop2={1}))", prop1Value, prop2Value);
            var user = searcher.FindOne().GetDirectoryEntry();

            return user;
        }
    }
}

//...

var user = GetADUser("val1","val2");

user.Properties["prop3"].Value = "Spagetti";
user.CommitChanges();

Would that work? It doesn't seem like active directory is showing changes I make in that way. I'm not getting any exceptions when calling commit changes.

This is related to: Is it OK doing a return from inside using block and What happens when 'return' is called from within a 'using' block?.

If it won't work this way, how bad could it get if I rewrote that function without the using blocks?

Community
  • 1
  • 1
249076
  • 650
  • 12
  • 22
  • Did you verify that the usings are the problem? Are your changes reflected in the AD if you put the two lines that change and commit the user inside the usings? – Daniel Hilgarth Jan 23 '12 at 16:57
  • I must have been doing something else wrong. It looks like this code works fine. – 249076 Jan 23 '12 at 19:13

2 Answers2

0

Don't hard code the Value Spagetti also if user.Properties["prop3"].Value gives and error try

 (string)user.Properties["prop3"] = some variable if it's type is a string or 
  user.Properties["prop3"].Value = someVariable.ToString() 

if its an integer cast as integer does this make sense..? you will not be able to access user if it's not inside of the using in regards to the commit.. so move the commit inside the using..

MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

declare your user as a SearchResult outside of the using blocks then assign it in the using(var searcher....) block then put your return statement after the end of the using block

i.e

SearchResult user = null;
using( var rootDE = new DirectoryEntry(LDAPPath, User, Password))
{
    using( var searcher = new DirectorySearcher(rootDE))
    {
        searcher.Filter = string.Format("(&(prop1={0})(prop2={1}))", prop1Value, prop2Value);
        var user = searcher.FindOne().GetDirectoryEntry();


    }
}
return user;

you can also streamline things a bit by changing the searcher using block

using (var searcher = new DirectorySearcher(rootDD, string.Format("(&(prop1={0})(prop2={1}))", prop1Value, prop2Value))
{
    user = searcher.FindOne();
}
Brian
  • 2,229
  • 17
  • 24
  • This post was the most hopefull. However it looks like my original code is fine. I was trying to update the thumbnailPhoto field, and I think something else was wrong with it. It looks like you can return from within a using and still make changes to the DirectoryEntry and commit them – 249076 Jan 24 '12 at 15:10
  • yes. the way you have it written where you can access the user object within the using statement. You're doing it a little differently with the directory searcher assigning directly to the var user vs declaring user as a searchresult. The catch I've always run in to is when you commit the changes. If you're still having problems, verify that the ldap connection is working and verify that the property name matches up to the ldap property with the case and spelling. The rest of the code looks ok – Brian Jan 24 '12 at 15:16