pGina.Plugin.LocalMachine.LocalAccount.UserExists C# (CSharp) Method

UserExists() public static method

This is a faster technique for determining whether or not a user exists on the local machine. UserPrincipal.FindByIdentity tends to be quite slow in general, so if you only need to know whether or not the account exists, this method is much faster.
public static UserExists ( string strUserName ) : bool
strUserName string The user name
return bool
        public static bool UserExists(string strUserName)
        {
            try
            {
                using (DirectoryEntry userEntry = LocalAccount.GetUserDirectoryEntry(strUserName))
                {
                    return userEntry != null;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

Usage Example

コード例 #1
0
        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.Save();

                    // Sync via DE
                    SyncUserPrincipalInfo(user, userInfo);

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

            user = GetUserPrincipal(userInfo.Username);
            if (user != null)
            {
                return(user);
            }
            else
            {
                throw new Exception(
                          String.Format("Unable to get user principal for account that apparently exists: {0}", userInfo.Username));
            }
        }
All Usage Examples Of pGina.Plugin.LocalMachine.LocalAccount::UserExists