If you are using FomrsAuthentication against ActiveDirectory you might want to search if a User Id exist in Active Directory or not before taking certain action like creating new profile.
Here is a Code Snippet used to Check for existance of a User in Active Directory. Some of the setting are stored in web.config.
Note: IT is highly recommended that you do not store usernames or passwords in web.config unencrypted here for demonstration I have put them in clear text to connect to AD.
ConfigurationSettings.AppSettings are read from web.config and you can add your own app settings for those variables.
public bool IsUserExistInActiveDirectory(string UserId)
{
bool IsValidLoginName = false;
string domain = "LDAP://" + ConfigurationSettings.AppSettings["Domain"];
System.DirectoryServices.DirectoryEntry entry =
new DirectoryEntry(domain, ConfigurationSettings.AppSettings["ADServiceAccount"], ConfigurationSettings.AppSettings["ADServiceAccountPassword"],
AuthenticationTypes.Secure);
DirectorySearcher adSearcher = new DirectorySearcher(entry);
adSearcher.SearchScope = SearchScope.Subtree;
adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + UserId + "))";
SearchResult oResult = adSearcher.FindOne();
if ( oResult != null)
{
IsValidLoginName = true;
}
return IsValidLoginName;
}