Saturday, January 30, 2010

Favorite Extension Methods




I thought I’d share some of my favorite extension methods I use regularly. I’ll go through a few extensions for MailMessage, HtmlHelper, and DirectoryEntry (for Active Directory).

MailMessage Extension (only 1)

    public static class MailMessageExtensions
{
public static void Send(this MailMessage mailMessage)
{
#if (DEBUG)
mailMessage.To.Clear();
mailMessage.To.Add(
WebConfig.FromEmail);
#endif

var sc = new SmtpClient();
sc.Send(mailMessage);
}
}

HtmlHelper (only 1)

        public static string DivSuccessMessage(this HtmlHelper html, string successMessage)
{
if (html.ViewData.ModelState.IsValid && html.ViewData["success"] != null)
return "<div class=\"success-message\">" + successMessage + "</div>";

return "";
}

I created this extension to act similar to the ValidationSummary extension. Basically it checks for ViewData[“success”] and that the model state is valid then returns the sucess message, otherwise it returns nothing.


DirectoryEntry


Need a property? Here you go, just give me the name. (I also use an enum for our AD schema too to pass in the propertyName)

public static string GetProperty(this DirectoryEntry entry, string propertyName)
{
if (entry.Properties[propertyName] == null || entry.Properties[propertyName].Value == null)
return null;

return entry.Properties[propertyName].Value.ToString();
}

Need to add a property? Done…it’ll even commit the changes.

public static void AddProperty(this DirectoryEntry entry, string propertyName, string propertyVal, bool commitChange)
{
if (entry.Properties[propertyName] == null || propertyVal == null)
return;

entry.Properties[propertyName].Value = propertyVal;
if (commitChange)
entry.CommitChanges();
}

I have a couple more for adding properties…one that adds just a property and commits without asking and then one that adds multiple properties at once.

public static void AddProperty(this DirectoryEntry entry, string propertyName, string propertyVal)
{
AddProperty(entry, propertyName, propertyVal,
true);
}

public static void AddProperties(this DirectoryEntry entry, params Func<string, string>[] values)
{
foreach (var func in values)
AddProperty(entry, func.Method.GetParameters()[
0].Name, func(null), true);
}

Here’s one to enable an account

public static void EnableAccount(this DirectoryEntry entry)
{
if (!ValidUserAccount(entry))
return;

entry.Properties[
Attributes.userAccountControl].Value = GetCurrentUserAccountControl(entry) & Flags.ADS_UF_NORMAL_ACCOUNT;
entry.CommitChanges();
}

private static int GetCurrentUserAccountControl(DirectoryEntry entry)
{
int currentVal;
return !int.TryParse(entry.Properties[Attributes.userAccountControl].Value.ToString(), out currentVal) ? 0 : currentVal;
}

private static bool ValidUserAccount(DirectoryEntry entry)
{
return entry.SchemaClassName.Equals("user", StringComparison.CurrentCultureIgnoreCase);
}

Here’s one to disable an account

public static void DisableAccount(this DirectoryEntry entry)
{
if (!ValidUserAccount(entry))
return;

entry.SetPassword(
"SomeDefaultPassword");
entry.Properties[
Attributes.userAccountControl].Value = GetCurrentUserAccountControl(entry) | Flags.ADS_UF_ACCOUNTDISABLE;
entry.CommitChanges();
}

Another one to reset password

public static void SetPassword(this DirectoryEntry entry, string password)
{
if (!ValidUserAccount(entry))
return;

entry.Invoke(
"SetPassword", new object[] {password});
entry.CommitChanges();
}

There you go…a few extensions that I use and thought I’d share. I know you all have some good ones so feel free to share.


Thanks for reading!


Shout it

kick it on DotNetKicks.com

blog comments powered by Disqus
Related Posts Plugin for WordPress, Blogger...