Monday, September 11, 2006

Set the Account Expiry property in LDAP

In many forums, I have found the question of how to set the AccountExpiry property in LDAP. I saw these when I was facing such a similar issue. Most of the forums did not mention much or those which mentioned something were incomplete. That is why I thought of blogging my solution.

This piece of code, is the one which I use to create a temporary user for my application. This can be helpful when you want to create a user whose account will expire after a certain period of time. Here I set it as 90 days.

The Code...


void SetAccountExpiry(string UserName)
{
DirectoryEntry UserDE = new DirectoryEntry(LDAPPath, LDAPUserName, LDAPPwd);

DirectorySearcher oSearch = new DirectorySearcher(UserDE);
oSearch.Filter = "(SAMAccountName=" + UserName + ")";
SearchResult oResult = oSearch.FindOne();
UserDE = oResult.GetDirectoryEntry();

DateTime accExp = DateTime.Now.AddDays(90);
int64 accExpNum = accExp.ToFileTime();
UserDE.Properties["accountExpires"].Value = GetLargeinteger(accExpNum);
UserDE.CommitChanges();
}


IADsLargeinteger GetLargeinteger(long val)
{
IADsLargeinteger largeint = new Largeinteger();
largeint.HighPart = (int) (val >> 32);
largeint.LowPart = (int) (val & 0xFFFFFFFF);
return largeInt;
}



Hope this code was helpful. Comments and suggestions are always welcome.

Thursday, September 07, 2006

Creating Custom App Settings in Web.Config

Most of the application requires lots of settings to be specified in the App Settings. Unless you manage that in a neat way, it will soon become a headache for you.

.Net helps you out in this situation by allowing you to have custom AppSettings in your web.config. This will help you to group your appSettings in a more efficient manner so that management becomes much easier.

In this blog, I have a sample code which lets you to retrieve the values from the custom AppSettings tag in your web.config.

The Code....


// Usage
string MyKey = GetSettings ("Mykey");

// The method
private string GetSettings(string Key)
{
System.Collections.Specialized.NameValueCollection obj = (System.Collections.Specialized.NameValueCollection) System.Configuration.ConfigurationSettings.GetConfig("MySettings");
return obj[Key].ToString();
}



The Web.config...

<!-- Defining a new section -->

<configSections>
<section name="MySettings"
type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>

<!-- The New Section -->

<MySettings>
<add key="Mykey" value="MyKeyValue" />
</MySettings>