use*_*084 10 c# windows asp.net user-accounts windows-server-2008
我有这个代码来创建一个本地Windows用户
public static bool CreateLocalWindowsAccount(string username, string password, string displayName, string description, bool canChangePwd, bool pwdExpires)
{
try
{
PrincipalContext context = new PrincipalContext(ContextType.Machine);
UserPrincipal user = new UserPrincipal(context);
user.SetPassword(password);
user.DisplayName = displayName;
user.Name = username;
user.Description = description;
user.UserCannotChangePassword = canChangePwd;
user.PasswordNeverExpires = pwdExpires;
user.Save();
//now add user to "Users" group so it displays in Control Panel
GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "Users");
group.Members.Add(user);
group.Save();
return true;
}
catch (Exception ex)
{
LogMessageToFile("error msg" + ex.Message);
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的机器上试过这个工作正常.但后来我把它放在Windows服务器上.并试图在那边创建一个用户.
首先我得到错误"常规访问被拒绝错误",所以我让用户成为管理员
但现在我收到错误"找不到网络路径"
我怎么能解决这个错误..谢谢
Sco*_*ain 10
我有一个非常类似的问题,改变了第一行
PrincipalContext context = new PrincipalContext(ContextType.Machine, "127.0.0.1");
Run Code Online (Sandbox Code Playgroud)
看看是否能解决您的问题.并使用管理员权限检查程序是否正在运行.
另一个问题可能是服务器具有密码复杂性要求password,并且传入该功能的服务器不符合这些要求.如果ASfas123@!fda将密码作为密码传递,问题是否会消失?
我90%肯定这是这两个问题之一.
对于不保存的用户组,我不确定原因.这是我的一个项目中的一个snippit,它正在做同样的事情.我看不出这种差异.
using (GroupPrincipal r = GroupPrincipal.FindByIdentity(context, "Remote Desktop Users"))
using (GroupPrincipal u = GroupPrincipal.FindByIdentity(context, "Users"))
{
//snip
UserPrincipal user = null;
try
{
if (userInfo.NewPassword == null)
throw new ArgumentNullException("userInfo.NewPassword", "userInfo.NewPassword was null");
if (userInfo.NewPassword == "")
throw new ArgumentOutOfRangeException("userInfo.NewPassword", "userInfo.NewPassword was empty");
//If the user already is in the list of existing users use that one.
if (pr.ContainsKey(username))
{
user = (UserPrincipal)pr[username];
user.Enabled = true;
user.SetPassword(userInfo.NewPassword);
}
else
{
//create new windows user.
user = new UserPrincipal(context, username, userInfo.NewPassword, true);
user.UserCannotChangePassword = true;
user.PasswordNeverExpires = true;
user.Save();
r.Members.Add(user);
r.Save();
u.Members.Add(user);
u.Save();
}
IADsTSUserEx iad = (IADsTSUserEx)((DirectoryEntry)user.GetUnderlyingObject()).NativeObject;
iad.TerminalServicesInitialProgram = GenerateProgramString(infinityInfo);
iad.TerminalServicesWorkDirectory = Service.Properties.Settings.Default.StartInPath;
iad.ConnectClientDrivesAtLogon = 0;
user.Save();
}
catch(Exception e)
{
//snip
}
finally
{
if (user != null)
{
user.Dispose();
}
}
}
Run Code Online (Sandbox Code Playgroud)