0 Replies - 1829 Views - Last Post: 17 September 2012 - 09:38 AM Rate Topic: -----

#1 shadowX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-September 12

I Cannot add user account in Active Directory via ASP.net c#

Posted 17 September 2012 - 09:38 AM

Hi all Expert Please Help me

I'm doing project about MAC-BASED WIRELESS AUTHENTICATION

I have 2 main component...

First is my Computer(Windows sever 2008 R2) have a domain Controller that has a Active Directory,RADIUS Server and DNS

Second is Linksys WRT54GS Routher

I already test all system like add user in AD (by manual) then use my another computer to login

The result is pass I can access to the network

Next I devolop web interface( by Microsoft Visual Studio(basic) 2008 ) for add user into AD

but I have a big problems that

I Cannot add user account in Active Directory via ASP.net c#!!

When I run the wbesite and click confirm button on confirm page it show Error!! (below)

Posted Image

Can you please help me how to slove this problems....Thank you very much

you can fix my code below to slove this problems


there are 3 file page about add user account system

1.Registration.aspx - Let user fill their information

2.confirm.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

this is code all 3 page

1.Registration.aspx

Posted Image
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 System.DirectoryServices;



public partial class Registration : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Confirm3_Click(object sender, EventArgs e)
    {
        Session["firstname"] = txtfirstname.Text;
        Session["password"] = txtpassword.Text;
        Session["username"] = txtusername.Text;
        Session["email"] = txtemail.Text;
        Session["lastname"] = txtlastname.Text;
        Session["mac"] = txtmac.Text;
        Response.Redirect("confirm.aspx");
    }
    protected void txtfirstname_TextChanged(object sender, EventArgs e)
    {

    }

    protected void txtusername_TextChanged(object sender, EventArgs e)
    {

    }
}

[b]2.confirm.aspx[/b]



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 maclogin;
using System.Xml.Linq;
using System.DirectoryServices;

public partial class confirm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
           
           lblfirstname.Text = Session["firstName"].ToString();

           lblpassword.Text = Session["password"].ToString();

           lbluesrname.Text = Session["username"].ToString();
           
           lbllastname.Text = Session["lastName"].ToString();
            
           lblemail.Text = Session["email"].ToString();
            
           lblmac.Text = Session["mac"].ToString();
            String displayname = lblfirstname.Text + lbllastname.Text;
            
        }

    }

    

    protected void submit_Click(object sender, EventArgs e)
    {

        String displayname = lblfirstname.Text + lbllastname.Text;

        Class1 AddUser = new Class1(lblfirstname.Text, lbllastname.Text, lblemail.Text, displayname, lbluesrname.Text, lbluesrname.Text);
        
        
        Response.Write("Registration is successful");

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("Registration.aspx");
    }
}

[b]3.Class1.cs[/b]

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 System.DirectoryServices;

namespace maclogin
{
    public class Class1
    {
        public struct UserInfo
        {
            public string username;
            public string AccountName;
            public string firstname;
            public string lastname;
            
            public string email;
            public string displayname;
            public string password;
            public string givenName;
            
        }

        UserInfo newUserInfo;
        public Class1(string Rfirstname, string Rlastname, string Remail, string Rdisplayname,
        string Rusername, string RaccountName)
        
        {
            newUserInfo.firstname = Rfirstname;
            newUserInfo.lastname = Rlastname;       
            newUserInfo.email = Remail;
            newUserInfo.displayname = Rdisplayname;
            newUserInfo.username = Rusername;
            newUserInfo.AccountName = RaccountName;


            DirectoryEntry adUserFolder = new DirectoryEntry("LDAP://192.168.1.100/CN=Users;DC=maclogin,DC=COM", "Administrator@maclogin.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["username"].Value = newUserInfo.username;
                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["email"].Value = newUserInfo.email;
                //newUser.Properties["Password"].Value = "Forgomkk69";


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

        }


    }
}


Thank you

*** Edit ***
Please use code tags when posting code.
Posted Image

This post has been edited by GunnerInc: 17 September 2012 - 02:40 PM
Reason for edit:: Addec code tags


Is This A Good Question/Topic? 0
  • +

Page 1 of 1