473,395 Members | 2,423 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,395 software developers and data experts.

Programming the User-Account_Property using C#

I am not sure that I have picked the right newsgroup for this post.

I am writing a program in C# VS 2005 to create user accounts for our
students in Active Directory.
This is to replace a program I wrote some years ago in VS6 using C++ and
ADSI.

I am trying not to use ADSI in my new program, focussing on
DirectoryServices instead.

In C# the syntax to set an Active Directory property is something like:
entry.Properties["givenName"].value="Jim"; (where entry is a
DirectoryEntry)

However for the userAccountControl property it is necessary to use a
combination of enumerated identifiers.
see http://msdn2.microsoft.com/en-us/library/aa772300.aspx
I do not want the students to be able to change a password so I need to use
ADS_UF_PASSWD_CANT_CHANGE.
This flag can be read, but not set directly as it involves setting trustee
rights etc. There is a link to an example program to set this flag but it is
in C++ and uses ADSI which I want to avoid.

Surely there must be a nice new clean way of doing this in Framework 2.0.

Is there any example C# code using DirectoryServices to set
ADS_UF_PASSWD_CANT_CHANGE?

I don't want to have to write a wrapper for my existing unmanaged code which
involved ACEs, ACLs, security descriptors etc
Mar 9 '07 #1
10 2489
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:ul**************@TK2MSFTNGP04.phx.gbl...
>I am not sure that I have picked the right newsgroup for this post.

I am writing a program in C# VS 2005 to create user accounts for our students in Active
Directory.
This is to replace a program I wrote some years ago in VS6 using C++ and ADSI.

I am trying not to use ADSI in my new program, focussing on DirectoryServices instead.

In C# the syntax to set an Active Directory property is something like:
entry.Properties["givenName"].value="Jim"; (where entry is a DirectoryEntry)

However for the userAccountControl property it is necessary to use a combination of
enumerated identifiers.
see http://msdn2.microsoft.com/en-us/library/aa772300.aspx
I do not want the students to be able to change a password so I need to use
ADS_UF_PASSWD_CANT_CHANGE.
This flag can be read, but not set directly as it involves setting trustee rights etc.
There is a link to an example program to set this flag but it is in C++ and uses ADSI
which I want to avoid.

Surely there must be a nice new clean way of doing this in Framework 2.0.

Is there any example C# code using DirectoryServices to set ADS_UF_PASSWD_CANT_CHANGE?

I don't want to have to write a wrapper for my existing unmanaged code which involved
ACEs, ACLs, security descriptors etc
The only way to set this "user cannot change password" property is by turning the ACE's on
the user object into an ACCESS_DENIED_ACE type ACE.
This can be done by using the DirectorySecurity class of the System.Security.AccessControl
namespace.

Willy.


Mar 9 '07 #2
Thanks Willy

But I'm still confused.
In my earlier version the algorithm to do this went roughly like this:
Get Security Descriptor for the object.
Using the security descriptor get the DACL for the object.
Create an ACE and add:
access mask ADS_RIGHT_DS_CONTROL_ACCESS
trustee (NT Authority\\Self)
access type ADS_ACE_TYPE_ACCESS_DENIED_OBJECT
set accessflags to to CHANGE_PASSWORD_GUID (no inheritance)
add ACE to DACL
Set Security Descriptor for the object.

Do I still need to do all this.
I am finding it difficult to translate the old terms into the new ones.
I can't see how to get to the ACEs from the DirectoryEntry
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:eA**************@TK2MSFTNGP05.phx.gbl...
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:ul**************@TK2MSFTNGP04.phx.gbl...
>>I am not sure that I have picked the right newsgroup for this post.

I am writing a program in C# VS 2005 to create user accounts for our
students in Active Directory.
This is to replace a program I wrote some years ago in VS6 using C++ and
ADSI.

I am trying not to use ADSI in my new program, focussing on
DirectoryServices instead.

