Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7969531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:15:03+00:00 2026-06-04T07:15:03+00:00

In a nutshell, what I’m trying to do is create a new user, which

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:15:04+00:00Added an answer on June 4, 2026 at 7:15 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code copied from C# 4.0 in a nutshell that uses Attribute/Reflection
In a nutshell, I have a web application which used to be able to
Browsing the code sample from C# 4.0 in a nutshell I came across some
In a nutshell what Im trying to achieve is to have a re-usable DLL
I'm trying to use the PredicateBuilder, as described here - http://www.albahari.com/nutshell/predicatebuilder.aspx The following code
In a nutshell, I'm trying to do the inverse of classObject.getDeclaredClasses() . I have
In a nutshell what my program does is: it executes and takes user input
In a nutshell, I have a solution that builds fine in the IDE, and
I'm building a remote presentation tool in AS3. In a nutshell, one user (the
In a nutshell I want a callback to fire after the html has been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.