Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
hi
I've problem with my code . there is an error shown when i'm trying to run my code to create a user into AD.

here is the error

logon failure unknown username or bad password.

here is the code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
 
namespace AD
{
    public partial class NewUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            //try
            //{
            DirectoryEntry adUserFolder = new DirectoryEntry("LDAP://IP/CN=Users,DC=TEST,DC=COM", "UserName", "Password");
 
                if (adUserFolder.SchemaEntry.Name == "container")
                {
                    DirectoryEntry newUser = adUserFolder.Children.Add("CN=" + username.Text, "User");
 
                    if (DirectoryEntry.Exists(newUser.Path))
                    {
                        Label1.Text = "user name is exists";
                    }
                    else
                    {
                        newUser.Properties["samAccountName"].Value = accountname.Text;
                        newUser.Properties["givenName"].Value = firstname.Text;
                        newUser.Properties["sn"].Value = lastname.Text;
                        newUser.Properties["initials"].Value = initials.Text;
                        newUser.Properties["siplayName"].Value = displayname.Text;
                        newUser.Properties["physicalDeliveryOfficeName"].Value = officename.Text;
                        newUser.Properties["telephoneNumber"].Value = phone.Text;
                        newUser.Properties["mail"].Value = Email.Text;
 
                        newUser.CommitChanges();
                        newUser.Invoke("setpassword", password.Text);
                        newUser.Properties["userAccountControl"].Value = 0x0200;
                        newUser.CommitChanges();
                        Label1.Text = "done!";
                    }
                }
            //}
            //catch
            //{
            //    Label1.Text = "wrong!";
            //}
 
        }
    }
} 


and i'm sure that the logon info are correct.
Posted
Updated 6-Jan-14 1:26am
v2
Comments
Are you providing the correct Username and Password?
Abdulmohsen Alshaya 6-Jan-14 6:56am    
yes i do . i check username and password 5 times

1 solution

Connect to AD using something like this:
var username = "your username";
var password = "your password";
var domain = "your domain";
var ctx = new PrincipalContext(ContextType.Domain, domain, username, password);


and then perform the operations using the PrincipalContext object.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Abdulmohsen Alshaya 6-Jan-14 7:25am    
could you make an example please? because i try it then get error in this line:

var ctx = new PrincipalContext(ContextType.Domain, domain, username, password);
Espen Harlinn 6-Jan-14 8:12am    
you need to add a
using System.DirectoryServices.AccountManagement;
statement to your file - and possibly a reference to the System.DirectoryServices.AccountManagement assembly.
Abdulmohsen Alshaya 7-Jan-14 4:06am    
Still same problem. should i link my computer with the domain?

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