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.
This is my code in a third file:
PowerShell
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 classClass1
{
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","[email protected]","********");
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();
}
}
}
}
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 2, 2010, 9:32 PM EST
if (DirectoryEntry.Exists(newUser.Path))
{
Console.Write("The user:" + newUser.Username + "exists, they will be deleted");
adUserFolder.Children.Remove(new DirectoryEntry(newUser.Path));
}
Here you are adding a child and just after adding the child you are checking if exist with the name. Definitely you will find a directory with the name as you have created and then after deleting the directory you are commiting the changes. Its quite confusing.... Give a lool to it again... hope you understand what i want to say.
if (DirectoryEntry.Exists(newUser.Path))
{
Console.Write("The user:" + newUser.Username + "exists, they will be deleted");
adUserFolder.Children.Remove(new DirectoryEntry(newUser.Path));
}
and run my web again. The error is showed the same.
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 2, 2010, 9:32 PM EST
change the position of remove condition below:
Here you are adding a child and just after adding the child you are checking if exist with the name. Definitely you will find a directory with the name as you have created
and then after deleting the directory you are commiting the changes.
Its quite confusing.... Give a lool to it again... hope you understand what i want to say.
John3 reputationPosted Mar 2, 2010, 9:47 PM EST