locked
Cannot add user into Active Directory via ASP.net RRS feed

  • Question

  • User-961927681 posted

    My System Have 2 Computer:

    Frist Computer act as A domain Controller that has a Active Directory,RADIUS Server,DHCP and DNS

    Second Computer act as A Web Server and use Microsoft Visual Studio Team System 2008


    I create a website to add user in active directory. I have 3 file:
    1.Default.aspx - Let user fill their privacy information

    2.ViewRegisterdata.aspx - Let user confirm their information before it's added to active directory. In this file,there is a Viewregisterdata.aspx.cs which collect user information and send to Class1.cs when users click confirm button.

    3.Class1.cs - Do the process in adding user in active directory



    I have a problem with a second and third files.
    When I run the website and go to a second page that is a ViewRegisterdata.aspx and then press a confirm button,it shows an error.




    Although I inactivate a code section that do adding user to active directory and write a code to show a value that is sent from A ViewRegisterdata.aspx,It still shows a ViewDataregister.aspx and not show a Windows console.It seems it cannot send user information from a ViewRegisterData.aspx despite I include a Class1.cs into the file already
    Please Help me.

    This is a code of ViewRegisterData.aspx.cs

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using senior;
    using System.DirectoryServices;


    public partial class ViewRegisterData : System.Web.UI.Page
    {   
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                Label1.Text = "<br/>First Name: ";
                Label2.Text = Session["FirstName"].ToString();
                Label3.Text = "<br/>Last Name: ";
                Label4.Text = Session["LastName"].ToString();
                Label5.Text = "<br/>Address: ";
                Label6.Text = Session["Address"].ToString();
                Label7.Text = "<br/>E-Mail: ";
                Label8.Text = Session["Email"].ToString();
                Label9.Text = "<br/>Mac Address: ";
                Label10.Text = Session["Mac"].ToString();
                /*String Vdisplayname = Label2.Text + Label4.Text;*/
                DateLabel.Text = "ICT Mahidol University </br> " + DateTime.Now.ToLongDateString() + "</br>" + DateTime.Now.ToLongTimeString();
                
             }
          }
        
        protected void Edit_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
       
        protected void Confirm_Click(object sender, EventArgs e)
            {
                
                Response.Write("Registration is successful");
                String Vdisplayname = Label2.Text + Label4.Text;
                Class1 AddUser = new Class1(Label2.Text, Label4.Text, Label6.Text, Label8.Text, Vdisplayname, Label10.Text, Label10.Text);
         
                
            }
        

    }





    This is a code of Class.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.DirectoryServices;
    using senior;


    namespace senior
    {
        public class Class1
        {
            public struct UserInfo
            {
                public string username;
                public string AccountName;        
                public string firstname;
                public string lastname;
                public string address;
                public string email;
                public string displayname;
            }
           
                UserInfo newUserInfo;
                public Class1(string Rfirstname, string Rlastname, string Raddress, string Remail, string Rdisplayname,
                    string Rusername, string RaccountName)
                {

                    newUserInfo.firstname = Rfirstname;
                    newUserInfo.lastname = Rlastname;
                    newUserInfo.address = Raddress;
                    newUserInfo.email = Remail;
                    newUserInfo.displayname = Rdisplayname;
                    newUserInfo.username = Rusername;
                    newUserInfo.AccountName = RaccountName;
                   
                    
                        DirectoryEntry adUserFolder = new DirectoryEntry("LDAP://10.1.0.1/CN=Users;DC=seniorproject.com");

                        if (adUserFolder.SchemaEntry.Name == "container")
                        {
                            DirectoryEntry newUser = adUserFolder.Children.Add("CN=" + newUserInfo.username, "User");

                            if (DirectoryEntry.Exists(newUser.Path))
                            {
                                Console.Write("The user:" + newUser.Username + "exists, they will be deleted");
                                adUserFolder.Children.Remove(new DirectoryEntry(newUser.Path));
                            }
                            newUser.Properties["AccountName"].Value = newUserInfo.AccountName;
                            newUser.Properties["givenName"].Value = newUserInfo.firstname;
                            newUser.Properties["sn"].Value = newUserInfo.lastname;
                            newUser.Properties["displayName"].Value = newUserInfo.displayname;
                            newUser.Properties["Address"].Value = newUserInfo.address;
                            newUser.Properties["email"].Value = newUserInfo.email;
                            newUser.Properties["Password"].Value = "Y7l11909";

                            newUser.CommitChanges();

                            newUser.Invoke("setPassword:", "P@ssword:");
                            newUser.Properties["userAccountControl"].Value = 0x0200;
                            newUser.CommitChanges();
                        }
                    
                      
                }
                
            }
        }


    Thursday, February 25, 2010 2:31 AM

All replies

  • User191633014 posted

    Asp is runnig with low rights user...

    You need to conect to LDAP with power user in this way:

    DirectoryEntry dir = new DirectoryEntry("LDAP://servername......",username,password,AuthenticationTypes.SecureSocketsLayer); 

    Thursday, February 25, 2010 3:36 AM
  • User-961927681 posted

    It shows error that a"username" variable which change to Adminstrator does not exist.


    I will try to quote a "username" and "passoword" variable and run. It shows a server is not operational.

    Thursday, February 25, 2010 3:57 AM
  • User191633014 posted

    User must be with rights in AD...  

    something like Admin@seniorproject.com or seniorproject\admin

    Thursday, February 25, 2010 4:13 AM
  • User-961927681 posted

    I change this code line which is a connection as you suggest

    from

    DirectoryEntry adUserFolder = new DirectoryEntry("LDAP://10.1.0.1/CN=Users;DC=seniorproject.com");

    to

    DirectoryEntry adUserFolder = new DirectoryEntry("LDAP://www.seniorproject.com", "Administrator@seniorproject.com", "********", AuthenticationTypes.SecureSocketsLayer);                                                                                   
                                                                                                                                                           

                                                                                                                                                           
                                                                                                                                                           

     I must quote at "Administrator@seniorproject.com" because if I don't,it will show red underline.

    When I run again, It shows error "The Server is not operational


    I have more questions.I have to install ADAM in a first computer which act as a domain controller or not to connect Active Directory because now I don't install ADAM yet.

    Thursday, February 25, 2010 4:35 AM
  • User191633014 posted

     why you changed the path... try with:

    DirectoryEntry adUserFolder = new DirectoryEntry(LDAP://10.1.0.1/CN=Users;DC=seniorproject.com, "Administrator@seniorproject.com", "********", AuthenticationTypes.SecureSocketsLayer);

    Thursday, February 25, 2010 4:42 AM
  • User-961927681 posted

    It still shows same error. The server is not operational.


    I need to install ADAM in the first computer that acts as a domain controller or not.Because I do not install ADAM yet.

    Friday, February 26, 2010 12:34 AM