Example #1
0
        public static bool UserInGroup(string sid, params AdGroup[] groups)
        {
            using (WindowsImpersonationContextFacade impersonationContext
                       = new WindowsImpersonationContextFacade(
                             nc))
            {
                var context       = new PrincipalContext(ContextType.Domain);
                var userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.Sid, sid);

                if (userPrincipal == null)
                {
                    return(false);
                }
                ////if (userPrincipal.IsMemberOf(context, IdentityType.Sid, AdUserGroup.GetSidByAdGroup(AdGroup.SuperAdmin))) { return true; }//Если юзер Суперадмин

                foreach (var grp in groups)
                {
                    if (userPrincipal.IsMemberOf(context, IdentityType.Sid, AdUserGroup.GetSidByAdGroup(grp)))
                    {
                        return(true);
                    }
                }


                return(false);
            }
        }
Example #2
0
        public static IEnumerable <KeyValuePair <string, string> > GetSpecialistList(AdGroup grp)
        {
            var list = new Dictionary <string, string>();

            using (WindowsImpersonationContextFacade impersonationContext
                       = new WindowsImpersonationContextFacade(
                             nc))
            {
                var domain = new PrincipalContext(ContextType.Domain);
                var group  = GroupPrincipal.FindByIdentity(domain, IdentityType.Sid, AdUserGroup.GetSidByAdGroup(grp));
                if (group != null)
                {
                    var members = group.GetMembers(true);
                    foreach (var principal in members)
                    {
                        var userPrincipal = UserPrincipal.FindByIdentity(domain, principal.SamAccountName);
                        if (userPrincipal != null)
                        {
                            var name = MainHelper.ShortName(userPrincipal.DisplayName);
                            var sid  = userPrincipal.Sid.Value;
                            list.Add(sid, name);
                        }
                    }
                }

                return(list.OrderBy(x => x.Value));
            }
        }
Example #3
0
        public static void AddUserToGroup(string userSid, string groupName)
        {
            NetworkCredential nc = GetNetCredential4Ad();

            using (WindowsImpersonationContextFacade impersonationContext = new WindowsImpersonationContextFacade(nc))
            {
                using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
                {
                    try
                    {
                        GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);

                        group.Members.Add(pc, IdentityType.Sid, userSid);

                        //bool flag = false;

                        //try
                        //{
                        //    group.Members.Add(pc, IdentityType.UserPrincipalName, String.Format("{0}@UN1T.GROUP", userSid));
                        //}
                        //catch (NoMatchingPrincipalException ex)
                        //{
                        //    flag = true;
                        //}
                        //try
                        //{
                        //    group.Members.Add(pc, IdentityType.UserPrincipalName, String.Format("{0}@unitgroup.ru", userSid));
                        //    flag = false;
                        //}
                        //catch (NoMatchingPrincipalException ex)
                        //{
                        //    if (flag) throw ex;
                        //}

                        group.Save();
                    }
                    catch (PrincipalExistsException ex)
                    {

                    }

                }
            }
        }
