Example #1
0
        /// <summary>
        /// Authenticates the user
        /// </summary>
        /// <param name="CompanyID">Company ID</param>
        /// <param name="UserID">User ID</param>
        /// <param name="Password">Password</param>
        /// <returns>True if authenticated successfully</returns>
        public bool AuthenticateUser(string CompanyID, string UserID, string Password)
        {
            bool boolReturn = false;

            boolReturn = (from user in dataConn.Users
                          where (user.UserID == UserID && user.Password == Password) || (user.UserID == UserID && user.IgnorePassword == true)
                          select user).Count() > 0;

            if (boolReturn == false && dataConn.Users.Count(user => user.UserID == UserID) > 0)
            {
                using (System.DirectoryServices.AccountManagement.PrincipalContext pContext = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain))
                {
                    boolReturn = pContext.ValidateCredentials(UserID, Password);

                    if (boolReturn)
                    {
                        var varUser = (from usr in dataConn.Users where usr.UserID == UserID select usr).First();
                        varUser.Password = Password;
                        dataConn.SaveChanges();
                    }
                }
            }

            return boolReturn;
        }
Example #2
0
        public bool DomainLogin(string pLogin, string pPassWord)
        {
#pragma warning disable CA1416 // Validate platform compatibility
            System.DirectoryServices.AccountManagement.PrincipalContext prCont = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, "Vopak");
            return(prCont.ValidateCredentials(pLogin, pPassWord));

#pragma warning restore CA1416 // Validate platform compatibility
        }
Example #3
0
        public static bool UpdateUserInfo()
        {
            try
            {
                string domain = GetFqd((!string.IsNullOrEmpty(_UserName) && _UserName.Contains("\\") ? _UserName.Split('\\')[0] : string.Empty));
                using (System.DirectoryServices.AccountManagement.PrincipalContext ctx = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, domain))
                {
                    // ReSharper disable once UnusedVariable
                    bool val = ctx.ValidateCredentials(null, null, System.DirectoryServices.AccountManagement.ContextOptions.Negotiate);
                    using (System.DirectoryServices.AccountManagement.UserPrincipal up = System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(ctx, _UserName))
                    {
                        // ReSharper disable once UnusedVariable
                        if (up != null)
                        {
                            using (System.DirectoryServices.DirectoryEntry de =
                                       (System.DirectoryServices.DirectoryEntry)up.GetUnderlyingObject())
                            {
                                //string adProperty = "";
                                //if (!de.Properties.Contains(adProperty))
                                //{
                                //    throw new Exception(String.Format("Property {0} does not exist for user {1}", adProperty, userID.Name));
                                //}
                                //switch(adProperty.ToLower()) {
                                //    case "accountexpires":
                                //    case "badpasswordtime":
                                //    case "lastlogon":
                                //    case "pwlastset":
                                //    case "whencreated":  //examples of AD date fields
                                //        DateTime adVal = SDHelpers.ADHelper.FromADDate(de.Properties[adProperty].Value);
                                //        //example of setting an AD Date value
                                //        //de.Properties[adProperty].Value = SDHelpers.ADHelper.ToADDate(DateTime.FromFileTimeUtc(0)); //0 for never(1 / 1 / 1601)-- i.e. for account expiration
                                //        //de.Properties[adProperty].Value = SDHelpers.ADHelper.ToADDate(DateTime.Now.AddMonths(3));   //actual date value
                                //        break;
                                //    case "objectguid":  //example of binary array (GUID) values
                                //        string adVal = SDHelpers.ADHelper.FromBinaryArray((byte[])de.Properties[adProperty].Value);   //equivilent to SDHelpers.ADHelper.FromBinaryArray(de.Properties(adProperty).Value, True)
                                //        //for no hypens
                                //        //string adVal = SDHelpers.ADHelper.FromBinaryArray((byte[])de.Properties[adProperty].Value, false);
                                //        break;
                                //    default:
                                //        string adVal = (string)de.Properties[adProperty].Value;
                                //        //example of setting an AD value
                                //        //de.Properties[adProperty].Value = "somevalue";
                                //        break;
                                //}
                                //  de.CommitChanges();   //save the changes
                                return(true);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                return(false);   //error creating the windowidentity object -- not a valid user
            }

            return(false);
        }
Example #4
0
 public static bool Validate_Principal(string userName, string pwd, string domain)
 {
     using (var pc = new System.DirectoryServices.AccountManagement
                     .PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, domain))
     {
         // validate the credentials
         return(pc.ValidateCredentials(userName, pwd));
     }
 }
Example #5
0
 private bool ValidateCredentials(string userName, string pwd)
 {
     if (NoPwdCheck)
     {
         return(true);
     }
     using (var pc = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, Domain))
     {
         // validate the credentials
         return(pc.ValidateCredentials(userName, pwd));
     }
 }
Example #6
0
 private static System.DirectoryServices.AccountManagement.PrincipalContext EstablishConnection()
 {
     try
     {
         System.DirectoryServices.AccountManagement.PrincipalContext adContext = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, Hostname, BaseDN, @Username, Password);
         Boolean result = adContext.ValidateCredentials(Username, Password);
         if (result)
         {
             log.Info("Successfully Established connection to AD '" + Hostname + "' with username '" + Username + "'");
             return(adContext);
         }
         return(null);
     }
     catch (Exception e)
     {
         log.Error("Exception in Establish Connection to AD '" + Hostname + "' with username '" + Username + "'");
         Console.Write("\nError in establish connection: " + e.Message);
         throw new Exception();
     }
 }
    public static bool Authenticate(string user_, string password_)
    {
      bool ret;
      try
      {
        using (var pc = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, DOMAIN))
        {
          ret = pc.ValidateCredentials(user_, password_);
        }

        if (ret)
          UserName = user_.ToLower();
      }
      catch (Exception ex_)
      {
        Logger.Error("Error trying to authenticate user", typeof (SymmetryEnvironment), ex_);
        ret = false;
      }

      return ret;
    }
Example #8
0
        public bool            validatePassword()
        {
            string domainName;
            string userName;

            int i = AccountName.IndexOf('\\');

            if (i >= 0)
            {
                domainName = AccountName.Substring(0, i);
                userName   = AccountName.Substring(i + 1);
            }
            else
            {
                domainName = null;
                userName   = AccountName;
            }

            using (System.DirectoryServices.AccountManagement.PrincipalContext pc = new System.DirectoryServices.AccountManagement.PrincipalContext(domainName != null ? System.DirectoryServices.AccountManagement.ContextType.Domain : System.DirectoryServices.AccountManagement.ContextType.Machine, domainName))
                return(pc.ValidateCredentials(userName, AccountPassword));
        }
Example #9
0
 private bool Validate_User(string username, string password)
 {
     bool valid = false;
     using (System.DirectoryServices.AccountManagement.PrincipalContext context = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain))
     {
         valid = context.ValidateCredentials(username, password);
     }
     return valid;
 }