PC Review


Reply
Thread Tools Rate Thread

Active directory user creation with c# (question concerning principalUsername and samAccountName)

 
 
Daniel Knöpfel
Guest
Posts: n/a
 
      29th Jun 2006
Hello all

In our project we have been using the samAccount name to authenticate users
against the active directory. As the samAccountName is limited to 20
characters, we are going to use the userPrincipalName. Unfortunately, i
couldtnt make it work until now. I ve got a .Net programm that access the
active directory through the third party dll "Interop.ActiveDs.dll"
(namespace ActiveDs). The code to create the user with using the
samAccountName looks like this:

DirectoryEntry newUser = mDirectoryEntry.Children.Add("CN=" + pLoginName,
"user");

newUser.Properties["samAccountName"].Value = pLoginName;

newUser.CommitChanges();

//get native object of the new user and add user to group

IADsUser nativeNewUser = (IADsUser)newUser.NativeObject;

for (int i = 0; i < pGroups.Length; i++) {

DirectoryEntry group = mDirectoryEntry.Children.Find(pGroups[i], "group");

group.Properties["member"].Add(newUser.Properties["distinguishedName"].Value);

group.CommitChanges(); // In order to work in AD: Group Properties->Managed
By -> "Manager can update membership list : must be set

}

//set properties for the new user

nativeNewUser.SetPassword(pPassword);

nativeNewUser.AccountDisabled = false;

nativeNewUser.Put("userPrincipalName", pLoginName);

int currSettings = (int)nativeNewUser.Get("userAccountControl");

currSettings |= UF_PASSWD_CANT_CHANGE;

currSettings |= UF_DONT_EXPIRE_PASSWD;

nativeNewUser.Put("userAccountControl", currSettings);

newUser.CommitChanges();







Now what do i have to change to make it run with the principelUsername. Ive
tried several variations like assigning the principelUsername the same way
as the samAccountName in the example above, or assigning only with put. Can
anybody help me with this. I would be very grateful. Thanks in advance

Daniel





PS: to verify whether creation of a user has been successfull i use the
following code:

private bool CheckPassword(string pLoginName, string pPassword) {

try {

DirectoryEntry usr = new DirectoryEntry(mProviderUrl, pLoginName, pPassword,
AuthenticationTypes.Secure | AuthenticationTypes.ServerBind);

DirectorySearcher se = new DirectorySearcher(usr);

try {

SearchResult result = se.FindOne();

return true;

} catch(Exception ee) {

return false;

}

} catch(Exception exc) {

throw new Exception("Error while checking password for user " + pLoginName,
exc);

}

}














 
Reply With Quote
 
 
 
 
Daniel Knöpfel
Guest
Posts: n/a
 
      6th Jul 2006
Hallo

Der grund war, dass der userPrincipalName eine suffix hat. z.B.
(E-Mail Removed) . Dieser musste angegeben werden (nicht bei der
erstellung aber bei operationen)

Gruss Dani

"Daniel Knöpfel" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello all
>
> In our project we have been using the samAccount name to authenticate
> users against the active directory. As the samAccountName is limited to 20
> characters, we are going to use the userPrincipalName. Unfortunately, i
> couldtnt make it work until now. I ve got a .Net programm that access the
> active directory through the third party dll "Interop.ActiveDs.dll"
> (namespace ActiveDs). The code to create the user with using the
> samAccountName looks like this:
>
> DirectoryEntry newUser = mDirectoryEntry.Children.Add("CN=" + pLoginName,
> "user");
>
> newUser.Properties["samAccountName"].Value = pLoginName;
>
> newUser.CommitChanges();
>
> //get native object of the new user and add user to group
>
> IADsUser nativeNewUser = (IADsUser)newUser.NativeObject;
>
> for (int i = 0; i < pGroups.Length; i++) {
>
> DirectoryEntry group = mDirectoryEntry.Children.Find(pGroups[i], "group");
>
> group.Properties["member"].Add(newUser.Properties["distinguishedName"].Value);
>
> group.CommitChanges(); // In order to work in AD: Group
> Properties->Managed By -> "Manager can update membership list : must be
> set
>
> }
>
> //set properties for the new user
>
> nativeNewUser.SetPassword(pPassword);
>
> nativeNewUser.AccountDisabled = false;
>
> nativeNewUser.Put("userPrincipalName", pLoginName);
>
> int currSettings = (int)nativeNewUser.Get("userAccountControl");
>
> currSettings |= UF_PASSWD_CANT_CHANGE;
>
> currSettings |= UF_DONT_EXPIRE_PASSWD;
>
> nativeNewUser.Put("userAccountControl", currSettings);
>
> newUser.CommitChanges();
>
>
>
>
>
>
>
> Now what do i have to change to make it run with the principelUsername.
> Ive tried several variations like assigning the principelUsername the same
> way as the samAccountName in the example above, or assigning only with
> put. Can anybody help me with this. I would be very grateful. Thanks in
> advance
>
> Daniel
>
>
>
>
>
> PS: to verify whether creation of a user has been successfull i use the
> following code:
>
> private bool CheckPassword(string pLoginName, string pPassword) {
>
> try {
>
> DirectoryEntry usr = new DirectoryEntry(mProviderUrl, pLoginName,
> pPassword, AuthenticationTypes.Secure | AuthenticationTypes.ServerBind);
>
> DirectorySearcher se = new DirectorySearcher(usr);
>
> try {
>
> SearchResult result = se.FindOne();
>
> return true;
>
> } catch(Exception ee) {
>
> return false;
>
> }
>
> } catch(Exception exc) {
>
> throw new Exception("Error while checking password for user " +
> pLoginName, exc);
>
> }
>
> }
>
>
>
>
>
>
>
>
>
>
>
>
>
>



 
Reply With Quote
 
