Click here to Skip to main content
15,891,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I am using windows 2008 R2 Server and trying to Add a user in active directory.

I am able to save user ID of length less than 20 Characters. But when I am trying to increase this value to 30 characters then I am getting the error.
C#
"System.DirectoryServices.DirectoryServicesCOMException (0x8007001F): A device attached to the system is not functioning. (Exception from HRESULT: 0x8007001F)"

This error when I searched on net give me various links saying that:

(a) Please verify if the issue is caused by the length of sAMAccountName

(b) The document of SAM-Account-Name also indicates that the length of it should be less than 20 characters.

I am using the code below to add user in Active Directory
C#
public static void AddUser(ADUser adUser)
{
       if (_logger.IsDebugEnabled)
          _logger.Debug("ADHelper.cs: Enter AddUser");


         // Local variables
            DirectoryEntry oDE = null;
            DirectoryEntry oDENewUser = null;
            DirectoryEntries oDEs = null;

try
{
oDE = GetDirectoryEntry(GetADPath(adUser.UserType));

// 1. Create user account
oDEs = oDE.Children;
oDENewUser = oDEs.Add("CN=" + adUser.UserName, "user");

// 2. Set properties
SetProperty(oDENewUser, Constants.ADAttributes.givenName, adUser.FirstName);
SetProperty(oDENewUser, Constants.ADAttributes.initials, adUser.MiddleInitial);
SetProperty(oDENewUser, Constants.ADAttributes.sn, adUser.LastName);
SetProperty(oDENewUser, Constants.ADAttributes.mail, adUser.Email);
SetProperty(oDENewUser, Constants.ADAttributes.sAMAccountName, adUser.UserName);

SetProperty(oDENewUser, Constants.ADAttributes.ChallengeQuestion, adUser.PasswordChallengeQuestion);
SetProperty(oDENewUser, Constants.ADAttributes.ChallengeAnswer, adUser.PasswordChallengeAnswer);

SetProperty(oDENewUser, Constants.ADAttributes.ChallengeQuestion2, adUser.PasswordChallengeQuestion2);
SetProperty(oDENewUser, Constants.ADAttributes.ChallengeAnswer2, adUser.PasswordChallengeAnswer2);

// Sharepoint changes
if (adUser.CompanyGroupSupplier != string.Empty)
{
SetProperty(oDENewUser, Constants.ADAttributes.CompanyGroupSupplier, adUser.CompanyGroupSupplier);
}
if (adUser.PersonalGroupAddress != string.Empty)
{
SetProperty(oDENewUser, Constants.ADAttributes.PersonalGroupAddress, adUser.PersonalGroupAddress);
}
if (adUser.PersonalGroupPhone != string.Empty)
{
SetProperty(oDENewUser, Constants.ADAttributes.PersonalGroupPhone, adUser.PersonalGroupPhone);
}
// Sharepoint changes

oDENewUser.CommitChanges();

// 3. Set password
SetPassword(oDENewUser.Path, adUser.Password);

// 4. Enable account
EnableAccount(oDENewUser);

oDENewUser.Close();
oDE.Close();

if (_logger.IsDebugEnabled)
_logger.Debug("ADHelper.cs: Exit AddUser");

}
catch (ApplicationException appex)
{
if (_logger.IsErrorEnabled)
_logger.Error("ADHelper.cs: Exception occurred in AddUser. Message: ", appex);
throw appex;
}
catch (Exception ex)
{
if (_logger.IsErrorEnabled)
_logger.Error("ADHelper.cs: Exception occurred in AddUser. Message: ", ex);
throw ex;
}
finally
{
if (oDENewUser != null)
{
oDENewUser.Dispose();
oDENewUser = null;
}
if (oDEs != null)
{
oDEs = null;
}
if (oDE != null)
{
oDE.Dispose();
oDE = null;
}
}
}

Please any one can suggest me how can increase sAMAccountName length in Active Directory around 30 characters in length.

Thanks in Advance
Posted
Updated 15-Dec-11 23:26pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900