Beispiel #1
0
        private void RemoveUser(string username)
        {
            ADResult phonenumberChangeResult = _activeDirectory.SetUserFingerprint(
                userlist_lstview.FocusedItem.Text, "");

            switch (phonenumberChangeResult)
            {
            case ADResult.UserNotFound:
                MessageBox.Show("User Not Found");
                break;

            case ADResult.DisabledAccount:
                MessageBox.Show("Specified account is disabled");
                break;

            case ADResult.InternalError:
                MessageBox.Show("Unable to set phone number due to internal error");
                break;

            case ADResult.Success:
                MessageBox.Show("Successfully removed phone number for user account");
                break;
            }
            LoadUsers();
        }
Beispiel #2
0
        public async Task <IADResult <DomainInfo> > SignIn(DomainInfo domain, HttpContext httpContext)
        {
            var result = new ADResult <DomainInfo>();

            try
            {
                var claimIndentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                claimIndentity.AddClaim(new Claim(nameof(DomainInfo.AdminName), domain.AdminName));
                claimIndentity.AddClaim(new Claim(nameof(DomainInfo.DomainName), domain.DomainName));
                claimIndentity.AddClaim(new Claim(nameof(DomainInfo.DomainStaticKey), domain.DomainStaticKey));

                var principal = new ClaimsPrincipal(claimIndentity);

                //验证参数内容
                var authProperties = new AuthenticationProperties
                {
                    AllowRefresh = true,

                    //是否永久保存cookie
                    IsPersistent = false
                };

                await httpContext?.SignInAsync(principal, authProperties);

                AddLoginDomainInfo(domain);
                return(result.ToReturn(true, domain));
            }
            catch (Exception ex)
            {
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #3
0
        private void save_btn_Click(object sender, EventArgs e)
        {
            if (!IsPhone(phonenumber_txtbox.Text))
            {
                MessageBox.Show("Specified phone number is not valid");
                return;
            }
            ADResult phonenumberChangeResult = activeDirectory.SetUserFingerprint(username_txtbox.Text,
                                                                                  MD5Helper.GetHash(phonenumber_txtbox.Text));

            switch (phonenumberChangeResult)
            {
            case ADResult.UserNotFound:
                MessageBox.Show("User Not Found");
                break;

            case ADResult.DisabledAccount:
                MessageBox.Show("Specified account is disabled");
                break;

            case ADResult.InternalError:
                MessageBox.Show("Unable to set phone number due to internal error");
                break;

            case ADResult.Success:
                MessageBox.Show("Successfully set phone number");
                DialogResult = DialogResult.OK;
                Close();
                break;
            }
        }
Beispiel #4
0
        public IADResult <ADUser> UnlockAccount(DomainInfo info, string samAccountName)
        {
            var result = new ADResult <ADUser>();

            if (string.IsNullOrWhiteSpace(samAccountName))
            {
                return(result.ToReturn("User Name is NullOrWhiteSpace"));
            }

            try
            {
                using var context            = new PrincipalContext(ContextType.Domain, info.DomainName, info.ContainerString, info.AdminName, info.AdminPassword);
                using UserPrincipal userPrin = new UserPrincipal(context);
                userPrin.SamAccountName      = samAccountName;

                using var user = (new PrincipalSearcher(userPrin).FindOne()) as UserPrincipal;

                if (user != null)
                {
                    if (user.IsAccountLockedOut())
                    {
                        user.UnlockAccount();
                        user.Save();
                    }
                    return(result.ToReturn(new ADUser().NewCopy(user)));
                }
                return(result.ToReturn("Cannot find the username in " + context.Name));
            }
            catch (Exception ex)
            {
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #5
0
        public IADResult <List <ADBase> > FindUserGroups(DomainInfo info, string samAccountName)
        {
            var result = new ADResult <List <ADBase> >();

            if (string.IsNullOrWhiteSpace(samAccountName))
            {
                return(result.ToReturn("User Name is NullOrWhiteSpace"));
            }

            try
            {
                using var context            = new PrincipalContext(ContextType.Domain, info.DomainName, info.ContainerString, info.AdminName, info.AdminPassword);
                using UserPrincipal userPrin = new UserPrincipal(context);
                userPrin.SamAccountName      = samAccountName;

                using var user = (new PrincipalSearcher(userPrin).FindOne()) as UserPrincipal;

                if (user != null)
                {
                    using var gs = user.GetGroups(context);
                    var list = new List <ADBase>();
                    foreach (var g in gs)
                    {
                        list.Add(new ADGroup().NewADBase(g));
                    }
                    return(result.ToReturn(list));
                }
                return(result.ToReturn("Cannot find the username in " + context.Name));
            }
            catch (Exception ex)
            {
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #6
0
        public IADResult <List <ADUser> > FindAllUser(DomainInfo info)
        {
            var result = new ADResult <List <ADUser> >();

            try
            {
                using var context = new PrincipalContext(ContextType.Domain, info.DomainName, info.ContainerString, info.AdminName, info.AdminPassword);
                using UserPrincipal userPrincipal = new UserPrincipal(context);
                using var principals = new PrincipalSearcher(userPrincipal).FindAll();

                var newList = new List <ADUser>();

                if (principals != null && principals.Any())
                {
                    foreach (var p in principals)
                    {
                        if (p is UserPrincipal u)
                        {
                            newList.Add(new ADUser().NewCopy(u));
                        }
                    }
                    return(result.ToReturn(newList));
                }

                return(result.ToReturn("Cannot find any AD User in " + context.Name));
            }
            catch (Exception ex)
            {
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #7
0
        public IADResult <List <ADGroup> > FindALLGroup(DomainInfo info)
        {
            var result = new ADResult <List <ADGroup> >();

            try
            {
                using var context      = new PrincipalContext(ContextType.Domain, info.DomainName, info.ContainerString, info.AdminName, info.AdminPassword);
                using var groupPrin    = new GroupPrincipal(context);
                using var searchGroups = new PrincipalSearcher(groupPrin).FindAll();

                var list = new List <ADGroup>();

                var principals = searchGroups.ToList();
                if (principals != null && principals.Any())
                {
                    foreach (var p in principals)
                    {
                        if (p is GroupPrincipal g)
                        {
                            list.Add(new ADGroup().NewCopy(g));
                        }
                    }
                    return(result.ToReturn(list));
                }

                return(result.ToReturn("Cannot find any AD Groups in " + context.Name));
            }
            catch (Exception ex)
            {
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #8
0
        public IADResult <ADGroup> FindGroup(DomainInfo info, string samAccountName)
        {
            var result = new ADResult <ADGroup>();

            if (string.IsNullOrWhiteSpace(samAccountName))
            {
                return(result.ToReturn("group name Is NullOrWhiteSpace"));
            }

            try
            {
                using var context          = new PrincipalContext(ContextType.Domain, info.DomainName, info.ContainerString, info.AdminName, info.AdminPassword);
                using GroupPrincipal group = new GroupPrincipal(context, samAccountName);
                using var g = new PrincipalSearcher(group).FindOne() as GroupPrincipal;

                if (g != null)
                {
                    return(result.ToReturn(new ADGroup().NewCopy(g)));
                }

                return(result.ToReturn("Cannot find the groupname in " + context.Name));
            }
            catch (Exception ex)
            {
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #9
0
        public IADResult <ADComputer> UnlockComputer(DomainInfo info, string name)
        {
            var result = new ADResult <ADComputer>();

            if (string.IsNullOrWhiteSpace(name))
            {
                return(result.ToReturn("computer Name is NullOrWhiteSpace"));
            }

            try
            {
                using var context = new PrincipalContext(ContextType.Domain, info.DomainName, info.ContainerString, info.AdminName, info.AdminPassword);
                using ComputerPrincipal comPrin = new ComputerPrincipal(context);
                comPrin.Name = name;

                using var com = (new PrincipalSearcher(comPrin).FindOne()) as ComputerPrincipal;

                if (com != null)
                {
                    if (com.IsAccountLockedOut())
                    {
                        com.UnlockAccount();
                        com.Save();
                    }
                    return(result.ToReturn(new ADComputer().NewCopy(com)));
                }
                return(result.ToReturn("Cannot find the computer in " + context.Name));
            }
            catch (Exception ex)
            {
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #10
0
        public IADResult <string> AddUserToGroup(DomainInfo info, string userSamAccount, List <string> addGroups)
        {
            var result = new ADResult <string>();

            var queryUserGroup = FindUserGroups(info, userSamAccount);

            if (!queryUserGroup.Success)
            {
                return(result.ToReturn(queryUserGroup.Error));
            }

            var userGroups = queryUserGroup.Value;

            try
            {
                foreach (var add in addGroups)
                {
                    // 要添加的组 不存在 本来组里面
                    if (!userGroups.Any(ug => ug.SamAccountName == add))
                    {
                        using var context = new PrincipalContext(ContextType.Domain, info.DomainName, info.ContainerString, info.AdminName, info.AdminPassword);
                        using GroupPrincipal groupPrin = new GroupPrincipal(context);
                        groupPrin.SamAccountName       = add;
                        using var find = (new PrincipalSearcher(groupPrin).FindOne()) as GroupPrincipal;
                        find.Members.Add(context, IdentityType.SamAccountName, userSamAccount);
                        find.Save();
                    }
                }

                foreach (var ug in userGroups)
                {
                    if (!addGroups.Contains(ug.SamAccountName))
                    {
                        using var context = new PrincipalContext(ContextType.Domain, info.DomainName, info.ContainerString, info.AdminName, info.AdminPassword);
                        using GroupPrincipal groupPrin = new GroupPrincipal(context);
                        groupPrin.SamAccountName       = ug.SamAccountName;
                        using var find = (new PrincipalSearcher(groupPrin).FindOne()) as GroupPrincipal;
                        find.Members.Remove(context, IdentityType.SamAccountName, userSamAccount);
                        find.Save();
                    }
                }

                return(result.ToReturn(true, userSamAccount));
            }
            catch (Exception ex)
            {
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #11
0
        public IADResult <DomainInfo> GetLoginDomainInfo(HttpContext httpContext)
        {
            var result   = new ADResult <DomainInfo>();
            var identity = httpContext.User.Identity as ClaimsIdentity;
            var key      = identity.FindFirst(nameof(DomainInfo.DomainStaticKey)).Value;
            var get      = DomainInfoStaticDb.TryGetValue(key, out DomainInfo info);

            if (get)
            {
                return(result.ToReturn(get, info));
            }
            else
            {
                return(result.ToReturn(key + " try to get DomainInfo in DomainInfoStaticDb return false."));
            }
        }
Beispiel #12
0
        public IADResult <ADUser> CreateUser(DomainInfo info, ADUser newuser, string password)
        {
            var result = new ADResult <ADUser>();

            if (newuser == null || string.IsNullOrWhiteSpace(newuser.SamAccountName))
            {
                return(result.ToReturn("User is NullOrWhiteSpace"));
            }

            try
            {
                var exist = FindUser(info, newuser.SamAccountName);
                if (exist.Success && exist.Value.Name == newuser.Name)
                {
                    return(result.ToReturn("Logon Name or Full Name is exist"));
                }

                var container = "cn=users," + info.ContainerString;
                using var context            = new PrincipalContext(ContextType.Domain, info.DomainName, container, info.AdminName, info.AdminPassword);
                using UserPrincipal userPrin = new UserPrincipal(context);
                // set value
                userPrin.SamAccountName    = newuser.SamAccountName;
                userPrin.Surname           = newuser.Surname;
                userPrin.GivenName         = newuser.GivenName;
                userPrin.Name              = newuser.Name;
                userPrin.DisplayName       = newuser.DisplayName;
                userPrin.Description       = newuser.Description;
                userPrin.UserPrincipalName = newuser.SamAccountName + "@" + info.DomainName;

                userPrin.Save();
                userPrin.Enabled = true;
                userPrin.SetPassword(password);
                userPrin.Save();

                return(result.ToReturn(new ADUser().NewCopy(userPrin)));
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    return(result.ToReturn(ex.InnerException.Message));
                }
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #13
0
        // 用 HttpContext.User.Identity.Name 从  DomainInfoStaticDb 获取 DomainInfo
        public IADResult <DomainInfo> GetLoginDomainInfoUseHttpContext()
        {
            var result   = new ADResult <DomainInfo>();
            var identity = _httpContext?.User?.Identity;

            if (identity == null)
            {
                return(result.ToReturn("HttpContext.User.Identity is Null"));
            }

            var query = new ADLoginHelper().GetLoginDomainInfo(_httpContext);

            if (query.Success)
            {
                return(result.ToReturn(query.Value));
            }
            else
            {
                return(result.ToReturn(query.Error));
            }
        }
Beispiel #14
0
        public async Task <IADResult <DomainInfo> > SetDomainInfoToDatabase(IADServiceDb db, DomainInfo info)
        {
            var result = new ADResult <DomainInfo>();

            if (info == null || string.IsNullOrWhiteSpace(info.DomainName) ||
                string.IsNullOrWhiteSpace(info.ContainerString))
            {
                return(result.ToReturn("DomainInfo Is NullOrWhiteSpace"));
            }

            try
            {
                var all = await db.Tbl_DomainInfo.GetListAsync();

                if (all != null && all.Any())
                {
                    await db.Tbl_DomainInfo.AsDeleteable().Where(all).ExecuteCommandAsync();
                }

                var set = await db.Tbl_DomainInfo.InsertAsync(info);

                if (set)
                {
                    return(result.ToReturn(await db.Tbl_DomainInfo.AsQueryable().FirstAsync()));
                }

                return(result.ToReturn("Cannot Set DomainInfo."));
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    return(result.ToReturn(ex.InnerException.Message));
                }
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #15
0
        public async Task <IADResult <DomainInfo> > GetDomainInfo(IADServiceDb db)
        {
            var result = new ADResult <DomainInfo>();

            try
            {
                var first = await db.Tbl_DomainInfo.AsQueryable().FirstAsync();

                if (first != null)
                {
                    return(result.ToReturn(first));
                }

                return(result.ToReturn("Cannot Find DomainInfo in Database."));
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    return(result.ToReturn(ex.InnerException.Message));
                }
                return(result.ToReturn(ex.Message));
            }
        }
Beispiel #16
0
        public async Task <IADResult <DomainInfo> > Login(DomainInfo domain, string loginName, string password)
        {
            var result = new ADResult <DomainInfo>();

            //var query = await GetDomainInfoInDatabase();
            try
            {
                //if (!query.Success)
                //{
                //    return result.ToReturn(query.Error);
                //}
                //var domain = query.Value;

                domain.AdminName     = loginName;
                domain.AdminPassword = password;

                using var context      = new PrincipalContext(ContextType.Domain, domain.DomainName, domain.ContainerString, domain.AdminName, domain.AdminPassword);
                domain.ConnectedServer = context.ConnectedServer; // throw a exception if server cannot connect.

                if (string.IsNullOrEmpty(domain.ConnectedServer))
                {
                    return(result.ToReturn("Connect to Domain Server fail."));
                }

                var login = await new ADLoginHelper().SignIn(domain, _httpContext);
                if (login.Success)
                {
                    return(result.ToReturn(login.Value));
                }
                return(result.ToReturn(login.Error));
            }
            catch (Exception ex)
            {
                return(result.ToReturn(ex.Message));
            }
        }