Example #4
0
        public static AdUser GetUserBySid(string sid)
        {
            var result = new AdUser();

            using (WindowsImpersonationContextFacade impersonationContext
                       = new WindowsImpersonationContextFacade(
                             nc))
            {
                var context       = new PrincipalContext(ContextType.Domain);
                var userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.Sid, sid);

                if (userPrincipal != null)
                {
                    result.Sid      = sid;
                    result.FullName = userPrincipal.DisplayName;
                    //result.DisplayName = MainHelper.ShortName(result.FullName);
                    result.Email = userPrincipal.EmailAddress;
                }
            }

            return(result);
        }
        //получение менеджеров из ActiveDirectory
        public static List<Manager> GetManagers()
        {
            //var man = new List<Manager>
            //{
            //    new Manager() {Id = "asd", Name = "Олег Иванов", Roles = new List<Role>() { Role.Enter, Role.Manager}, SubDivision = "Barcelona", Chief = "Александров А.А."},
            //    new Manager() {Id = "rtre", Name = "Андрей Петров", Roles = new List<Role>() { Role.Enter, Role.Manager, Role.Operator}, SubDivision = "Borussia", Chief = "Широков Р.В."},
            //    new Manager() {Id = "fgdsf", Name = "Дмитрий Степанов", Roles = new List<Role>() { Role.Enter, Role.Manager, Role.TenderStatus}, SubDivision = "Zenit", Chief = "Файзулин В.Г."}
            //};
            //man.ForEach((x) =>
            //{
            //    x.ShortName = x.Name;
            //    x.ChiefShortName = x.Chief;
            //});
            //return man;
            using (WindowsImpersonationContextFacade impersonationContext
                = new WindowsImpersonationContextFacade(
                    nc))
            {
                var list = new List<Manager>();
                var domain = new PrincipalContext(ContextType.Domain);
                var group = GroupPrincipal.FindByIdentity(domain, IdentityType.Name,
                    _roles.First(x => x.Role == Role.Manager).Name);
                if (group != null)
                {
                    var members = group.GetMembers(true);
                    foreach (var principal in members)
                    {
                        var userPrincipal = UserPrincipal.FindByIdentity(domain, principal.Name);
                        if (userPrincipal != null)
                        {
                            var email = userPrincipal.EmailAddress;
                            var name = userPrincipal.DisplayName;
                            var sid = userPrincipal.Sid.Value;
                            var shortName = GetShortName(name);
                            var departament = GetProperty(userPrincipal, "department");
                            var manager = GetProperty(userPrincipal, "manager");
                            var managerShortName = string.Empty;
                            if (!string.IsNullOrEmpty(manager))
                            {
                                var managerUser = UserPrincipal.FindByIdentity(domain, manager);
                                if (managerUser != null)
                                {
                                    manager = managerUser.DisplayName;
                                    managerShortName = GetShortName(manager);
                                }
                            }
                            var user = new Manager()
                            {
                                Id = sid,
                                Name = name,
                                ShortName = shortName,
                                Email = email,
                                SubDivision = departament,
                                Chief = manager,
                                ChiefShortName = managerShortName,
                                Roles = new List<Role>() {Role.Manager}
                            };
                            list.Add(user);
                        }
                    }
                }
                list = list.OrderBy(m => m.ShortName).ToList();

                return list;
            }
        }
 //public static IEnumerable<KeyValuePair<string, string>> GetOperators()
 //{
 //    return GetUserSelectionList(AdGroup.SpeCalcOperator);
 //    //using (WindowsImpersonationContextFacade impersonationContext
 //    //    = new WindowsImpersonationContextFacade(
 //    //        nc))
 //    //{
 //    //    var list = new List<Operator>();
 //    //    var domain = new PrincipalContext(ContextType.Domain);
 //    //    var group = GroupPrincipal.FindByIdentity(domain, IdentityType.Name,
 //    //        _roles.First(x => x.Role == Role.Operator).Name);
 //    //    if (group != null)
 //    //    {
 //    //        var members = group.GetMembers(true);
 //    //        foreach (var principal in members)
 //    //        {
 //    //            var userPrincipal = UserPrincipal.FindByIdentity(domain, principal.Name);
 //    //            if (userPrincipal != null)
 //    //            {
 //    //                var email = userPrincipal.EmailAddress;
 //    //                var name = userPrincipal.DisplayName;
 //    //                var sid = userPrincipal.Sid.Value;
 //    //                var shortName = GetShortName(name);
 //    //                var departament = GetProperty(userPrincipal, "department");
 //    //                var manager = GetProperty(userPrincipal, "manager");
 //    //                var managerShortName = string.Empty;
 //    //                if (!string.IsNullOrEmpty(manager))
 //    //                {
 //    //                    var managerUser = UserPrincipal.FindByIdentity(domain, manager);
 //    //                    if (managerUser != null)
 //    //                    {
 //    //                        manager = managerUser.DisplayName;
 //    //                        managerShortName = GetShortName(manager);
 //    //                    }
 //    //                }
 //    //                var user = new Operator()
 //    //                {
 //    //                    Id = sid,
 //    //                    Name = name,
 //    //                    ShortName = shortName,
 //    //                    Email = email,
 //    //                    SubDivision = departament,
 //    //                    Chief = manager,
 //    //                    ChiefShortName = managerShortName,
 //    //                    Roles = new List<Role>() { Role.Manager }
 //    //                };
 //    //                list.Add(user);
 //    //            }
 //    //        }
 //    //    }
 //    //    list = list.OrderBy(m => m.ShortName).ToList();
 //    //    return list;
 //    //}
 //}
 //public static List<ControllerUser> GetControllerUsers()
 //{
 //    return  new List<ControllerUser>()
 //    {
 //        new ControllerUser() { Id = "bngbtjradbdfgbffg", Name = "Тихонов Андрей", Roles = new List<Role>() { Role.Enter, Role.Controller}},
 //        new ControllerUser() { Id = "uyjtjuktsdfvwvfv", Name = "Аршавин Денис", Roles = new List<Role>() { Role.Enter, Role.Controller, Role.TenderStatus}},
 //    };
 //}
 //public static List<TenderStatusUser> GetTenderStatusUsers()
 //{
 //    return new List<TenderStatusUser>()
 //    {
 //        new TenderStatusUser() { Id = "rtyutyujyujyuj", Name = "C. Ronaldo", Roles = new List<Role>() { Role.Enter, Role.TenderStatus, Role.ProductManager}},
 //        new TenderStatusUser() { Id = "iumsdfvsdfsdr", Name = "L. Modrich", Roles = new List<Role>() { Role.Enter, Role.TenderStatus}},
 //    };
 //}
 //public static UserBase GetUserByName(string name)
 //{
 //    UserBase user = null;
 //    var managers = GetManagers();
 //    user = managers.FirstOrDefault(x => x.Name == name);
 //    if (user == null)
 //    {
 //        var products = GetProductManagers();
 //        user = products.FirstOrDefault(x => x.Name == name);
 //    }
 //    if (user == null)
 //    {
 //        var operators = GetOperators();
 //        user = operators.FirstOrDefault(x => x.Name == name);
 //    }
 //    if (user == null)
 //    {
 //        var controllerUsers = GetControllerUsers();
 //        user = controllerUsers.FirstOrDefault(x => x.Name == name);
 //    }
 //    if (user == null)
 //    {
 //        var tenderStatusUsers = GetTenderStatusUsers();
 //        user = tenderStatusUsers.FirstOrDefault(x => x.Name == name);
 //    }
 //    return user;
 //}
 //получение юзера по id(sid)
 public static UserBase GetUserById(string id)
 {
     //UserBase user = null;
     //var managers = GetManagers();
     //user = managers.FirstOrDefault(x => x.Id == id);
     //if (user == null)
     //{
     //    var products = GetProductManagers();
     //    user = products.FirstOrDefault(x => x.Id == id);
     //}
     //if (user == null)
     //{
     //    var operators = GetOperators();
     //    user = operators.FirstOrDefault(x => x.Id == id);
     //}
     //if (user == null)
     //{
     //    var controllerUsers = GetControllerUsers();
     //    user = controllerUsers.FirstOrDefault(x => x.Id == id);
     //}
     //if (user == null)
     //{
     //    var tenderStatusUsers = GetTenderStatusUsers();
     //    user = tenderStatusUsers.FirstOrDefault(x => x.Id == id);
     //}
     //return user;
     using (WindowsImpersonationContextFacade impersonationContext
         = new WindowsImpersonationContextFacade(
             nc))
     {
         UserBase user = null;
         var domain = new PrincipalContext(ContextType.Domain);
         var userPrincipal = UserPrincipal.FindByIdentity(domain, IdentityType.Sid, id);
         if (userPrincipal != null)
         {
             var email = userPrincipal.EmailAddress;
             var name = userPrincipal.DisplayName;
             var sid = userPrincipal.Sid.Value;
             var shortName = GetShortName(name);
             var manager = GetProperty(userPrincipal, "manager");
             user = new UserBase()
             {
                 Id = sid,
                 Name = name,
                 ShortName = shortName,
                 Email = email,
                 ManagerName = manager,
                 Roles = new List<Role>() { Role.Enter }
             };
         }
         return user;
     }
 }
        //получение юзера из идентичности потока
        public static UserBase GetUser(IIdentity identity)
        {
            using (WindowsImpersonationContextFacade impersonationContext
                = new WindowsImpersonationContextFacade(
                    nc))
            {
                //UserBase user = null;
                //var userName = identity.Name;
                //user = GetUserByName(userName);
                //return user;
                UserBase user = null;
                var wi = (WindowsIdentity) identity;
                if (wi.User != null)
                {
                    user = new UserBase();
                    var domain = new PrincipalContext(ContextType.Domain);
                    var id = wi.User.Value;
                    //id = "S-1-5-21-1970802976-3466419101-4042325969-1750";
                    user.Id = id;
                    var login = wi.Name.Remove(0, wi.Name.IndexOf("\\", StringComparison.CurrentCulture) + 1);
                    //login = "******";
                    var userPrincipal = UserPrincipal.FindByIdentity(domain, login);
                    if (userPrincipal != null)
                    {
                        var mail = userPrincipal.EmailAddress;
                        var name = userPrincipal.DisplayName;
                        user.Email = mail;
                        user.Name = name;
                        user.ShortName = GetShortName(user.Name);
                        user.Roles = new List<Role>();
                        var wp = new WindowsPrincipal(wi);
                        //user.Roles.Add(Role.Operator);
                        //user.Roles.Add(Role.Enter);
                        foreach (var role in _roles)
                        {
                            var grpSid = new SecurityIdentifier(role.Sid);
                            if (wp.IsInRole(grpSid))
                            {
                                user.Roles.Add(role.Role);
                            }
                        }
                    }
                }

            return user;
            }
        }
        //получение снабженцев из ActiveDirectory
        public static List<ProductManager> GetProductManagers()
        {
            //var man = new List<ProductManager>()
            //{
            //   new ProductManager(){Id = "dfsadfs", Name = "Гена", Roles = new List<Role>() { Role.Enter, Role.ProductManager}},
            //   new ProductManager(){Id = "fdbfgbv", Name = "Вася", Roles = new List<Role>() { Role.Enter, Role.ProductManager}},
            //   new ProductManager(){Id = "dfsdfvfdhadfs", Name = "Петр", Roles = new List<Role>() { Role.Enter, Role.ProductManager, Role.Manager}},
            //   new ProductManager(){Id = "dfsdwqedqwefefadfs", Name = "Олег", Roles = new List<Role>() { Role.Enter, Role.ProductManager, Role.Controller}},
            //   new ProductManager(){Id = "df45gfdgsadfs", Name = "Дима", Roles = new List<Role>() { Role.Enter, Role.ProductManager, Role.Operator}},
            //   new ProductManager(){Id = "dfsvdfgdfgdfbadfs", Name = "Alex", Roles = new List<Role>() { Role.Enter, Role.ProductManager}},
            //   new ProductManager(){Id = "khnhbfgbdf", Name = "Stan", Roles = new List<Role>() { Role.Enter, Role.ProductManager}}
            //};
            //man.ForEach((x) => x.ShortName = x.Name);
            //return man;
            using (WindowsImpersonationContextFacade impersonationContext
                = new WindowsImpersonationContextFacade(
                    nc))
            {
                var list = new List<ProductManager>();
                var domain = new PrincipalContext(ContextType.Domain);
                var group = GroupPrincipal.FindByIdentity(domain, IdentityType.Name,
                    _roles.First(x => x.Role == Role.ProductManager).Name);
                if (group != null)
                {
                    var members = group.GetMembers(true);
                    foreach (var principal in members)
                    {
                        var userPrincipal = UserPrincipal.FindByIdentity(domain, principal.Name);
                        if (userPrincipal != null)
                        {
                            var email = userPrincipal.EmailAddress;
                            var name = userPrincipal.DisplayName;
                            var sid = userPrincipal.Sid.Value;
                            var shortName = GetShortName(name);
                            var user = new ProductManager()
                            {
                                Id = sid,
                                Name = name,
                                Email = email,
                                ShortName = shortName,
                                Roles = new List<Role>() {Role.ProductManager}
                            };
                            list.Add(user);
                        }
                    }
                }

                list = list.OrderBy(m => m.ShortName).ToList();
                return list;
            }
        }