Daniel Knöpfel
Guest
Posts: n/a
 
      25th Jul 2006
Hello all

We have found a solution. The code listed below is actually correct. Just
accessing the user afterwards must be done differently as the
userPrincipalName has got an e-mail like suffix and the samAccountName a
prefix.

Greetings

Daniel

"Daniel Knöpfel" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello all
>
> In our project we have been using the samAccount name to authenticate
> users against the active directory. As the samAccountName is limited to 20
> characters, we are going to use the userPrincipalName. Unfortunately, i
> couldtnt make it work until now. I ve got a .Net programm that access the
> active directory through the third party dll "Interop.ActiveDs.dll"
> (namespace ActiveDs). The code to create the user with using the
> samAccountName looks like this:
>
> DirectoryEntry newUser = mDirectoryEntry.Children.Add("CN=" + pLoginName,
> "user");
>
> newUser.Properties["samAccountName"].Value = pLoginName;
>
> newUser.CommitChanges();
>
> //get native object of the new user and add user to group
>
> IADsUser nativeNewUser = (IADsUser)newUser.NativeObject;
>
> for (int i = 0; i < pGroups.Length; i++) {
>
> DirectoryEntry group = mDirectoryEntry.Children.Find(pGroups[i], "group");
>
> group.Properties["member"].Add(newUser.Properties["distinguishedName"].Value);
>
> group.CommitChanges(); // In order to work in AD: Group
> Properties->Managed By -> "Manager can update membership list : must be
> set
>
> }
>
> //set properties for the new user
>
> nativeNewUser.SetPassword(pPassword);
>
> nativeNewUser.AccountDisabled = false;
>
> nativeNewUser.Put("userPrincipalName", pLoginName);
>
> int currSettings = (int)nativeNewUser.Get("userAccountControl");
>
> currSettings |= UF_PASSWD_CANT_CHANGE;
>
> currSettings |= UF_DONT_EXPIRE_PASSWD;
>
> nativeNewUser.Put("userAccountControl", currSettings);
>
> newUser.CommitChanges();
>
>
>
>
>
>
>
> Now what do i have to change to make it run with the principelUsername.
> Ive tried several variations like assigning the principelUsername the same
> way as the samAccountName in the example above, or assigning only with
> put. Can anybody help me with this. I would be very grateful. Thanks in
> advance
>
> Daniel
>
>
>
>
>
> PS: to verify whether creation of a user has been successfull i use the
> following code:
>
> private bool CheckPassword(string pLoginName, string pPassword) {
>
> try {
>
> DirectoryEntry usr = new DirectoryEntry(mProviderUrl, pLoginName,
> pPassword, AuthenticationTypes.Secure | AuthenticationTypes.ServerBind);
>
> DirectorySearcher se = new DirectorySearcher(usr);
>
> try {
>
> SearchResult result = se.FindOne();
>
> return true;
>
> } catch(Exception ee) {
>
> return false;
>
> }
>
> } catch(Exception exc) {
>
> throw new Exception("Error while checking password for user " +
> pLoginName, exc);
>
> }
>
> }
>
>
>
>
>
>
>
>
>
>
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Active Directory User Creation Charlie Ting Microsoft Windows 2000 Active Directory 1 22nd Nov 2005 04:03 PM
windows user creation script in active directory.... uday via WinServerKB.com Microsoft Windows 2000 Active Directory 1 4th Jul 2005 05:36 PM
2K active directory -- new user account creation =?Utf-8?B?dGVsbmV0LXdvZXM=?= Microsoft Windows 2000 Active Directory 2 5th Feb 2005 06:13 PM
2K active directory -- new user account creation =?Utf-8?B?dGVsbmV0LXdvZXM=?= Microsoft Windows 2000 3 10th Nov 2004 04:04 AM
2K active directory -- new user account creation =?Utf-8?B?dGVsbmV0LXdvZXM=?= Microsoft Windows 2000 New Users 0 10th Nov 2004 01:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:21 PM.