473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

ADSI and C#

Hi,

I'm trying to rename the Administrator Login, and i want to know if my
approach is right:
void RenameUsr(string hostname, string admusr)

{

DirectoryEntry entry = new DirectoryEntry("WinNT://" + hostname + "/" +
"Administrator" + ",user");

entry.Properties["UserPrincipalName"].Value = admusr;

entry.CommitChanges();

}

Thanks,

Victor
Nov 17 '05 #1
10 6885
>I'm trying to rename the Administrator Login, and i want to know if my
approach is right:


What do you mean by "rename"? What name (there are plenty!) do you
want to change? The actual logon name? The LDAP object name? The
display name ?

Also, are you running on a local machine, or in a network?? If you're
on a network, I'd STRONGLY recommend using LDAP instead of the
deprecated WinNT provider.

Marc
Nov 17 '05 #2
Hi Marc,
I want to rename the logon name. Now i changed my approach and now i'm using
WMI. But my problem is: This code does not work in W2k, just in XP and 2003.
Do you know how can i do it in a W2k Enviroment ?
Look my code:

void RenameUsr(string hostname, string admusr)

{
try

{

ConnectionOptions oConn = new ConnectionOptions();

string myMachine = @"\\" + hostname + @"\root\cimv2" ;

string myQuery = "Select * from Win32_UserAccount WHERE Domain = " + "'" +
hostname + "'" + " AND SIDType = 1 ";

ManagementScope oMs = new ManagementScope(myMachine, oConn);

ObjectQuery oQuery = new ObjectQuery(myQuery);

ManagementObjectSearcher oSearcher = new
ManagementObjectSearcher(oMs,oQuery);

ManagementObjectCollection queryCollection1 = oSearcher.Get();

foreach( ManagementObject mo in queryCollection1 )

{
try

{

string sLogin = mo["Name"].ToString();

if(sLogin.StartsWith("Admin"))

{

ManagementBaseObject iPar = mo.GetMethodParameters("Rename");

iPar["Name"] = admusr;

ManagementBaseObject outPar= mo.InvokeMethod("Rename", iPar, null);

uint ret = (uint)outPar.Properties["ReturnValue"].Value;

if(ret != 0)

{

MessageBox.Show("Error " + ret.ToString() + " trying to rename user");

}

}

}

catch(Exception ez)

{

MessageBox.Show(hostname + " " + admusr + " " + ez.Message);

}

}

}

catch(Exception zz)

{

MessageBox.Show(hostname + " " + admusr + " " + zz.Message);

}

}

}

Thanks in advance,

Victor

"Marc Scheuner [MVP ADSI]" <m.********@inova.SPAMBEGONE.ch> wrote in message
news:s4********************************@4ax.com...
I'm trying to rename the Administrator Login, and i want to know if my
approach is right:


What do you mean by "rename"? What name (there are plenty!) do you
want to change? The actual logon name? The LDAP object name? The
display name ?

Also, are you running on a local machine, or in a network?? If you're
on a network, I'd STRONGLY recommend using LDAP instead of the
deprecated WinNT provider.

Marc

Nov 17 '05 #3
>I want to rename the logon name.

Again: are you dealing with a local machine and a local account, or is
this an account on a network / in a network domain?

AFAIK, WMI is read-only by design - I don't think you can update
anything through it, really.

If you're dealing with a domain and a user account in a domain, your
best choice is to use ADSI and the LDAP provider - find the user in
question (either by just knowing his LDAP path, or by searching for
it), and then update the appropriate name (in your case: the
"sAMAccountName"), and save the changes back to the store.

Something like:

DirectoryEntry deUser = new
DirectoryEntry("LDAP://cn=JohnDoe,cn=Users,dc=fabrikam,dc=com");

deUser.Properties["sAMAccountName"].Value = "new_logon_name";
deUser.CommitChanges();

Marc
Nov 17 '05 #4
Hi Marc,
WMI isn't read-only and my pasted code is working but just in windows xp and
2003.
I'm working with local machine, so that's why i'm not using the LDAP
provider. My problem is: I want to rename the local Administrator in a
"Windows 2000 enviroment".

Thanks for your help,

Victor
"Marc Scheuner [MVP ADSI]" <m.********@inova.SPAMBEGONE.ch> wrote in message
news:b1********************************@4ax.com...
I want to rename the logon name.


Again: are you dealing with a local machine and a local account, or is
this an account on a network / in a network domain?

AFAIK, WMI is read-only by design - I don't think you can update
anything through it, really.

If you're dealing with a domain and a user account in a domain, your
best choice is to use ADSI and the LDAP provider - find the user in
question (either by just knowing his LDAP path, or by searching for
it), and then update the appropriate name (in your case: the
"sAMAccountName"), and save the changes back to the store.

