public BaseResponse(T responseResult, ADResponseType rt, string msg, Exception e) : this(responseResult, rt, msg)
 {
     if (null != e)
     {
         exceptionMessage    = e.Message;
         exceptionStackTrace = e.StackTrace;
     }
 }
 public BaseResponse(T responseResult, ADResponseType rt, string msg)
 {
     response     = responseResult;
     responseType = rt;
     message      = msg;
 }
        /// <summary>
        /// If user does not exist, this method will create a user in the specified Organizational Unit.
        /// </summary>
        /// <param name="s">User information</param>
        /// <param name="path">Distingushed Name of the Organizational Unit.</param>
        /// <param name="login">Login information to Active Directory.</param>
        /// <returns>ObjectGUID (within BaseResponse) created by Active Directory.</returns>
        public BaseResponse <Guid> CreateUser(UserInfoModel s, string LDAPPath)
        {
            ADResponseType rt         = ADResponseType.Undefined;
            Guid           rResult    = Guid.Empty;
            string         rMessage   = string.Empty;
            Exception      rException = null;
            ADLoginModel   login      = GetAdminActiveDirectoryLogin();

            try
            {
                if (!DoesUserExist(s))
                {
                    using (var pc = new PrincipalContext(ContextType.Domain, DomainController, GetDomainContainer(LDAPPath), ContextOptions.SimpleBind, login.Username, login.Password))
                    {
                        using (var up = new UserPrincipal(pc))
                        {
                            up.SamAccountName    = s.Username;
                            up.GivenName         = s.FirstName;
                            up.Surname           = s.LastName;
                            up.MiddleName        = s.MiddleName;
                            up.EmailAddress      = s.DomainEmailAddress;
                            up.UserPrincipalName = s.DomainEmailAddress;
                            up.DisplayName       = String.Format("{0} {1}", s.FirstName, s.LastName);

                            up.SetPassword(s.Password);
                            up.PasswordNeverExpires = true;
                            up.Enabled = true;

                            up.Save();

                            ((DirectoryEntry)up.GetUnderlyingObject()).Properties[SSOUsersIDProperty].Value = s.ID.ToString();
                            up.Save();

                            rResult = ((DirectoryEntry)up.GetUnderlyingObject()).Guid;
                            rt      = ADResponseType.OK;
                        }
                    }
                }
                else
                {
                    rMessage = "User already exists.";
                    rt       = ADResponseType.Warning;
                }
            }
            catch (System.DirectoryServices.ActiveDirectory.ActiveDirectoryOperationException E)
            {
                rMessage   = "Unable to perform operation.";
                rt         = ADResponseType.Exception;
                rException = E;
            }
            catch (DirectoryServicesCOMException E)
            {
                Console.WriteLine(String.Format("EXCEPTION : {0}\r\n{1}", E.Message, E.StackTrace));
                rMessage   = "Unable to set password.";
                rt         = ADResponseType.Exception;
                rException = E;
            }
            catch (System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectNotFoundException E)
            {
                rMessage   = "There is a problem with the LDAP string.";
                rt         = ADResponseType.Exception;
                rException = E;
            }
            catch (System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectExistsException E)
            {
                rMessage   = "User already exists.";
                rt         = ADResponseType.Exception;
                rException = E;
            }
            catch (System.Reflection.TargetInvocationException E)
            {
                rMessage   = "Password does not meet requirements.";
                rt         = ADResponseType.Exception;
                rException = E;
            }
            return(new BaseResponse <Guid>(rResult, rt, rMessage, rException));
        }