リモート コンピューターでユーザーを作成しようとすると、以下のエラーが発生します。
System.UnauthorizedAccessException: System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo() での一般的なアクセス拒否エラー System.DirectoryServices.DirectoryEntry.RefreshCache() で System.DirectoryServices.AccountManagement.PrincipalContext.DoMachineInit() で System.DirectoryServices.AccountManagement .PrincipalContext.Initialize() の System.DirectoryServices.AccountManagement.PrincipalContext.ContextForType(Type t) の System.DirectoryServices.AccountManagement.Principal.GetStoreCtxToUse() の System.DirectoryServices.AccountManagement.Principal.set_DisplayName(String value) の testemail.CreateLocalWindowsAccount (文字列のユーザー名、文字列のパスワード、文字列の表示名、文字列の説明、ブール値の canChangePwd、ブール値の pwdExpires)
これがコードです。
public void CreateLocalWindowsAccount(string username, string password, string displayName, string description, bool canChangePwd, bool pwdExpires)
{
try
{
PrincipalContext context = new PrincipalContext(ContextType.Machine, "127.0.0.1");
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, "Remote Desktop Users");
group.Members.Add(user);
group.Save();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}