Skip to content
jarzynam edited this page Jun 5, 2019 · 23 revisions

This package provides functionality for managing local user's accounts and user's local groups in basic scenarios.

How to start

Before you start see main prerequisites.

var factory = new ContinuousManagementFactory();
    
var _userShell = factory.UserShell();
var _groupShell = factory.LocalUserGroup();

Create user

IUserShell:

Add new user:

// in package older than 1.2.1 use UserModel class
var user =  new LocalUserCreateModel 
{
    Name = "testUser1",
    FullName = "Test User 1",
    Description = "Delete this user after tests",
    Password = "Test123",
    Expires = new DateTime(2018, 01, 01)
};
_userShell.Create(user);

Leave Expires = null if user account should never expire. If you try add user with already existing name, the MethodInvocationException will occur.

Remove user

IUserShell:

Remove existing user:

_userShell.Remove(userName);

if you try remove non-existing user the MethodInvocationException will occur.

Remove existing user with windows user profile:

_userShell.Remove(userName, true);

if you try remove non-existing user the MethodInvocationException will occur.

LocalUserInfo (v2.0):

Remove existing user:

var user = new LocalUserInfo("userName");
user.Remove();

if you try to create user instance for non-existing user or remove already removed user the MethodInvocationException will occur.

Remove existing user with windows user profile:

user.RemoveWithProfile();

if you try to create user instance for non-existing user or remove already removed user the MethodInvocationException will occur.

Get user

IUserShell:

Get existing user:

// in package older than v1.2.1 use _userShell.Get(userName);
LocalUserInfo user = _userShell.GetLocalUser(userName);

Returns null if user is not existing.

LocalUserInfo (v2.0):

Get existing user:

var user = new LocalUserInfo("userName");

Throws MethodInvocationException if user doesn't exists.

Refresh user

LocalUserInfo (v2.0):

Refresh properties in current user instance:

var user = new LocalUserInfo("userName");
user.Refresh();

Throws MethodInvocationException if user doesn't exists.

Get logged in user

IUserShell:

Get user which is currently logged in:

LocalUserInfo user = _userShell.GetLoggedInUser();

Get all users

IUserShell:

Get all users from current domain:

List<LocalUserInfo> users = _userShell.GetAllUsers();

Exists user

IUserShell:

Check if user exists

bool exists = _userShell.Exists(userName);

Returns true if user exists.

LocalUserInfo (v2.0):

Check if user exists

var user = new LocalUserInfo("userName"); // throws exception when user doesn't exist
bool exists = user.Exists(); // if user has been removed, returns false

Returns true if user still existing.

Change user password

IUserShell:

Change existing user password:

// not secure
_userShell.ChangePassword("userName", "newPassword");

// secure
var securePassword = new SecurePassword();
securePassword.AppendChar('n');
securePassword.AppendChar('W');
securePassword.AppendChar('x');

_shell.ChangePassword("userName", securePassword);

If you try change password in non-existing user the MethodInvocationException will occur.

LocalUserInfo (v2.0):

Change password in current user:

var securePassword = new SecurePassword();
securePassword.AppendChar('n');
securePassword.AppendChar('W');
securePassword.AppendChar('x');

var user = new LocalUserInfo("userName");

user.Change()
    .Password(securePassword)
    .Apply();

If you try change password in non-existing user the MethodInvocationException will occur.

Set PasswordRequired

IUserShell:

Specify whether the password will required at user logon:

_userShell.SetPasswordRequired(userName, false);

User as default has set this value to true.
If user is not existing, the MethodInvocationException will occur.

LocalUserInfo (v2.0):

Specify whether the password will required at user logon:

var user = new LocalUserInfo("userName");

user.Change()
    .PasswordRequired(false)
    .Apply();

If user is not existing, the MethodInvocationException will occur.

Set PasswordCanBeChangedByUser

IUserShell:

Specify whether user be able to change his password:

_userShell.SetPasswordCanBeChangedByUser(userName, false);

User as default has set this value to true.
If user is not existing, the MethodInvocationException will occur.

LocalUserInfo (v2.0):

Specify wheter the password can be changed by user:

var user = new LocalUserInfo("userName");

user.Change()
    .PasswordCanBeChangedByUser(false)
    .Apply();

If user is not existing, the MethodInvocationException will occur.

Set PasswordCanExpire

IUserShell:

Specify whether the user's password can expire:

_userShell.SetPasswordCanExpire(userName, false);

User as default has set this value to true.
If user is not existing, the MethodInvocationException will occur.

