Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Domain Controller parameter #17

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions ConfigMgrWebService/ADComputer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;

namespace ConfigMgrWebService
{
Expand All @@ -11,5 +10,33 @@ public class ADComputer
public string CanonicalName { get; set; }
public string DnsHostName { get; set; }
public string DistinguishedName { get; set; }

public ADComputer() { }

/// <summary>
/// The 2nd constructor for <see cref="ADComputer"/>. Using the specified <see cref="DirectoryEntry"/>,
/// this will populate the class's properties.
/// </summary>
/// <param name="dirEntry">The <see cref="DirectoryEntry"/> to use when populating this class's properties.</param>
public ADComputer(DirectoryEntry dirEntry)
{
using (dirEntry)
{
this.DistinguishedName = dirEntry.Properties[ConfigMgrWebService.DISTINGUISHED_NAME].Value as string;
this.CanonicalName = dirEntry.Properties[ConfigMgrWebService.COMMON_NAME].Value as string;
this.DnsHostName = dirEntry.Properties[ConfigMgrWebService.DNS_HOST_NAME].Value as string;
this.SamAccountName = dirEntry.Properties[ConfigMgrWebService.SAM_ACCOUNT_NAME].Value as string;
}
}

private ADComputer(ADComputerFromDC withDC)
{
this.CanonicalName = withDC.CanonicalName;
this.SamAccountName = withDC.SamAccountName;
this.DistinguishedName = withDC.DistinguishedName;
this.DnsHostName = withDC.DnsHostName;
}

public static explicit operator ADComputer(ADComputerFromDC compFromDC) => new ADComputer(compFromDC);
}
}
36 changes: 36 additions & 0 deletions ConfigMgrWebService/ADComputerFromDC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.DirectoryServices;

namespace ConfigMgrWebService
{
public class ADComputerFromDC
{
public string SamAccountName { get; set; }
public string CanonicalName { get; set; }
public string DnsHostName { get; set; }
public string DistinguishedName { get; set; }
public string RespondingDC { get; set; }

public ADComputerFromDC() { }

/// <summary>
/// The 2nd constructor for <see cref="ADComputerFromDC"/>. Using the specified <see cref="DirectoryEntry"/>,
/// this will populate the class's properties. It also specfies the DomainController that responding with the
/// <see cref="DirectoryEntry"/>.
/// </summary>
/// <param name="dirEntry">The <see cref="DirectoryEntry"/> to use when populating this class's properties.</param>
/// <param name="dc">The responding domain controller of the <see cref="DirectoryEntry"/>.</param>
public ADComputerFromDC(DirectoryEntry dirEntry, string dc)
{
this.RespondingDC = dc;
using (dirEntry)
{
this.DistinguishedName = dirEntry.Properties[ConfigMgrWebService.DISTINGUISHED_NAME].Value as string;
this.CanonicalName = dirEntry.Properties[ConfigMgrWebService.COMMON_NAME].Value as string;
this.DnsHostName = dirEntry.Properties[ConfigMgrWebService.DNS_HOST_NAME].Value as string;
this.SamAccountName = dirEntry.Properties[ConfigMgrWebService.SAM_ACCOUNT_NAME].Value as string;
}
}
}
}
94 changes: 93 additions & 1 deletion ConfigMgrWebService/ADDomain.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,105 @@
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Web;

namespace ConfigMgrWebService
{
public class ADDomain
public class ADDomain : IDisposable
{
private Domain _domain;
private string _typeName => this.GetType().FullName;
private bool _isDisp;

public string DomainName { get; set; }
public string DefaultNamingContext { get; set; }
public string Path { get; set; }

public ADDomain() { }

private ADDomain(Domain domain)
{
this.DomainName = domain.Name;
using (DirectoryEntry de = domain.GetDirectoryEntry())
{
this.DefaultNamingContext = de.Properties[ConfigMgrWebService.DISTINGUISHED_NAME].Value as string;
this.Path = de.Path;
}
_domain = domain;
}

public DomainControllerCollection GetAllDomainControllers()
{
this.CheckIfDisposed();
return _domain.FindAllDomainControllers();
}

public DomainController FindDomainController()
{
this.CheckIfDisposed();
return _domain.FindDomainController();
}

public bool IsDC(string computerName)
{
bool result = false;
DomainControllerCollection dcCol = this.GetAllDomainControllers();
if (dcCol.Count <= 0)
return result;

foreach (DomainController dc in dcCol)
{
using (dc)
{
using (DirectoryEntry dcEntry = dc.GetDirectoryEntry())
{
if (computerName.Equals(
dcEntry.Properties[ConfigMgrWebService.NAME].Value as string,
StringComparison.CurrentCultureIgnoreCase))
{
result = true;
break;
}
}
}
}
return result;
}

#region IDISPOSABLE METHODS
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (_isDisp)
return;

if (disposing)
_domain.Dispose();

_isDisp = true;
}

private void CheckIfDisposed()
{
if (_isDisp)
throw new ObjectDisposedException(_typeName);
}

#endregion

public static implicit operator ADDomain(Domain domain) => new ADDomain(domain);

public static implicit operator Domain(ADDomain adDomain)
{
adDomain.CheckIfDisposed();
return adDomain._domain;
}
}
}
23 changes: 22 additions & 1 deletion ConfigMgrWebService/ADGroup.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Web;

