当前位置: 首页>>代码示例>>C#>>正文


C# UserPrincipal.ExpirePasswordNow方法代码示例

本文整理汇总了C#中System.DirectoryServices.AccountManagement.UserPrincipal.ExpirePasswordNow方法的典型用法代码示例。如果您正苦于以下问题:C# UserPrincipal.ExpirePasswordNow方法的具体用法?C# UserPrincipal.ExpirePasswordNow怎么用?C# UserPrincipal.ExpirePasswordNow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.DirectoryServices.AccountManagement.UserPrincipal的用法示例。

在下文中一共展示了UserPrincipal.ExpirePasswordNow方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: AddUser

        public static void AddUser(SBSUser user)
        {
            UserPrincipal userPrincipal = new UserPrincipal(Context);
            //if (lastName != null && lastName.Length > 0)
            userPrincipal.Surname = user.UserName;

            //if (firstName != null && firstName.Length > 0)
            userPrincipal.GivenName = user.UserName;

            //if (employeeID != null && employeeID.Length > 0)
            //    userPrincipal.EmployeeId = employeeID;

            //if (emailAddress != null && emailAddress.Length > 0)
            userPrincipal.EmailAddress = user.Email;

            //if (telephone != null && telephone.Length > 0)
            //    userPrincipal.VoiceTelephoneNumber = telephone;

            //if (userLogonName != null && userLogonName.Length > 0)
            userPrincipal.SamAccountName = user.UserName;

            string pwdOfNewlyCreatedUser = user.PassWord;
            userPrincipal.SetPassword(pwdOfNewlyCreatedUser);

            userPrincipal.Enabled = true;
            userPrincipal.ExpirePasswordNow();

            userPrincipal.Save();
        }
开发者ID:cpm2710,项目名称:cellbank,代码行数:29,代码来源:UserHelper.cs


示例2: createUser

 /// <summary>
 /// Creates an Active Directory user using a firstName.lastName convention in the default Users container for the domain 
 /// </summary>
 /// <param name="firstName"></param>
 /// <param name="lastName"></param>
 /// <returns>UserPrincipal</returns>
 public static UserPrincipal createUser(string firstName, string lastName, string password)
 {
     try
     {
         PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, Properties.Settings.Default.domain, Properties.Settings.Default.domainLDAPbase);
         UserPrincipal newUser = new UserPrincipal(domainContext);
         newUser.GivenName = new CultureInfo("en-US").TextInfo.ToTitleCase(firstName);
         newUser.Surname = new CultureInfo("en-US").TextInfo.ToTitleCase(lastName);
         string display = new CultureInfo("en-US").TextInfo.ToTitleCase(firstName + " " + lastName);
         newUser.Name = display;
         newUser.DisplayName = display;
         newUser.SamAccountName = firstName.ToLowerInvariant() + "." + lastName.ToLowerInvariant();
         newUser.Save();
         newUser.SetPassword(password);
         newUser.ExpirePasswordNow();
         newUser.Enabled = true;
         return newUser;
     }
     catch (System.DirectoryServices.DirectoryServicesCOMException ex)
     {
         throw ex;
     }
 }
开发者ID:macnlinux,项目名称:userManagement,代码行数:29,代码来源:userManagement.cs


示例3: CreateNewLdapAccount

        public AccountStatus CreateNewLdapAccount(UserInfo userInfo, out string errorText, bool pswdPolicyChk = false)
        {
            errorText = string.Empty;
            if (LdapHelper.LdapAccountExists(userInfo, RootPrincipal))
            {
                return AccountStatus.AccountAlreadyExists;
            }

            try
            {
                userInfo.FirstName = LdapHelper.EscapeChars(userInfo.FirstName);
                userInfo.LastName = LdapHelper.EscapeChars(userInfo.LastName);
                var preNewUserInfo = LdapHelper.GetUniqueFirstNameLastName(userInfo, RootPrincipal);
                var newUser = new UserPrincipal(RootPrincipal)
                {
                    SamAccountName = preNewUserInfo.SamName,
                    DisplayName = String.Format("{0} {1}", preNewUserInfo.FirstName, preNewUserInfo.LastName),
                    Surname = preNewUserInfo.LastName,
                    GivenName = preNewUserInfo.FirstName,
                    UserPrincipalName = preNewUserInfo.Email,
                    EmailAddress = preNewUserInfo.Email,
                };

                if (!String.IsNullOrEmpty(userInfo.Password))
                {
                    newUser.Enabled = true;
                    newUser.PasswordNeverExpires = true;
                    newUser.SetPassword(userInfo.Password);
                }
                else
                {
                    newUser.ExpirePasswordNow();
                }
                newUser.Save();
                return AccountStatus.NewAccount;
            }
            catch (Exception ex)
            {
                errorText = String.Format("Exception creating LDAP account for {0} with exception {1}", userInfo.Email, ex.Message);
                return AccountStatus.AccountCreationFailed;
            }
        }
开发者ID:semsogutlu,项目名称:adlayer,代码行数:42,代码来源:Ldap.cs


