如何使创建的本地windows用户能够从C#登录到他的帐户?

如何使创建的本地windows用户能够从C#登录到他的帐户?,c#,.net,windows,user-accounts,C#,.net,Windows,User Accounts,我已经创建了本地windows帐户,并使用System.DirectoryServices.AccountManagement命名空间中的类将其添加到组用户中。然后,我尝试登录到新创建的帐户(手动而不是从C#)并得到错误“用户配置文件服务失败无法加载用户配置文件”。我的C#代码是: 我错过了什么 我忘记写了,GetDefaultPrincipalContext()返回新的PrincipalContext(Context.Machine,null) 更新: 只是要提到,我还没有为路径C:\User

我已经创建了本地windows帐户,并使用System.DirectoryServices.AccountManagement命名空间中的类将其添加到组用户中。然后,我尝试登录到新创建的帐户(手动而不是从C#)并得到错误“用户配置文件服务失败无法加载用户配置文件”。我的C#代码是:

我错过了什么

我忘记写了,GetDefaultPrincipalContext()返回新的PrincipalContext(Context.Machine,null)

更新:
只是要提到,我还没有为路径C:\Users\NewUser上的新用户创建主目录和其中的所有内容。。。奇怪的是(在我看来),它不是自动生成的

我正在写一个答案,以防有人看到这篇文章并认为上面的代码不好。实际上,代码在我尝试运行它的其他每台windows机器上都运行得很好。我不知道我的个人电脑有什么问题,但问题肯定不在代码中。。。我将询问其他问题以解决我的windows问题

这是一个非常奇怪的错误消息。您的系统是否配置为需要使用漫游配置文件?(这是一个组策略设置。)我不确定,它有Windows 8.1的默认设置,所以我不这么认为。计算机是否已加入域?我假设用户ID字符串不包含任何奇数字符?当然可以这样创建帐户。Windows 8帐户管理中的所有云内容仍然是可选的。MS通常在向后兼容性方面非常热门,因此以编程方式创建本地帐户似乎不太可能需要在Windows8中执行任何额外步骤。我有Windows7和Windows8.1的盒子,如果我明天有机会,我会做一点实验。同时,尝试设置
newUser.Name
,如果不起作用,则设置其他可设置的属性。DeleteUser方法的作用是什么?
      try
        {
            // Create context for server and create new user if one not exists
            string userID = GetUserID(userName, userType);
            PrincipalContext serverContext = GetDefaultPrincipalContext();
            if (DoesExist(userID, serverContext))
            {
                throw new Exception("User already exists!");
            }
            UserPrincipal newUser = new UserPrincipal(serverContext, userID, userID, true);

            // Get description of user from its privilege and save user if everything went OK
            var description = GetDescriptionFromUserType(userType);
            if (null != description)
            {
                newUser.Description = description;
                newUser.Save(); 

                // Add user to group so it is displayed in Control Panel
                GroupPrincipal group = GroupPrincipal.FindByIdentity(serverContext, USERS_GROUP);
                group.Members.Add(newUser);
                group.Save();
                group.Dispose();
                newUser.Dispose();
                serverContext.Dispose();
                success = true;
            }
        }
        catch (Exception e)
        {
            Log.LogError(TYPE, e.Message);
            DeleteUser(userName, userType);
        }