LocalUserInfo (v2.0):

Specify wheter the password can expire:

var user = new LocalUserInfo("userName");

user.Change()
    .PasswordCanExpire(true)
    .Apply();

If user is not existing, the MethodInvocationException will occur.

Set PasswordExpired

IUserShell:

Specify whether the user's password is expired:

_userShell.SetPasswordExpired(userName, true);

If true, user will be forced to change the password at next login.
If user is not existing, the MethodInvocationException will occur.

LocalUserInfo (v2.0):

Specify whether the user's password is expired:

var user = new LocalUserInfo("userName");

user.Change()
    .PasswordExpired(true)
    .Apply();

If true, user will be forced to change the password at next login.
If user is not existing, the MethodInvocationException will occur.

Set AccountDisabled

IUserShell:

Specify whether the user's account is disabled:

_userShell.SetAccountDisabled(userName, true);

If true, the account will be disabled and user will not be able to login.
If user is not existing, the MethodInvocationException will occur.

LocalUserInfo (v2.0):

Specify whether the user's account is disabled:

var user = new LocalUserInfo("userName");

user.Change()
    .AccountDisabled(true)
    .Apply();

If true, the account will be disabled and user will not be able to login.
If user is not existing, the MethodInvocationException will occur.

Set UserVisibility

IUserShell:

Set user visibility in windows welcome (login) screen:

_userShell.SetUserVisibility(userName, false); //hide user

If false, user will be hidden in windows welcome screen. As default, visibility is always set to true.
If user is not existing, the MethodInvocationException will occur.

LocalUserInfo (v2.0):

Set user visibility in windows welcome (login) screen: Specify whether the user's account is disabled:

var user = new LocalUserInfo("userName");

user.Change()
    .IsVisible(false)
    .Apply();

If false, user will be hidden in windows welcome screen. As default, visibility is always set to true.
If user is not existing, the MethodInvocationException will occur.

Set Description

IUserShell:

Set user description:

_userShell.SetUserDescription(userName, "description"); 

If user is not existing, the MethodInvocationException will occur.

LocalUserInfo (v2.0):

Set user description:

var user = new LocalUserInfo("userName");

user.Change()
    .Description("description")
    .Apply();

If user is not existing, the MethodInvocationException will occur.

Set FullName

IUserShell:

Set user full name:

_userShell.SetUserFullName("userName", "user full name");

If user is not existing, the MethodInvocationException will occur.

LocalUserInfo (v2.0):

Change user full name:

var user = new LocalUserInfo("userName");

user.Change()
    .FullName("new full name")
    .Apply();

If user is not existing, the MethodInvocationException will occur.

Create local user group

Create new local group:

_groupShell.Create(groupName, groupDescription);

If you try add group with already existing name, the MethodInvocationException will occur.

Remove local user group

Remove existing group:

_groupShell.Remove(groupName);

If you try remove non-existing group, the MethodInvocationException will occur.

Get local user group

Get existing group:

var group = _groupShell.Get("groupName");

With this method you can get group group.Name, group.Description and all user names who are assigned to this group as group.Members.
Return null if group not exists.

Get local user group by SID

Get existing group by SID property:

var group = _groupShell.GetBySid("sid");

With this method you can get group group.Name, group.Description and all user names who are assigned to this group as group.Members.
Return null if group not exists.

Assign users to local user group

assign list of users to existing group:

var groupName = "testGroup";
var users = new List<string> { "User1" };
    
_groupShell.AssignUsers(groupName, users);

If you try assign empty or null list of users or provide invalid groupName, the MethodInvocationException will occur.

Assign single user to local user group

assign single user to existing group:

var groupName = "testGroup";
var user = "User1";
    
_groupShell.AssignUser(groupName, user);

If you try assign not existing user or provide invalid groupName, the MethodInvocationException will occur.

Remove users from local group

remove assigned users from existing group:

var groupName = "testGroup";
var users = new List<string> { "User1" };
    
_groupShell.RemoveUsers(groupName, users);

If you try remove empty or null user's list or provide invalid groupName, the MethodInvocationException will occur.

Remove user from local group

remove assigned user from existing group:

var groupName = "testGroup";
var user =  "User1";
    
_groupShell.RemoveUser(groupName, user);

If you try remove not exiting user or provide invalid groupName, the MethodInvocationException will occur.

Get assigned users to group

Get all user names assigned to specific group:

var groupName = "testGroup";
List<string> userNames;

userNames = _groupShell.GetMembers(groupName);

If you provide invalid groupName, the MethodInvocationException will occur.