简体   繁体   English

以编程方式创建Windows用户c#.net(使用PricinpalUser / CreateProfile)

[英]Create Windows User programmatically c# .net (using PricinpalUser / CreateProfile)

In a nutshell, what I'm trying to do is create a new user, which has the ability to log in. 简而言之,我要做的是创建一个具有登录功能的新用户。

I have plucked code from various sources, and tried to simplify it. 我从各种来源中提取代码,并试图简化它。 However, I'm hitting a few stumbling blocks. 但是,我正在遇到一些绊脚石。

When I call UserPrincipal.Save() - it gives me an error 当我调用UserPrincipal.Save() - 它给了我一个错误

'The directory property cannot be found in the cache' with an exception type of.. 'COMExceptioncrossed a native/managed boundary'. '在缓存中找不到目录属性',例外类型为..'COMExceptioncrossed native / managed boundary'。

For some reason, when I run my program directly (not through vs2010) it works fine. 出于某种原因,当我直接运行我的程序(而不是通过vs2010)时,它运行正常。 So I can get around that ! 所以我可以解决这个问题!

My main problem though, is that even though everything seems ok, when I try to log in, it comes up with the message 'loading desktop' or whatever it is, and then just says 'logging out'. 我的主要问题是,即使一切似乎都没问题,当我尝试登录时,它会显示消息“加载桌面”或其他任何内容,然后只是说“退出”。 So it's almost as if the profile hasn't been set up correctly. 所以几乎就像配置文件没有正确设置一样。

The return value from the API 'CreateProfile' isn't 0, so maybe that's causing a problem. API“CreateProfile”的返回值不为0,因此可能会导致问题。

Is there anything else I need to do ? 还有什么我需要做的吗?

My Code is... 我的代码是......

private void Run(string un, string pw)
{
    UserPrincipal NewUP = CreateUser(un, pw);
    AddGroup(NewUP, "Users");
    AddGroup(NewUP, "HomeUsers");
    CreateProfile(NewUP);
}
private UserPrincipal CreateUser(string Username, string Password)
{
    PrincipalContext pc = new PrincipalContext(ContextType.Machine, Environment.MachineName);
    UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, Username);
    if (up == null)
    {
        up = new UserPrincipal(pc, Username, Password, true);
        up.UserCannotChangePassword = false;
        up.PasswordNeverExpires = false;
        up.Save(); // this is where it crashes when I run through the debugger
    }
    return up;
}
private void AddGroup(UserPrincipal Up, string GroupName)
{
    PrincipalContext pc = new PrincipalContext(ContextType.Machine, Environment.MachineName);
    GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, GroupName);
    if (!gp.Members.Contains(Up))
    {
        gp.Members.Add(Up);
        gp.Save();
    }
    gp.Dispose();
}
private void CreateProfile(UserPrincipal Up)
{
    int MaxPath = 240;
    StringBuilder pathBuf = new StringBuilder(MaxPath);
    uint pathLen = (uint)pathBuf.Capacity;
    int Res = CreateProfile(Up.Sid.ToString(), Up.SamAccountName, pathBuf, pathLen);
}

Strangely, when this is run on a server machine (ie not my development machine) it works fine. 奇怪的是,当它在服务器机器上运行时(即不是我的开发机器),它工作正常。 I've got a feeling this is something to do with Windows 7, or my particular installation of it. 我有一种感觉,这与Windows 7或我的特定安装有关。

Thanks for your suggestions anyway. 无论如何,谢谢你的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM