Thursday, July 9, 2009

Active Directory in .NET: Recursively find all groups for a user

I had to write a function which returns all the groups (even if they are nested) for a user.
A good thing I discovered the System.DirectoryServices.AccountManagement namespace.

Take a look at the code snippet. I think it's pretty readable.. What do you think?



The GetListOfGroupsRecursively() function tries to find a Principal object by the Username of the ADUser parameter. If the Principal object is found, we call the GetGroups() function and store it's return value in a PrincipalSearchResult. If there are searchresults, we try to get the members for each group by calling the GetMembersForGroup() method. This method recursively searches through all the members of the group looking for Principal objects where the StructuralObjectClass equals "group".

Tuesday, July 7, 2009

Active Directory in .NET: Getting started (A better version - Using the AccountManagement namespace)

A few days ago I blogged on Active Directory in .NET: Getting Started.

In that post I stated that everything you need to query the Active Directory in .NET lives in the System.DirectoryServices, well that was a lie. .NET Framework 3.5 introduced the System.DirectoryServices.AccountManagement namespace.

I learned about this namespace today, and wow, it really is much better. I have rewritten our own Active Directory wrapper in less then a few hours.

All you need to know in general is that everything you do using the AccountManagement namespace needs to run under a PrincipalContext.



Let me show you two basic methods..

I commented out the original method which uses the DirectoryServices namespace. Can you see the difference?

Authenticate a user



Get the DisplayName of a user



The code hardly needs any explanation. Hope it helps!

Sunday, July 5, 2009

Active Directory in .NET: Getting started

Note: If you are using .NET Framework 3.5 or better, check out this post for querying the Active Directory!

Working with the Active Directory is one of the things that every .NET or ASP.NET will face in his career. Most of the time you need information from the Active Directory. Editing, inserting or removing objects from a .NET (web)application is very rare, mostly because a System Administrator is managing the Active Directory.

Let me show you how to get started on getting information out of the Active Directory.

The basics

Everything you need to work with the Active Directory lives in the System.DirectoryServices namespace.

The most important classes you will need are:
Directory Searcher
Directory Entry
Search Result

So I advice you reading through the documentation of these classes.

Getting familiar with LDAP queries

I strongly advice you to try LDP.EXE

Ldp.exe is a Windows 2000 Support Tools utility you can use to perform Lightweight Directory Access Protocol (LDAP) searches against the Active Directory for specific information given search criteria. This also allows administrators to query data that would otherwise not be visible through the Administrative tools included in the product. All data that is returned in LDP queries, however, is subject to security permissions.




This tool will help you understand LDAP queries. Go through this article in the Microsoft Knowledge Base to get started.

Tuesday, June 23, 2009

Searching MSDN with FireFox and IE

Did you ever notice this linkbutton on the MSDN site?



I didn't, until today. I tested it with Mozilla FireFox 3.0.11 and Internet Explorer 7, and it worked with both browsers.



This might help you save a few seconds a day ;)

Monday, June 22, 2009

GridView with Sort Indication: Change backgroundColor of row Onmouseover

I blogged on writing a custom control which inherits from the GridView to support sort indication here.

Today I added another feature: Change the color of a row when you hover your mouse over the row.

I added two extra properties: OnMouseOverBackgroundColor and OnMouseOutBackgroundColor. All it took were four lines of code in the OnRowCreated method.



Adding simple javascript attributes did the trick.

This is the result.



Learn more about ASP.NET Controls in the book Pro ASP.NET 3.5 Server Controls and AJAX Components.

Sunday, June 21, 2009

GridView with Sort Indication: Change forecolor headercolumn on sorting

This is something which gets asked pretty often by customers: 'Can you add some kind of sort indication to the headers of this column?'.

I've seen a few solutions on the web. Some use CSS to fix this, some handle the OnRowCreated event.. These are both good solutions, but these solutions aren't very reusable and might end up in a copy-paste fest.

Because I'm reading Pro ASP.NET 3.5 Server Controls and AJAX Components, I thought it would be a good idea to create a custom control which inherits from the GridView to put a chapter of the book into practice.

I extended the GridView so you can set a HeaderColorSortedColumn. I added the property HeaderColorSortedColumn and overrode the OnRowCreated method.



The most important part in this code snippet is finding the LinkButton and comparing it's CommandArgument to the SortExpression of the GridView.

I implemented my custom control in default.aspx by adding the register tag on top of the page.



And here is the result..



Next up is adding an image to the headers.

Monday, June 8, 2009

Introducing Microsoft SQL Server 2008 (Free E-book)

Microsoft Press is releasing this E-book for free! All you need is a Windows Live ID.



You can download it here!

I haven't read it yet, but when I scrolled through the introduction the title "T-SQL: Still here to stay" caught my eye. This is something I was wondering about a while ago.. Should we still be using stored procedures? If even Microsoft is still using stored procedures, we can be pretty sure we should.

A while ago I asked a few professionals their opinion on stored procedures. You can find that article here. What are your thoughts?

I hope you enjoy this free e-book!