Something like:

DirectoryEntry deUser = new
DirectoryEntry("LDAP://cn=JohnDoe,cn=Users,dc=fabrikam,dc=com");

deUser.Properties["sAMAccountName"].Value = "new_logon_name";
deUser.CommitChanges();

Marc

Nov 17 '05 #5
Hi Willy,
Thanks for your reply.. two MVPs aswering my questions.. that is a honor!

But when you sad that i must create and delete i think your wrong and here
is a function to proof it!

void RenameUser(string hostname,string admusr)

{

try

{

DirectoryEntry entry = new DirectoryEntry("WinNT://" + hostname +
",computer");

DirectoryEntry cEntry = entry.Children.Find("administrator");

MessageBox.Show(cEntry.Path.ToString());

cEntry.MoveTo(entry,admusr);

cEntry.CommitChanges();

}

catch(Exception cc)

{

MessageBox.Show(cc.Message.ToString());

}

}

Reguards,

Victor Pereira MVP-WannaBe :-)
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...

"Victor Pereira" <ta*******@yahoo.com.br> wrote in message
news:e7**************@TK2MSFTNGP12.phx.gbl...
Hi Marc,
I want to rename the logon name. Now i changed my approach and now i'm
using
WMI. But my problem is: This code does not work in W2k, just in XP and
2003.
Do you know how can i do it in a W2k Enviroment ?
You can't call Rename on W2K, it's only supported on XP, W2K and higher.
Only thing you can do is delete and recreate the account using
DirectoryServices with the WinNT provider. You are also aware of the

dangers of renaming the administrator account do you?

Willy.

Nov 17 '05 #6

"Victor Pereira" <ta*******@yahoo.com.br> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi Willy,
Thanks for your reply.. two MVPs aswering my questions.. that is a honor!

But when you sad that i must create and delete i think your wrong and here
is a function to proof it!

void RenameUser(string hostname,string admusr)

{

try

{

DirectoryEntry entry = new DirectoryEntry("WinNT://" + hostname +
",computer");

DirectoryEntry cEntry = entry.Children.Find("administrator");

MessageBox.Show(cEntry.Path.ToString());

cEntry.MoveTo(entry,admusr);

cEntry.CommitChanges();

}

catch(Exception cc)

{

MessageBox.Show(cc.Message.ToString());

}

}

Reguards,

Victor Pereira MVP-WannaBe :-)


Yes, but you probably know there are different ways to skin a cat, here is
another one.

DirectoryEntry cEntry = userEntry.Children.Find("administrator");
cEntry.Rename(admuser);
cEntry.CommitChanges();

All depends what you realy wanna do, do you need a completely new entry
(that is a new SID) or do you need to keep the SID associated with a new
name?.
Note that someone who knows the SID of the original administrator, can also
find the name of the new administrator when using MoveTo and Rename.
Willy.
Nov 17 '05 #7
Hi Willy,

I tried this "DirectoryEntry cEntry = userEntry.Find("admin"), but did'nt
worked."

Thanks for your reply,

Victor
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:el**************@TK2MSFTNGP15.phx.gbl...

"Victor Pereira" <ta*******@yahoo.com.br> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi Willy,
Thanks for your reply.. two MVPs aswering my questions.. that is a honor!
But when you sad that i must create and delete i think your wrong and here is a function to proof it!

void RenameUser(string hostname,string admusr)

{

try

{

DirectoryEntry entry = new DirectoryEntry("WinNT://" + hostname +
",computer");

DirectoryEntry cEntry = entry.Children.Find("administrator");

MessageBox.Show(cEntry.Path.ToString());

cEntry.MoveTo(entry,admusr);

cEntry.CommitChanges();

}

catch(Exception cc)

{

MessageBox.Show(cc.Message.ToString());

}

}

Reguards,

Victor Pereira MVP-WannaBe :-)

Yes, but you probably know there are different ways to skin a cat, here is
another one.

DirectoryEntry cEntry = userEntry.Children.Find("administrator");
cEntry.Rename(admuser);
cEntry.CommitChanges();

All depends what you realy wanna do, do you need a completely new entry
(that is a new SID) or do you need to keep the SID associated with a new
name?.
Note that someone who knows the SID of the original administrator, can

also find the name of the new administrator when using MoveTo and Rename.
Willy.

Nov 17 '05 #8
Missing Children in ...
userEntry.Find

Willy.

"Victor Pereira" <ta*******@yahoo.com.br> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...
Hi Willy,

I tried this "DirectoryEntry cEntry = userEntry.Find("admin"), but did'nt
worked."

Thanks for your reply,

Victor
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:el**************@TK2MSFTNGP15.phx.gbl...

