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!
Plaсe it intο your Sales lettег, οr haνе some fun since уοu can't win. If a sales manager has made research and is convinces about the professionalism and solid market status of a potential investor. The market business seems to be.
ReplyDeleteFeel free to surf to my weblog; internet marketing online course
On an accrual basis and small pay per click service providers can do all those things for multiple client for a fee.
ReplyDeleteHere is my blog: boston search engine optimization
Read their HTML source code. Because it can give you experience and can open you up
ReplyDeleteto other web-related career paths such as Internet marketing and online coaching.
The monetisation of another of Google's well-worn advertising platforms has led to the ice cracking under many websites tried and tested methods and practices employed by Web Search Engine firms aimed at increasing prominence. Preventive crawling is another weapon Web Search Engine firms have created it attainable for the insufficient guy to beat pricey advert endeavors.
Look into my blog; search engine placement specialist