namespace ConfigMgrWebService
{
public class ADGroup
{
public string samAccountName { get; set; }
public string SamAccountName { get; set; }
public string DistinguishedName { get; set; }
public string RespondingDC { get; }

public ADGroup() { }

/// <summary>
/// The 2nd constructor for <see cref="ADGroup"/>. Using the specified <see cref="DirectoryEntry"/>,
/// this will populate the class's properties. It also specfies the DomainController that responding with the
/// <see cref="DirectoryEntry"/>.
/// </summary>
/// <param name="dirEntry">The <see cref="DirectoryEntry"/> to use when populating this class's properties.</param>
/// <param name="dc">The responding domain controller of the <see cref="DirectoryEntry"/>.</param>
public ADGroup(DirectoryEntry dirEntry, string dc)
{
this.RespondingDC = dc;
using (dirEntry)
{
this.SamAccountName = dirEntry.Properties["sAMAccountName"].Value as string;
this.DistinguishedName = dirEntry.Properties["distinguishedName"].Value as string;
}
}
}
}
58 changes: 58 additions & 0 deletions ConfigMgrWebService/ADOperations/AddADComputerToGroupByDC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Diagnostics;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Services;

namespace ConfigMgrWebService
{
public partial class ConfigMgrWebService
{
[WebMethod(Description = "")]
public bool AddADComputerToGroupByDC(string secret, string groupName, string computerName, string domainController)
{
var method = MethodBase.GetCurrentMethod();
MethodBegin(method);

//' Variable for return value
bool returnValue = false;

//' Validate secret key
if (secret == secretKey)
{
//' Log that secret key was accepted
WriteEventLog("Secret key was accepted", EventLogEntryType.Information);

//' Get AD object distinguished name for computer and group
string computerDistinguishedName = (GetADObject(computerName, ADObjectClass.Computer, ADObjectType.distinguishedName, domainController)).Remove(0, 7);
string groupDistinguishedName = GetADObject(groupName, ADObjectClass.Group, ADObjectType.distinguishedName, domainController);

if (!string.IsNullOrEmpty(computerDistinguishedName) && !string.IsNullOrEmpty(groupDistinguishedName))
{
try
{
//' Add computer to group and commit
var groupEntry = new DirectoryEntry(groupDistinguishedName);
groupEntry.Properties["member"].Add(computerDistinguishedName);
groupEntry.CommitChanges();

//' Dispose object
groupEntry.Dispose();

returnValue = true;
}
catch (Exception ex)
{
WriteEventLog(string.Format("An error occured when attempting to add a computer object in Active Directory to a group. Error message: {0}", ex.Message), EventLogEntryType.Error);
}
}
}

MethodEnd(method);
return returnValue;
}
}
}
55 changes: 55 additions & 0 deletions ConfigMgrWebService/ADOperations/AddADUserToGroupByDC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Diagnostics;
using System.DirectoryServices;
using System.Reflection;
using System.Web.Services;

namespace ConfigMgrWebService
{
public partial class ConfigMgrWebService
{
[WebMethod(Description = "Add a user in Active Directory to a specific group on the specified domain controller")]
public bool AddADUserToGroupByDC(string secret, string groupName, string userName, string domainController)
{
MethodBase method = MethodBase.GetCurrentMethod();
MethodBegin(method);

//' Variable for return value
bool returnValue = false;

//' Validate secret key
if (secret == secretKey)
{
//' Log that secret key was accepted
WriteEventLog("Secret key was accepted", EventLogEntryType.Information);

//' Get AD object distinguished name for computer and group
string userDistinguishedName = (GetADObject(userName, ADObjectClass.User, ADObjectType.distinguishedName)).Remove(0, 7);
string groupDistinguishedName = GetADObject(groupName, ADObjectClass.Group, ADObjectType.distinguishedName);

if (!String.IsNullOrEmpty(userDistinguishedName) && !String.IsNullOrEmpty(groupDistinguishedName))
{
try
{
//' Add user to group and commit
DirectoryEntry groupEntry = new DirectoryEntry(groupDistinguishedName);
groupEntry.Properties["member"].Add(userDistinguishedName);
groupEntry.CommitChanges();

//' Dispose object
groupEntry.Dispose();

returnValue = true;
}
catch (Exception ex)
{
WriteEventLog(String.Format("An error occured when attempting to add an user object in Active Directory to a group. Error message: {0}", ex.Message), EventLogEntryType.Error);
}
}
}

MethodEnd(method);
return returnValue;
}
}
}
Loading