"Victor Pereira" <ta*******@yahoo.com.br> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
> Hi Willy,
> Thanks for your reply.. two MVPs aswering my questions.. that is a honor! >
> But when you sad that i must create and delete i think your wrong and here > is a function to proof it!
>
> void RenameUser(string hostname,string admusr)
>
> {
>
> try
>
> {
>
> DirectoryEntry entry = new DirectoryEntry("WinNT://" + hostname +
> ",computer");
>
> DirectoryEntry cEntry = entry.Children.Find("administrator");
>
> MessageBox.Show(cEntry.Path.ToString());
>
> cEntry.MoveTo(entry,admusr);
>
> cEntry.CommitChanges();
>
> }
>
> catch(Exception cc)
>
> {
>
> MessageBox.Show(cc.Message.ToString());
>
> }
>
> }
>
>
>
> Reguards,
>
> Victor Pereira MVP-WannaBe :-)
>


Yes, but you probably know there are different ways to skin a cat, here
is
another one.

DirectoryEntry cEntry = userEntry.Children.Find("administrator");
cEntry.Rename(admuser);
cEntry.CommitChanges();

All depends what you realy wanna do, do you need a completely new entry
(that is a new SID) or do you need to keep the SID associated with a new
name?.
Note that someone who knows the SID of the original administrator, can

also
find the name of the new administrator when using MoveTo and Rename.
Willy.


Nov 17 '05 #9
>I tried this "DirectoryEntry cEntry = userEntry.Find("admin"), but did'nt
worked."


First of all, as Willy pointed out, you need to search in the
userEntry.Children collection.

Secondly, you need to prefix your user with the cn= moniker.

And thirdly, I'd suggest using the other overload of this function,
where you can specify the class name as well.

So I'd search like this:

DirectoryEntry cEntry = userEntry.Children.Find("cn=admin", "user");

Also mind you - you need to use the actual object name in Active
Directory - *NOT* it's sam account name (which you'd use to log on).

Marc

================================================== ==============
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
Nov 17 '05 #10
Marc and Willy,
Thanks!
"Marc Scheuner [MVP ADSI]" <m.********@inova.SPAMBEGONE.ch> wrote in message
news:uj********************************@4ax.com...
I tried this "DirectoryEntry cEntry = userEntry.Find("admin"), but did'nt
worked."


First of all, as Willy pointed out, you need to search in the
userEntry.Children collection.

Secondly, you need to prefix your user with the cn= moniker.

And thirdly, I'd suggest using the other overload of this function,
where you can specify the class name as well.

So I'd search like this:

DirectoryEntry cEntry = userEntry.Children.Find("cn=admin", "user");

Also mind you - you need to use the actual object name in Active
Directory - *NOT* it's sam account name (which you'd use to log on).

Marc

================================================== ==============
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch

Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Miguel Orrego | last post by:
Hi, I have found some code that authenticates users agains a domain using ADSI. I then redirect to another page and pass the username they have entered as a string. However, it would be nice to...
4
by: Akhlaq Khan | last post by:
we are developing an intranet application (web based) which needs to detect the logged in user ID of the user hitting the website. the intranet is huge and based on win2k active directory (around...
1
by: Ryan Ritten | last post by:
I was wondering if anyone knew how (or if it's even possible) to cache the results of an ADSI call in asp for a longer period of time. Basically what I am doing is I have a website that loads the...
2
by: Enigma Webmaster | last post by:
Hi All, We've written a couple of functions which, when run in VB6 work fine and allow AD users to be updated. When we include the code into an ASP Page and try and update a users information...
14
by: Arran Pearce | last post by:
Hi, I am looking for a way to use System.DirectoryServices to find all users on a domain whos accounts are either locked out or disabled. I have used ADSIEdit and the mmc schema add-in to try...
3
by: Roy Osherove | last post by:
Hi folks. I have an ASP.Net application that runs a .Net dll that uses WMI and ADSI(both managed) to connect to a given IIS root and search through it. When not using the ASP.Net client, but...
8
by: msnews.microsoft.com | last post by:
I have ADSI code that I can make work at the command line. I cannot in any way get it to work in asp.net. Even using Windows authentication, impersonation on, and providing the credentials...
3
by: chat_devil | last post by:
hi, does anyone know if it is possible to remove an attribute that can not be read into the ADSI property cache/collection. i'm trying to do an eDirectory password change from .net directory...
1
by: LittlePython | last post by:
I am a little confused on why I can not detect an object that does not exist with a try and except. If I understand ADSI correctly from what I have read you do not create these objects but rather...
8
by: John | last post by:
Hi, gurus, How can I implement the following feature in C#: Set objGroup = GetObject("WinNT://" & strComputer & "/" & strGroup & ", group") For Each objMember In objGroup.Members...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.