Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cannot add user into Active Directory via c#.net

48 views
Skip to first unread message

dexterMor

unread,
Mar 5, 2010, 10:00:13 AM3/5/10
to
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(join domain with a first computer already)

and use Microsoft Visual Studio Team System 2008

I create a website on a second computer 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 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 as this picture link
below:.

http://img21.imageshack.us/img21/2654/errorweb3.jpg

This is my code in a third file:

using System;
using System.Collections;
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;

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;
public string password;
public string givenName;
}

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,DC=COM","Admini...@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();
}

}


}
}

I search in google about adding user into Active Directory.The most results
are like my code and can run well while I cannot. Why?

--
Message posted via http://www.dotnetmonster.com

Mark Rae [MVP]

unread,
Mar 5, 2010, 10:46:23 AM3/5/10
to
"dexterMor" <u53265@uwe> wrote in message news:a48a26c744cbe@uwe...

> 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;

> I search in google about adding user into Active Directory.The most
> results
> are like my code and can run well while I cannot. Why?

The error message is telling you that one of the properties you're trying to
assign to the user object does not exist. Rem them all out apart from the
first one and then add them back in one at a time until you find the one
which is incorrect. It's been a while since I did any AD stuff, but
["AccountName"] doesn't look right to me - are you sure it's not
sAMAccountName? Not sure about ["Address"] either...
http://www.kouti.com/tables/userattributes.htm

Don't forget that C# is case-sensitive...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

0 new messages