3

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

'The directory property cannot be found in the cache' with an exception type of.. 'COMExceptioncrossed a native/managed boundary'.

For some reason, when I run my program directly (not through vs2010) it works fine. 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.

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);
}
eboix
  • 5,113
  • 1
  • 27
  • 38
Rich S
  • 3,248
  • 3
  • 28
  • 49
  • And what happens if you comment out the `CreateProfile`? – Wiktor Zychla Jan 25 '12 at 19:17
  • Check the windows event log, it will most likely have an event which will give you a hint as to what the problem is. – CodingGorilla Jan 25 '12 at 19:23
  • the createprofile actually created a directory in the 'users' path, so it's obviously doing something. If I comment it out, create a new user, then try to log in, it has another windows error (something along the lines of.. unable to load profile) This is how I initially did it, which prompted me to look into creating a profile. Maybe it IS to do with the return code not being 0. – Rich S Jan 25 '12 at 19:25
  • Is the directory that CreateProfile creates fully populated (Documents, Pictures, AppData, etc.)? Is the owner set properly? And what _is_ the return value of CreateProfile? – Phil N. Jan 25 '12 at 21:30
  • the error code which comes back from the User.Save is -2147467259 the CreateProfile api call returns -2147024894 – Rich S Jan 26 '12 at 00:06
  • also.. there are no errors in the event log.. it has details about the creation of the account etc, but no mention of the 'CreateProfile' call. – Rich S Jan 26 '12 at 00:11
  • @PhilN. the directory created contains 'AppData', 'Local Settings' and about 7 hidden files named ntuser.dat.* – Rich S Jan 26 '12 at 00:20
  • Ok, I've found a few other odd things.. I tried running this app on a different Windows 7 pc, and it returned 0. I then tried to create a windows user (on my machine) using the control panel, then tried to log in and it didn't let me, saying 'the user profile service service failed the login, user profile could not be loaded' (not verbatim) - Maybe there's just a problem with my PC and I've been barking up the wrong tree !! – Rich S Jan 26 '12 at 11:56
  • What should I do with this question ? it's obviously not valid, as it seems to just be a problem with my PC. – Rich S Jan 28 '12 at 12:59

1 Answers1

1

Strangely, when this is run on a server machine (i.e. 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.

Thanks for your suggestions anyway.

Rich S
  • 3,248
  • 3
  • 28
  • 49