Question

I've created local windows account and added it to group Users using classes from System.DirectoryServices.AccountManagement namespace. Then I tried to log into that newly created account (manually not from C#) and got error 'user profile service failed user profile cannot be loaded'. My C# code is:

      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);
        }

What i'm missing?

I've forgot to write, GetDefaultPrincipalContext() returns new PrincipalContext(Context.Machine, null)...

UPDATE: Just to mention that I haven't made home directory and all the stuff inside it for new user on path C:\Users\NewUser... And what is strange (in my opinion), one isn't made automatically.

Était-ce utile?

La solution

I'm writing an answer in case that anyone sees this post and thinks that the code above is no good. Actually code is working nicely on every other windows machine on which I tried to run it. I don't know what is the issue on my personal computer but the problem is definitely not in code... I will ask other question to resolve my windows problem...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top