In C# the syntax to set an Active Directory property is something like:
entry.Properties["givenName"].value="Jim"; (where entry is a
DirectoryEntry)

However for the userAccountControl property it is necessary to use a
combination of enumerated identifiers.
see http://msdn2.microsoft.com/en-us/library/aa772300.aspx
I do not want the students to be able to change a password so I need to
use ADS_UF_PASSWD_CANT_CHANGE.
This flag can be read, but not set directly as it involves setting
trustee rights etc. There is a link to an example program to set this
flag but it is in C++ and uses ADSI which I want to avoid.

Surely there must be a nice new clean way of doing this in Framework 2.0.

Is there any example C# code using DirectoryServices to set
ADS_UF_PASSWD_CANT_CHANGE?

I don't want to have to write a wrapper for my existing unmanaged code
which involved ACEs, ACLs, security descriptors etc

The only way to set this "user cannot change password" property is by
turning the ACE's on the user object into an ACCESS_DENIED_ACE type ACE.
This can be done by using the DirectorySecurity class of the
System.Security.AccessControl namespace.

Willy.


Mar 9 '07 #3
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:eW**************@TK2MSFTNGP06.phx.gbl...
Thanks Willy

But I'm still confused.
In my earlier version the algorithm to do this went roughly like this:
Get Security Descriptor for the object.
Using the security descriptor get the DACL for the object.
Create an ACE and add:
access mask ADS_RIGHT_DS_CONTROL_ACCESS
trustee (NT Authority\\Self)
access type ADS_ACE_TYPE_ACCESS_DENIED_OBJECT
set accessflags to to CHANGE_PASSWORD_GUID (no inheritance)
add ACE to DACL
Set Security Descriptor for the object.

Do I still need to do all this.
I am finding it difficult to translate the old terms into the new ones.
I can't see how to get to the ACEs from the DirectoryEntry
This is excatly what you need to do, except that now in v2 you have a class which makes it a
lot easier.
Take a look at the ActiveDirectorySecurity class (and not DirectorySecurity as per my
previous answer) in System.Security.AccessControl.
And here is a C# sample (scroll down the page):
http://msdn2.microsoft.com/en-gb/library/ms180915.aspx
but there are easier ways to do this using the SDDL format.

Willy.
Mar 9 '07 #4
Thanks Willy

Thanks - this was exactly what I needed. It works fine and its a great deal
less code than my previous version from 5/6 years ago.
When I've finished this I am unlikely to have to stray into into this area
again for a good few years. It will probably be something like VS 2010 with
Framework 10 and yet another new technology! Unless you work regularly with
a particular technology it is difficult to know what approach to take to
solve a particular problem and to find examples on the MS site. SDDL is a
new one for me. I will have to look it up.

I now have to work out how to 'mail enable' a user and create a mailbox for
them. I used CDOEXM before. I hope there is a Framework class now to handle
all this.

Thanks again for your help

Regards

Chris

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uN*************@TK2MSFTNGP04.phx.gbl...
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:eW**************@TK2MSFTNGP06.phx.gbl...
>Thanks Willy

But I'm still confused.
In my earlier version the algorithm to do this went roughly like this:
Get Security Descriptor for the object.
Using the security descriptor get the DACL for the object.
Create an ACE and add:
access mask ADS_RIGHT_DS_CONTROL_ACCESS
trustee (NT Authority\\Self)
access type ADS_ACE_TYPE_ACCESS_DENIED_OBJECT
set accessflags to to CHANGE_PASSWORD_GUID (no inheritance)
add ACE to DACL
Set Security Descriptor for the object.

Do I still need to do all this.
I am finding it difficult to translate the old terms into the new ones.
I can't see how to get to the ACEs from the DirectoryEntry