示例4: SaveUser

        //public static void GetEmployeeManager(Employee emp, out string managerUsername)
        //{
        //    managerUsername = String.Empty;
        //    if (emp.Department == null) return;
        //    if (!Department.CheckUserIsChief(emp.Department.Id, emp.Id))
        //    {
        //        managerUsername = emp.Manager != null ? GetLoginFromEmail(emp.Manager.Email) : String.Empty;
        //    }
        //    if (String.IsNullOrEmpty(managerUsername))
        //    {
        //        Department currDep = new Department(emp.Department.Id);
        //        GetParentDepChief(currDep, out managerUsername);
        //    }
        //}
        //public static void GetParentDepChief(Department dep, out string managerUsername)
        //{
        //    managerUsername = String.Empty;
        //    if (dep.ParentDepartment != null && dep.ParentDepartment.Id > 0)
        //    {
        //        var parentDep = new Department(dep.ParentDepartment.Id);
        //        var overManager = new Employee(parentDep.Chief.Id);
        //        managerUsername = GetLoginFromEmail(overManager.Email);
        //        if (String.IsNullOrEmpty(managerUsername))
        //        {
        //            GetParentDepChief(parentDep, out managerUsername);
        //        }
        //    }
        //}
        public static string SaveUser(Employee emp)
        {
            if (!emp.HasAdAccount) return String.Empty;
            using (WindowsImpersonationContextFacade impersonationContext
                = new WindowsImpersonationContextFacade(
                    nc))
            {
                string username = String.IsNullOrEmpty(emp.AdLogin) ? GetLoginFromEmail(emp.Email) : StringHelper.Trim(emp.AdLogin);

                if (String.IsNullOrEmpty(username.Trim())) return string.Empty;
                string mail = StringHelper.Trim(emp.Email);
                string fullName = StringHelper.Trim(emp.FullName);
                string surname = StringHelper.Trim(emp.Surname);
                string name = StringHelper.Trim(emp.Name);
                string position = emp.Position != null ? emp.Position.Id > 0 && String.IsNullOrEmpty(emp.Position.Name) ? new Position(emp.Position.Id).Name : emp.Position.Name : String.Empty;
                string workNum = StringHelper.Trim(emp.WorkNum);
                string mobilNum = StringHelper.Trim(emp.MobilNum);
                string city = emp.City != null ? StringHelper.Trim(emp.City.Name) : String.Empty;
                string org = emp.Organization != null ? emp.Organization.Id > 0 && String.IsNullOrEmpty(emp.Organization.Name) ? new Organization(emp.Organization.Id).Name : emp.Organization.Name : String.Empty;
                string dep = emp.Department != null ? emp.Department.Id > 0 && String.IsNullOrEmpty(emp.Department.Name) ? new Department(emp.Department.Id).Name : emp.Department.Name : String.Empty;
                var photo = emp.Photo != null && emp.Photo.Length > 0 ? emp.Photo : null;
                Employee manager = new Employee(emp.Manager.Id);
                string managerUsername = String.IsNullOrEmpty(manager.AdLogin)
                    ? GetLoginFromEmail(manager.Email)
                    : manager.AdLogin;
                //GetEmployeeManager(emp, out managerUsername);
                string managerName = String.Empty;
                bool userIsExist = false;

                DirectoryEntry directoryEntry = new DirectoryEntry(DomainPath);

                using (directoryEntry)
                {
                    //Если пользователь существует
                    DirectorySearcher search = new DirectorySearcher(directoryEntry);
                    search.Filter = String.Format("(&(objectClass=user)(sAMAccountName={0}))", username);
                    SearchResult resultUser = search.FindOne();
                    userIsExist = resultUser != null && resultUser.Properties.Contains("sAMAccountName");
                }

                if (!String.IsNullOrEmpty(managerUsername.Trim()))
                {
                    using (directoryEntry)
                    {
                        DirectorySearcher search = new DirectorySearcher(directoryEntry);
                        search.Filter = String.Format("(&(objectClass=user)(sAMAccountName={0}))", managerUsername);
                        search.PropertiesToLoad.Add("DistinguishedName");
                        SearchResult resultManager = search.FindOne();
                        if (resultManager != null)
                            managerName = (string)resultManager.Properties["DistinguishedName"][0];
                    }
                }

                if (!userIsExist)
                {
                    //Создаем аккаунт в AD
                    using (
                        var pc = new PrincipalContext(ContextType.Domain, "UN1T", "OU=Users,OU=UNIT,DC=UN1T,DC=GROUP"))
                    {
                        using (var up = new UserPrincipal(pc))
                        {
                            up.SamAccountName = username;
                            up.UserPrincipalName = username + "@unitgroup.ru";
                            up.SetPassword("z-123456");
                            up.Enabled = true;
                            up.ExpirePasswordNow();

                            try
                            {
                                up.Save();
                            }
                            catch (PrincipalOperationException ex)
//.........这里部分代码省略.........
开发者ID:aleks19921015,项目名称:UnitApis,代码行数:101,代码来源:AdHelper.cs


示例5: AddADUser

        /// <summary>
        /// Create AD User in a container
        /// </summary>
        /// <param name="userinfo">ADUser object</param>
        /// <returns></returns>
        public ResponseMessage AddADUser( RequestUserCreate userinfo )
        {
            ResponseMessage status = new ResponseMessage();

            status.IsSuccessful = false;
            status.Message = string.Empty;

            Session stat = ValidateSession( userinfo.DomainInfo.SessionKey );

            if ( stat.IsAuthenticated == true )
            {

                PrincipalContext principalContext = null;

                string uri = FixADURI( userinfo.DomainInfo.ADHost , userinfo.DomainInfo.ContainerPath );

                if ( string.IsNullOrWhiteSpace( uri ) )
                {
                    status.Message = status.Message = "AD Host is not allowed to be empty, kindly provide the AD Host";
                    return status;
                }

                bool isAllowWite = CheckWriteOermission( uri , userinfo.DomainInfo.BindingUserName , userinfo.DomainInfo.BindingUserPassword );

                try
                {
                    UserPrincipal usr = FindADUser( userinfo.UserLogonName , userinfo.DomainInfo );
                    if ( usr != null )
                    {
                        status.Message = " user already exists. Please use a different User Logon Name";
                        return status;
                    }
                    else
                    {
                        principalContext = new PrincipalContext( ContextType.Domain , userinfo.DomainInfo.DomainName , userinfo.DomainInfo.ContainerPath , userinfo.DomainInfo.BindingUserName , userinfo.DomainInfo.BindingUserPassword );
                    }
                }
                catch ( Exception ex )
                {
                    status.Message = @"Failed to create PrincipalContext: " + ex;
                    return status;
                }

                // Create the new UserPrincipal object
                UserPrincipal userPrincipal = new UserPrincipal( principalContext );

                if ( !string.IsNullOrWhiteSpace( userinfo.LastName ) )
                    userPrincipal.Surname = userinfo.LastName;

                if ( !string.IsNullOrWhiteSpace( userinfo.FirstName ) )
                    userPrincipal.GivenName = userinfo.FirstName;

                if ( !string.IsNullOrWhiteSpace( userinfo.LastName ) && !string.IsNullOrWhiteSpace( userinfo.FirstName ) )
                    userPrincipal.DisplayName = userinfo.FirstName + " " + userinfo.LastName;

                if ( !string.IsNullOrWhiteSpace( userinfo.Description ) )
                    userPrincipal.Description = userinfo.Description;

                if ( !string.IsNullOrWhiteSpace( userinfo.EmployeeID ) )
                    userPrincipal.EmployeeId = userinfo.EmployeeID;

                if ( !string.IsNullOrWhiteSpace( userinfo.EmailAddress ) )
                    userPrincipal.EmailAddress = userinfo.EmailAddress;

                if ( !string.IsNullOrWhiteSpace( userinfo.Telephone ) )
                    userPrincipal.VoiceTelephoneNumber = userinfo.Telephone;

                if ( !string.IsNullOrWhiteSpace( userinfo.UserLogonName ) )
                    userPrincipal.SamAccountName = userinfo.UserLogonName;

                if ( !string.IsNullOrWhiteSpace( userinfo.Password ) )
                    userPrincipal.SetPassword( userinfo.Password );

                userPrincipal.Enabled = true;
                userPrincipal.ExpirePasswordNow();

                try
                {
                    userPrincipal.Save();

                    DirectoryEntry de = (DirectoryEntry)userPrincipal.GetUnderlyingObject();

                    FillUserExtraAttributes( ref de , userinfo );

                    de.CommitChanges();
                    status.Message = "Account has been created successfuly";
                    status.IsSuccessful = true;
                }
                catch ( Exception ex )
                {
                    status.Message = "Exception creating user object. " + ex;
                    status.IsSuccessful = false;
                    return status;
                }

//.........这里部分代码省略.........
开发者ID:sghaida,项目名称:ADWS,代码行数:101,代码来源:Adws.svc.cs


示例6: CreateOrGetUserPrincipal

        private UserPrincipal CreateOrGetUserPrincipal(UserInformation userInfo)
        {
            UserPrincipal user = null;
            if ( ! LocalAccount.UserExists(userInfo.Username) )
            {
                // See note about MS bug in CreateOrGetGroupPrincipal to understand the mix of DE/Principal here:
                using (user = new UserPrincipal(m_machinePrincipal))
                {
                    user.Name = userInfo.Username;
                    user.SetPassword(userInfo.Password);
                    user.Description = "pGina created";
                    userInfo.Description = user.Description;
                    if (userInfo.PasswordEXP)
                        user.ExpirePasswordNow();
                    user.Save();

                    // Sync via DE
                    SyncUserPrincipalInfo(userInfo);

                    // We have to re-fetch to get changes made via underlying DE
                    return GetUserPrincipal(user.Name);
                }
            }

            user = GetUserPrincipal(userInfo.Username);
            if (user == null)
                m_logger.ErrorFormat("Unable to get user principal for account that apparently exists: {0}", userInfo.Username);

            return user;
        }
开发者ID:MutonUfoAI,项目名称:pgina,代码行数:30,代码来源:LocalAccount.cs


示例7: createNewUserBackgroundWorker_DoWork

        private void createNewUserBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                using (var principalContext = new PrincipalContext(ContextType.Domain, domainDnsName, cnDnDictionary[createInComboBox.Text]))
                {
                    using (var checkForExistingUser = UserPrincipal.FindByIdentity(principalContext, sAMAccountNameTextBox.Text))
                    {
                        if (checkForExistingUser != null)
                        {
                            AddActivityItem(sAMAccountNameTextBox.Text + " already exists. Use a different sAMAccountName.");
                            activityListBox.BackColor = Color.Pink;
                            return;
                        }
                    }
                    using (var newUser = new UserPrincipal(principalContext))
                    {
                        newUser.SamAccountName = sAMAccountNameTextBox.Text;
                        if (givenNameTextBox.Text.Length > 0)
                            newUser.GivenName = givenNameTextBox.Text;

                        if (middleNameTextBox.Text.Length > 0)
                            newUser.MiddleName = middleNameTextBox.Text;

                        if (surnameTextBox.Text.Length > 0)
                            newUser.Surname = surnameTextBox.Text;

                        if (displayNameTextBox.Text.Length > 0)
                            newUser.DisplayName = displayNameTextBox.Text;

                        if (emailTextBox.Text.Length > 0)
                            newUser.EmailAddress = emailTextBox.Text;

                        if (employeeIDTextBox.Text.Length > 0)
                            newUser.EmployeeId = employeeIDTextBox.Text;

                        if (descriptionTextBox.Text.Length > 0)
                            newUser.Description = descriptionTextBox.Text;

                        if (userPrincipalNameTextBox.Text.Length > 0)
                            newUser.UserPrincipalName = userPrincipalNameTextBox.Text;

                        newUser.SetPassword(passwordTextBox.Text);

                        if (userEnabledCheckBox.Checked)
                            newUser.Enabled = true;

                        if (changePasswordCheckBox.Checked)
                            newUser.ExpirePasswordNow();

                        if (passwordNeverExpiresCheckBox.Checked)
                            newUser.PasswordNeverExpires = true;

                        newUser.Save();

                        if (!string.IsNullOrEmpty(copyFromComboBox.Text))
                        {
                            using (PrincipalSearchResult<Principal> newUsersExistingGroupMemberships = newUser.GetAuthorizationGroups())
                            using (var userToCopyFrom = UserPrincipal.FindByIdentity(principalContext, copyFromComboBox.Text))
                            {
                                using (PrincipalSearchResult<Principal> groups = userToCopyFrom.GetAuthorizationGroups())
                                {
                                    foreach (GroupPrincipal groupPrincipal in groups)
                                    {
                                        bool newUserIsAlreadyAMember = false;
                                        foreach (GroupPrincipal existingGroupMembership in newUsersExistingGroupMemberships)
                                        {
                                            if (existingGroupMembership.Name == groupPrincipal.Name)
                                                newUserIsAlreadyAMember = true;
                                        }
                                        if (newUserIsAlreadyAMember == false)
                                        {
                                            groupPrincipal.Members.Add(newUser);
                                            groupPrincipal.Save();
                                            AddActivityItem("User added to group: " + groupPrincipal.Name);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            AddActivityItem("No group memberships were copied.");
                        }

                        // To set additional attributes beyond those provided by UserPrincipal, we must drop down to the underlying object.
                        using (DirectoryEntry newUserEntry = (DirectoryEntry)newUser.GetUnderlyingObject())
                        {
                            if (initialsTextBox.Text.Length > 0)
                                newUserEntry.Properties["initials"].Value = initialsTextBox.Text;

                            if (employeeNumberTextBox.Text.Length > 0)
                                newUserEntry.Properties["employeeNumber"].Value = employeeNumberTextBox.Text;

                            if (telephoneNumberTextBox.Text.Length > 0)
                                newUserEntry.Properties["telephoneNumber"].Value = telephoneNumberTextBox.Text;

                            newUserEntry.CommitChanges();
                        }
                        AddActivityItem("User " + newUser.SamAccountName + " created successfully.");
//.........这里部分代码省略.........
开发者ID:ryanries,项目名称:CustomAddADUser,代码行数:101,代码来源:CustomAddADUserMainForm.cs



注:本文中的System.DirectoryServices.AccountManagement.UserPrincipal.ExpirePasswordNow方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。