Beispiel #1
0
        public ADUserDTO AdminPrincipal(string identity, bool isLocal = false)
        {
            var ctxType    = isLocal ? ContextType.Machine : ContextType.Domain;
            var hostDomain = isLocal ? "DESKTOP-LHO63TH" : "lan.naftan.by";
            var container  = isLocal ? null : "DC=lan,DC=naftan,DC=by";

            ADUserDTO user = new ADUserDTO()
            {
                Name = "Anonymous", Description = "Not defy"
            };

            try {
                using (var ctx = new PrincipalContext(ctxType, hostDomain, container, ContextOptions.Negotiate)) {
                    var userPrincipal = UserPrincipal.FindByIdentity(ctx, identity);

                    if (userPrincipal != null)
                    {
                        user = new ADUserDTO {
                            FullName          = userPrincipal.Name,
                            EmailAddress      = userPrincipal.EmailAddress,
                            IdEmp             = userPrincipal.EmployeeId != null ? 0 : Int32.Parse(userPrincipal.EmployeeId),
                            Description       = userPrincipal.Description,
                            IsEnable          = userPrincipal.Enabled ?? false,
                            Phone             = userPrincipal.VoiceTelephoneNumber,
                            Server            = userPrincipal.Context.ConnectedServer,
                            GivenName         = userPrincipal.GivenName,
                            MiddleName        = userPrincipal.MiddleName,
                            Surname           = userPrincipal.Surname,
                            DistinguishedName = userPrincipal.DistinguishedName,
                            HomeDirector      = userPrincipal.HomeDirectory,
                            HomeDrive         = userPrincipal.HomeDrive,
                            DisplayName       = userPrincipal.DisplayName,
                            Sam           = userPrincipal.SamAccountName,
                            Guid          = userPrincipal.Guid ?? new Guid(),
                            Sid           = userPrincipal.Sid,
                            PrincipalName = userPrincipal.UserPrincipalName,
                            Groups        = userPrincipal.GetGroups().Select(gr => new ADGroupDTO {
                                Name        = gr.Name,
                                Description = gr.Description,
                                Sam         = gr.SamAccountName,
                                Sid         = gr.Sid,
                                Guid        = gr.Guid ?? new Guid(),
                                //Users = GetMembers(gr.Name, 50).ToList()
                            }).ToList()
                        }
                    }
                    ;
                }
            } catch (Exception ex) {
                this.Log.DebugFormat(@"Исключение при попытке работы с AD: {0}", ex.Message);
            }
            return(user);
        }
Beispiel #2
0
        public void AdminPrincipal()
        {
            var isLocal    = false;
            var ctxType    = isLocal ? ContextType.Machine : ContextType.Domain;
            var container  = isLocal ? null : "DC=lan,DC=naftan,DC=by";
            var hostDomain = isLocal ? "DESKTOP-LHO63TH" : "lan.naftan.by";

            ADUserDTO user = new ADUserDTO()
            {
                Name = "Anonymous", Description = "Not defy"
            };

            using (var ctx = new PrincipalContext(ctxType, hostDomain, container, ContextOptions.Negotiate)) {
                var userPrincipal = UserPrincipal.FindByIdentity(ctx, @"lan/cpn");

                if (userPrincipal != null)
                {
                    user = new ADUserDTO {
                        FullName          = userPrincipal.Name,
                        EmailAddress      = userPrincipal.EmailAddress,
                        IdEmp             = userPrincipal.EmployeeId != null ? 0 : Int32.Parse(userPrincipal.EmployeeId),
                        Description       = userPrincipal.Description,
                        IsEnable          = userPrincipal.Enabled ?? false,
                        Phone             = userPrincipal.VoiceTelephoneNumber,
                        Server            = userPrincipal.Context.ConnectedServer,
                        GivenName         = userPrincipal.GivenName,
                        MiddleName        = userPrincipal.MiddleName,
                        Surname           = userPrincipal.Surname,
                        DistinguishedName = userPrincipal.DistinguishedName,
                        HomeDirector      = userPrincipal.HomeDirectory,
                        HomeDrive         = userPrincipal.HomeDrive,
                        DisplayName       = userPrincipal.DisplayName,
                        Sam           = userPrincipal.SamAccountName,
                        Guid          = userPrincipal.Guid ?? new Guid(),
                        Sid           = userPrincipal.Sid,
                        PrincipalName = userPrincipal.UserPrincipalName,
                        Groups        = userPrincipal.GetGroups().Select(gr => new ADGroupDTO {
                            Name        = gr.Name,
                            Description = gr.Description,
                            Sam         = gr.SamAccountName,
                            Sid         = gr.Sid,
                            Guid        = gr.Guid ?? new Guid(),
                            Users       = this.GetMembers(gr.Name).ToList()
                        }).ToList()
                    };
                }
            }

            Assert.IsTrue(user != null);
        }
        /// <summary>
        /// Implements <see cref="IStatisticsService.GetUsers(string)"/>.
        /// </summary>
        public IEnumerable <ADUserDTO> GetUsers(string domain)
        {
            List <ADUserDTO> users = new List <ADUserDTO>();

            using (var context = new PrincipalContext(ContextType.Domain, domain))
            {
                using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
                {
                    foreach (var result in searcher.FindAll())
                    {
                        if (!result.SamAccountName.Equals("Administrator") && !result.SamAccountName.Equals("Guest") &&
                            !result.SamAccountName.Equals("krbtgt") && !result.SamAccountName.Equals("ASPNET"))
                        {
                            ADUserDTO user = new ADUserDTO
                            {
                                SamAccountName = result.SamAccountName,
                                DN             = result.DistinguishedName,
                                Roles          = result.GetGroups().Select(x =>
                                {
                                    switch (x.Name)
                                    {
                                    case "SSTU_Student":
                                        return(x.Name);

                                    case "SSTU_Administrator":
                                        return(x.Name);

                                    case "SSTU_Deanery":
                                        return(x.Name);

                                    case "SSTU_Inspector":
                                        return(x.Name);

                                    default:
                                        return(null);
                                    }
                                }).Where(y => y != null).ToList()
                            };
                            users.Add(user);
                        }
                    }
                }
            }
            return(users);
        }
Beispiel #4
0
        public void CheckValidationOnRemoteADDomain()
        {
            var isLocal    = false;
            var ctxType    = isLocal ? ContextType.Machine : ContextType.Domain;
            var container  = isLocal ? null : "DC=lan,DC=naftan,DC=by";
            var hostDomain = isLocal ? "DESKTOP-LHO63TH" : "lan.naftan.by";

            ADUserDTO user = new ADUserDTO()
            {
                Name = "Anonymous", Description = "Not defy"
            };
            bool IsAuth = false;

            try {
                using (var ctx = new PrincipalContext(ContextType.Domain, hostDomain, null, ContextOptions.Negotiate)) {
                    IsAuth = ctx.ValidateCredentials(@"cpn", "1111");
                }
            } catch (Exception ex) {
                Debug.WriteLine(ex.Message);
            }

            Assert.AreEqual(true, IsAuth);
        }