Tuesday, June 9, 2009

Reduce LDAP connection creation

I have a problem when my application runs to do a high-volumn LDAP operation.

This is the original codes. This code creation a lot of TCP port to AD with "TIME WAIT" status.

private void AddToGroup(UserPrincipal currentUser, GroupPrincipal group)
{
using (DirectoryEntry dirEntry = new DirectoryEntry(string.Format("LDAP://{0}/{1}", group.Context.ConnectedServer, group.DistinguishedName)))
{
dirEntry.Properties["member"].Add(currentUser.DistinguishedName);
dirEntry.CommitChanges();
dirEntry.Close();
dirEntry.Dispose();
}

}

In order to reduce the LDAP connection creation from using SDS.AM. We could use the GetUnderlyingObject() method to get the corresponding DirectoryEntry object of the UserPrincipal or GroupPrincipal. Using this way, my application could finish its task without error. Here is the new codes to do the same thing.

private void AddToGroup(UserPrincipal currentUser, GroupPrincipal group)
{
using (DirectoryEntry gEntry = (DirectoryEntry)group.GetUnderlyingObject())
{
gEntry.Properties["member"].Add(currentUser.DistinguishedName);
gEntry.CommitChanges();
}
}

Here is a reference from Microsoft. http://msdn.microsoft.com/en-us/library/bb924562.aspx

Saturday, June 6, 2009

No wrap for Telerik RadGrid cell content

Put the cell content between < nobr></nobr>

Get Mime Type from IIS

Except the System.DirectoryServices, I also need to add a reference of com object "Active DS IIS Namespace Provider" in VS.

using System.DirectoryServices;

public static string GetMimeType(string extension)
{
string retMimeType = null;

using (DirectoryEntry mimeMap = new DirectoryEntry("IIS://Localhost/MimeMap"))
{
PropertyValueCollection pValues = mimeMap.Properties["MimeMap"];

foreach (object value in pValues)
{
IISOle.IISMimeType mimeType = (IISOle.IISMimeType)value;

if (extension == mimeType.Extension)
{
retMimeType = mimeType.MimeType;
}
}

}
return retMimeType;
}