创建本地用户帐户 C# 和 .NET 2.0
如何使用 .NET 2.0 和 c# 创建本地用户帐户,并且能够将“密码永不过期”设置为永不。
我尝试使用“Net.exe”使用 Process.Start 并传递其参数,但似乎“网络用户”无法将“密码永不过期”设置为永不。
How can I create a local user account using .NET 2.0 and c# and also be able to set the "Password never expires" to never.
I have tried using "Net.exe" using Process.Start and passing its parameters but it seems that the "net user" is unable to set the "Password never expires" to never.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
阅读这篇优秀的 CodeProject 文章
Howto:(几乎)通过 C# 实现 Active Directory 中的所有内容< /strong>
有一个“创建用户帐户”和“处理用户密码”部分。
更新:
要调整本地帐户的代码,请将相应的行替换为以下内容:
这里开始域帐户的原始代码片段:
Read this excellent CodeProject article
Howto: (Almost) Everything In Active Directory via C#
There is a section "Create User Account" and "Dealing with User Passwords".
UPDATE:
To adapt the code for local accounts replace the respective lines with these:
Here starts the original code snippet for domain accounts:
此代码将创建一个本地帐户,并设置密码永不过期选项:
0x10000 标志表示密码永不过期。
我花了很长时间弄清楚如何创建一个密码设置不过期的本地用户帐户。 似乎当您尝试使用:
来自活动目录的权限时就会发挥作用。 如果您具有活动目录权限,则一切正常。 如果不这样做,那么获取 userAccountControl 属性将始终导致空值。 尝试设置 userAccountControl 将导致异常“在缓存中找不到目录属性”。
然而,经过多次搜寻,我发现另一个属性“UserFlags”需要使用 Invoke 设置。 您可以使用它在本地帐户上设置标志。 我已经尝试过这段代码,它可以在 Windows Server 2008 上运行。
希望这会有所帮助
This code will create a local account with the password never expires option set:
The 0x10000 flag means PasswordNeverExpires.
I spent a long time figuring out how to create a local user account with the password set not to expire. It seems that when you try to use:
permissions from active directory come into play. If you have active directory permissions everything works fine. If you don't then getting the userAccountControl property will always result in a null value. Trying to set userAccountControl will result in an exception "The directory property cannot be found in the cache".
However after much hunting around I found another property "UserFlags" that needs to be set using Invoke. You can use this to set the flag on a local account. I've tried this code and it worked on windows server 2008.
Hope this helps
使用系统目录服务;
using System.DirectoryServices;