Example #9
0
        public static bool UserInGroup(string sid, params string[] groups)
        {
            using (WindowsImpersonationContextFacade impersonationContext
                = new WindowsImpersonationContextFacade(
                    nc))
            {
                var context = new PrincipalContext(ContextType.Domain);
                var userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.Sid, sid);

                if (userPrincipal == null) return false;
                ////if (userPrincipal.IsMemberOf(context, IdentityType.Sid, AdUserGroup.GetSidByAdGroup(AdGroup.SuperAdmin))) { return true; }//Если юзер Суперадмин

                foreach (var grp in groups)
                {
                    if (userPrincipal.IsMemberOf(context, IdentityType.Sid, grp))
                    {
                        return true;
                    }
                }

                return false;
            }
        }
Example #10
0
        //public static bool CheckUserGroup(int userId, string groupName)
        //{
        //    NetworkCredential nc = GetNetCredential4Ad();
        //    bool result = false;
        //    using (WindowsImpersonationContextFacade impersonationContext = new WindowsImpersonationContextFacade(nc))
        //    {
        //        using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
        //        {
        //            GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
        //            //bool flagUnitG = false;
        //            //    bool flagUnit = false;
        //            bool flag = false;
        //            try
        //            {
        //                try
        //                {
        //                    UserPrincipal user = UserPrincipal.FindByIdentity(pc,
        //                        String.Format("{0}@UN1T.GROUP", userId));
        //                    result = user.IsMemberOf(group);
        //                    flag = true;
        //                }
        //                catch (NoMatchingPrincipalException ex)
        //                {
        //                    flag = false;
        //                }
        //                if (!flag)
        //                {
        //                    try
        //                    {
        //                        UserPrincipal user = UserPrincipal.FindByIdentity(pc,
        //                            String.Format("{0}@unitgroup.ru", userId));
        //                        result = user.IsMemberOf(group);
        //                        flag = true;
        //                    }
        //                    catch (NoMatchingPrincipalException ex)
        //                    {
        //                        if (!flag) throw new Exception();
        //                    }
        //                }
        //            }
        //            catch (Exception ex)
        //            {
        //                result = false;
        //            }
        //            //if (flagUnitG || flagUnit)
        //            return result;
        //            //try
        //            //{
        //            //    return user.IsMemberOf(group);
        //            //}
        //            //catch (Exception exception)
        //            //{
        //            //    return false;
        //            //}
        //        }
        //    }
        //}
        public static string[] GetGroupMembers(string groupName)
        {
            NetworkCredential nc = GetNetCredential4Ad();
            List<string> lstMembers = new List<string>();

            using (WindowsImpersonationContextFacade impersonationContext = new WindowsImpersonationContextFacade(nc))
            {
                using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
                {
                        GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
                        var memb = group.GetMembers(true);

                        foreach (var m in memb)
                    {
                        lstMembers.Add(m.DisplayName);
                    }
                }
            }

            return lstMembers.ToArray();
        }