public void TestLocalUserWithSingleGroup() { NTHost host = NTHost.CurrentMachine; // find a unique user name NTLocalUser user = null; NTLocalGroup group = null; try { group = host.CreateLocalGroup(GenerateGroupName(host), null); user = host.CreateLocalUser(GenerateUserName(host), "P4ssw0rd123"); user.AddMembership(group.Name); Assert.IsTrue(ContainsObjectByName(user.GetMembership(), group.Name)); } finally { try { if (user != null) { user.Delete(); } } catch { } try { if (user != null) { group.Delete(); } } catch { } } }
public void TestLocalUsersWithMultipleGroups() { NTHost host = NTHost.CurrentMachine; // find a unique user name NTLocalUser user1 = null; NTLocalUser user2 = null; NTLocalGroup group1 = null; NTLocalGroup group2 = null; NTLocalGroup group3 = null; try { group1 = host.CreateLocalGroup(GenerateGroupName(host), "description"); Assert.IsNotNull(group1); group2 = host.CreateLocalGroup(GenerateGroupName(host), "description"); Assert.IsNotNull(group2); group3 = host.CreateLocalGroup(GenerateGroupName(host), "description"); Assert.IsNotNull(group3); user1 = host.CreateLocalUser(GenerateUserName(host), "P4ssw0rd123"); user2 = host.CreateLocalUser(GenerateUserName(host), "P4ssw0rd123"); user1.AddMembership(group1.Name); user1.AddMembership(group2.Name); user2.AddMembership(group1.Name); user2.AddMembership(group3.Name); NTLocalGroup[] user1Membership = user1.GetMembership(); Assert.IsNotNull(user1Membership); Assert.IsTrue(ContainsObjectByName(user1Membership, group1.Name)); Assert.IsTrue(ContainsObjectByName(user1Membership, group2.Name)); NTLocalGroup[] user2Membership = user2.GetMembership(); Assert.IsNotNull(user2Membership); Assert.IsTrue(ContainsObjectByName(user2Membership, group1.Name)); Assert.IsTrue(ContainsObjectByName(user2Membership, group3.Name)); } finally { try { if (user1 != null) { user1.Delete(); } } catch { } try { if (user2 != null) { user2.Delete(); } } catch { } try { if (group1 != null) { group1.Delete(); } } catch { } try { if (group2 != null) { group2.Delete(); } } catch { } try { if (group3 != null) { group3.Delete(); } } catch { } } }