Friday, February 15, 2008

Use Active Directory to Authenticate

Today I spent sometime to review the System.DirectoryServices name space in .Net 2.0. It is very convenient to use it to access A.D. and doing Authentication for your apps. Here are some sample codes in C# you might need to start with:

//Variables for getting groups
string _path;
string _filterAttr;

bool validateUser(string username, string password)
{
//Validate Users
string path = "LDAP:// your domain";
string domainUserName = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(path, domainUserName, password);

try
{
// Bind to the native object to force authentication to happen
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (result != null)
{
// Authenticated
_path = result.Path;
_filterAttr = result.Properties["cn"][0].ToString();
return true;
}
else
return false;
}
catch (Exception ex)
{
throw new Exception("User not authenticated: " + ex.Message);
}
}

//Getting the groups this user belongs to
string getUserGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttr + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groups = new StringBuilder();

try
{
SearchResult result = search.FindOne();

int nCnt = result.Properties["memberOf"].Count;

for (int i = 0; i < nCnt; i++)
{
string dn = result.Properties["memberOf"][i].ToString();
int eidx = dn.IndexOf("=", 1);
int cidx = dn.IndexOf(",", 1);

if (eidx == -1)
{
return string.Empty;
}

groups.Append(dn.Substring(eidx + 1, cidx - eidx - 1));
groups.Append("|");
}
}
catch(Exception ex)
{
throw new Exception("Groups not getting: " + ex.Message);
}
return groups.ToString();
}

Wednesday, February 13, 2008

Referenced assembly System.Deployment is not installed for ClickOnce?

My computer has been complaining for "Referenced assembly System.Deployment is not installed..." whenever trying to install any ClickOnce apps. And those apps were not installed successfully since sometime. Here are the solutions I found from msdn forum. It worked very well after I delete the ClickOnce store folder.

1. Run "Mage -cc". (Mage.exe can be found in the .NET 2.0 SDK)

2. Uninstall the application via Add/Remove Programs and reinstall it

3. If neither of these work then the last option is to delete the ClickOnce store and get back to clean state. To delete the ClickOnce store delete the folder "%userprofile%\Local Settings\Apps".

Monday, February 11, 2008

Use Authorization Manager (AzMan) with ASP.NET 2.0

It is very convenient to use AuthorizationStoreRoleProvider with ActiveDirectoryMembershipProvider for ASP.net application authentication and security control. But there are a few tricks you need to know when developing and deploying. Here are the useful links:

How To: Use Authorization Manager (AzMan) with ASP.NET 2.0


When I tried to deploy the app to Windows 2003 Service Pack 1, it didn't work. I found out it needs to apply this fix (KB915786) to make it work.

Friday, February 08, 2008