This is excatly what you need to do, except that now in v2 you have a
class which makes it a lot easier.
Take a look at the ActiveDirectorySecurity class (and not
DirectorySecurity as per my previous answer) in
System.Security.AccessControl.
And here is a C# sample (scroll down the page):
http://msdn2.microsoft.com/en-gb/library/ms180915.aspx
but there are easier ways to do this using the SDDL format.

Willy.


Mar 10 '07 #5
Willy

It looks like I've found what I need for creating the mailboxes to save you
replying.
http://support.microsoft.com/kb/313114/

Regards

Chris

"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:uL**************@TK2MSFTNGP04.phx.gbl...
Thanks Willy

Thanks - this was exactly what I needed. It works fine and its a great
deal less code than my previous version from 5/6 years ago.
When I've finished this I am unlikely to have to stray into into this area
again for a good few years. It will probably be something like VS 2010
with Framework 10 and yet another new technology! Unless you work
regularly with a particular technology it is difficult to know what
approach to take to solve a particular problem and to find examples on the
MS site. SDDL is a new one for me. I will have to look it up.

I now have to work out how to 'mail enable' a user and create a mailbox
for them. I used CDOEXM before. I hope there is a Framework class now to
handle all this.

Thanks again for your help

Regards

Chris

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uN*************@TK2MSFTNGP04.phx.gbl...
>"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:eW**************@TK2MSFTNGP06.phx.gbl...
>>Thanks Willy

But I'm still confused.
In my earlier version the algorithm to do this went roughly like this:
Get Security Descriptor for the object.
Using the security descriptor get the DACL for the object.
Create an ACE and add:
access mask ADS_RIGHT_DS_CONTROL_ACCESS
trustee (NT Authority\\Self)
access type ADS_ACE_TYPE_ACCESS_DENIED_OBJECT
set accessflags to to CHANGE_PASSWORD_GUID (no inheritance)
add ACE to DACL
Set Security Descriptor for the object.

Do I still need to do all this.
I am finding it difficult to translate the old terms into the new ones.
I can't see how to get to the ACEs from the DirectoryEntry

This is excatly what you need to do, except that now in v2 you have a
class which makes it a lot easier.
Take a look at the ActiveDirectorySecurity class (and not
DirectorySecurity as per my previous answer) in
System.Security.AccessControl.
And here is a C# sample (scroll down the page):
http://msdn2.microsoft.com/en-gb/library/ms180915.aspx
but there are easier ways to do this using the SDDL format.

Willy.



Mar 12 '07 #6
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Willy

It looks like I've found what I need for creating the mailboxes to save you replying.
http://support.microsoft.com/kb/313114/
Yep, but this is still using CDOEXT. Don't know if Excjhange 2007 hasn't some managed code
libraries for these.

Willy.

Mar 12 '07 #7
Our network team have only just upgraded to Exchange 2003.
I assume that Exchange 2007 will have extra features and that a program
wriiten for it will probably not work with 2003.
How could I find out?

Chris

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uK**************@TK2MSFTNGP03.phx.gbl...
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>Willy

It looks like I've found what I need for creating the mailboxes to save
you replying.
http://support.microsoft.com/kb/313114/

Yep, but this is still using CDOEXT. Don't know if Excjhange 2007 hasn't
some managed code libraries for these.

Willy.

Mar 12 '07 #8
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:es**************@TK2MSFTNGP04.phx.gbl...
Our network team have only just upgraded to Exchange 2003.
I assume that Exchange 2007 will have extra features and that a program wriiten for it
will probably not work with 2003.
How could I find out?

Chris
Chris,
I suggest you to ask this in an Exchange NG. Another option is to download a trial version
of Exchange 2007 and try it out yourself.

Willy.

Mar 12 '07 #9
Thanks Willy

I have got everything working now except for one small point which is
bugging me.
I want to add a user to several groups. I have several different kinds of
user with a different number of group memberships.
I have a user class which has the number of groups required and an array of
strings which hold the group names.
I have tried various approaches the last one being:

// entry is a DirectoryEntry - user.Group[n] is a String

for (int n=0;n<user.Number_Of_Groups;n++)
entry.Properties["memberOf"].Add(user.Group[n].ToString());

I can't use indexing as there may be existing group memberships. Why doesn't
this work?

I realise that I will have to check for existing group membership to avoid
duplication.

Chris
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:OI**************@TK2MSFTNGP06.phx.gbl...
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:es**************@TK2MSFTNGP04.phx.gbl...
>Our network team have only just upgraded to Exchange 2003.
I assume that Exchange 2007 will have extra features and that a program
wriiten for it will probably not work with 2003.
How could I find out?

Chris

Chris,
I suggest you to ask this in an Exchange NG. Another option is to download
a trial version of Exchange 2007 and try it out yourself.

Willy.

Mar 12 '07 #10
Willy

I am going to turn this on its head and add the users to the group rather
than add groups to the user as it looks easier. When I checked my old code
that was how I did it then.
I found this which should help me

http://msdn2.microsoft.com/en-us/lib...04(vs.80).aspx

Chris
"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Thanks Willy

I have got everything working now except for one small point which is
bugging me.
I want to add a user to several groups. I have several different kinds of
user with a different number of group memberships.
I have a user class which has the number of groups required and an array
of strings which hold the group names.
I have tried various approaches the last one being:

// entry is a DirectoryEntry - user.Group[n] is a String

for (int n=0;n<user.Number_Of_Groups;n++)
entry.Properties["memberOf"].Add(user.Group[n].ToString());

I can't use indexing as there may be existing group memberships. Why
doesn't this work?

I realise that I will have to check for existing group membership to avoid
duplication.

Chris
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:OI**************@TK2MSFTNGP06.phx.gbl...
>"Chris Noble" <ch*********@newsgroup.nospamwrote in message
news:es**************@TK2MSFTNGP04.phx.gbl...
>>Our network team have only just upgraded to Exchange 2003.
I assume that Exchange 2007 will have extra features and that a program
wriiten for it will probably not work with 2003.
How could I find out?

Chris

Chris,
I suggest you to ask this in an Exchange NG. Another option is to
download a trial version of Exchange 2007 and try it out yourself.

Willy.


Mar 13 '07 #11

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

Similar topics

0
by: John Davis | last post by:
I always heard the term "data/database driven programming" model in ASP database, SQL programming, and web programming circles. But I don't quite sure what does it mean? Does it mean the web...
16
by: Feico | last post by:
Dear friends I am an experienced programmer, but I happen to have MS Access, which uses a language unknown to me. I want to perform an operation on all record on a table, like this For...
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
42
by: Kevin Spencer | last post by:
Is it just me, or am I really observing a trend away from analysis and probem-solving amongst programmers? Let me be more specific: It seems that every day, in greater numbers, people are coming...
6
by: Martin Ortiz | last post by:
Which is best approach? Should Try + Catch be used to only deal with "catastrophic" events (like divide by zero, non-existant file, etc...etc...) Or should Try + Catch be used IN PLACE of...
38
by: ifti_crazy | last post by:
I am VB6 programmer and wants to start new programming language but i am unable to deciced. i have read about Python, Ruby and Visual C++. but i want to go through with GUI based programming...
3
dmjpro
by: dmjpro | last post by:
plz send me a good link which can clearify me how the J2EE framework works i want the details information .... plz help thanx
7
Banfa
by: Banfa | last post by:
Posted by Ganon11 So, you want to learn how to program! Good for you! Programming is a very intruiging and fun activity to pick up, and it's also a great career choice if you like it! Finally,...
43
by: Adem24 | last post by:
The World Joint Programming Language Standardization Committe (WJPLSC) hereby proclaims to the people of the world that a new programming language is needed for the benefit of the whole mankind in...
5
by: av3rage | last post by:
I have never done any programming in my life but I have decided to go into engineering and in doing so we have to take this intro to programming course and I am pretty clueless. I am starting to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.