Example #1
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 #2
0
        public AdUser GetCurUser()
        {
            AdUser user = new AdUser();

            try
            {
                using (WindowsImpersonationContextFacade impersonationContext
                           = new WindowsImpersonationContextFacade(
                                 nc))
                {
                    var wi = (WindowsIdentity)base.User.Identity;
                    if (wi.User != null)
                    {
                        var    domain = new PrincipalContext(ContextType.Domain);
                        string sid    = wi.User.Value;
                        user.Sid = sid;
                        var login = wi.Name.Remove(0, wi.Name.IndexOf("\\", StringComparison.CurrentCulture) + 1);
                        user.Login = login;
                        //var userPrincipal = UserPrincipal.FindByIdentity(domain, login);
                        //if (userPrincipal != null)
                        //{
                        //    var mail = userPrincipal.EmailAddress;
                        //    var name = userPrincipal.DisplayName;
                        //    user.Email = mail;
                        //    user.FullName = name;
                        //    user.AdGroups = new List<AdGroup>();
                        //    var wp = new WindowsPrincipal(wi);
                        //    foreach (var role in AdUserGroup.GetList())
                        //    {
                        //        var grpSid = new SecurityIdentifier(role.Sid);
                        //        if (wp.IsInRole(grpSid))
                        //        {
                        //            user.AdGroups.Add(role.Group);
                        //        }
                        //    }
                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return(user);
        }
Example #3
0
        public AdUser GetCurUser()
        {
            AdUser user = new AdUser();
            try
            {
                using (WindowsImpersonationContextFacade impersonationContext
                    = new WindowsImpersonationContextFacade(
                        nc))
                {
                    var wi = (WindowsIdentity)base.User.Identity;
                    if (wi.User != null)
                    {
                        var domain = new PrincipalContext(ContextType.Domain);
                        string sid = wi.User.Value;
                        user.Sid = sid;
                        var login = wi.Name.Remove(0, wi.Name.IndexOf("\\", StringComparison.CurrentCulture) + 1);
                        user.Login = login;
                        var userPrincipal = UserPrincipal.FindByIdentity(domain, login);
                        if (userPrincipal != null)
                        {
                            var mail = userPrincipal.EmailAddress;
                            var name = userPrincipal.DisplayName;
                            user.Email = mail;
                            user.FullName = name;
                            user.AdGroups = new List<AdGroup>();
                            var wp = new WindowsPrincipal(wi);
                            foreach (var role in AdUserGroup.GetList())
                            {
                                var grpSid = new SecurityIdentifier(role.Sid);
                                if (wp.IsInRole(grpSid))
                                {
                                    user.AdGroups.Add(role.Group);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return user;
        }
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;
        }
Example #5
0
        public AdUser GetCurUser()
        {
            AdUser user = new AdUser();

            try
            {
                //////List<GroupPrincipal> result = new List<GroupPrincipal>();

                //////// establish domain context
                //////PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);

                //////// find your user
                //////UserPrincipal usr = UserPrincipal.FindByIdentity(yourDomain, userName);

                //////// if found - grab its groups
                //////if (user != null)
                //////{
                //////    PrincipalSearchResult<Principal> groups = usr.GetAuthorizationGroups();

                //////    // iterate over all groups
                //////    foreach (Principal p in groups)
                //////    {
                //////        // make sure to add only group principals
                //////        if (p is GroupPrincipal)
                //////        {
                //////            result.Add((GroupPrincipal)p);
                //////        }
                //////    }
                //////}

                //////return user;

                string fakeSid    = null;
                string fakeLosgin = null;
                //fakeSid = "S-1-5-21-1970802976-3466419101-4042325969-3837";
                //fakeLosgin = "olga.skidan";

                using (WindowsImpersonationContextFacade impersonationContext
                           = new WindowsImpersonationContextFacade(
                                 nc))
                {
                    var wi = (WindowsIdentity)base.User.Identity;
                    if (wi.User != null)
                    {
                        var    domain = new PrincipalContext(ContextType.Domain);
                        string sid    = fakeSid ?? wi.User.Value;
                        user.Sid = sid;
                        var login = fakeLosgin ?? wi.Name.Remove(0, wi.Name.IndexOf("\\", StringComparison.CurrentCulture) + 1);
                        user.Login = login;
                        var userPrincipal = UserPrincipal.FindByIdentity(domain, login);
                        if (userPrincipal != null)
                        {
                            var mail = userPrincipal.EmailAddress;
                            var name = userPrincipal.DisplayName;
                            user.Email    = mail;
                            user.FullName = name;
                            //user.AdGroups = new List<AdGroup>();
                            //var wp = new WindowsPrincipal(wi);
                            //foreach (var role in AdUserGroup.GetList())
                            //{
                            //    var grpSid = new SecurityIdentifier(role.Sid);
                            //    if (wp.IsInRole(grpSid))
                            //    {
                            //        user.AdGroups.Add(role.Group);
                            //    }
                            //}
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return(user);
        }
Example #6
0
        public AdUser GetCurUser()
        {
            AdUser user = new AdUser();
            try
            {

                //////List<GroupPrincipal> result = new List<GroupPrincipal>();

                //////// establish domain context
                //////PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);

                //////// find your user
                //////UserPrincipal usr = UserPrincipal.FindByIdentity(yourDomain, userName);

                //////// if found - grab its groups
                //////if (user != null)
                //////{
                //////    PrincipalSearchResult<Principal> groups = usr.GetAuthorizationGroups();

                //////    // iterate over all groups
                //////    foreach (Principal p in groups)
                //////    {
                //////        // make sure to add only group principals
                //////        if (p is GroupPrincipal)
                //////        {
                //////            result.Add((GroupPrincipal)p);
                //////        }
                //////    }
                //////}

                //////return user;

                string fakeSid = null;
                string fakeLosgin = null;
                //fakeSid = "S-1-5-21-1970802976-3466419101-4042325969-3837";
                //fakeLosgin = "olga.skidan";

                using (WindowsImpersonationContextFacade impersonationContext
                    = new WindowsImpersonationContextFacade(
                        nc))
                {
                    var wi = (WindowsIdentity)base.User.Identity;
                    if (wi.User != null)
                    {
                        var domain = new PrincipalContext(ContextType.Domain);
                        string sid = fakeSid??wi.User.Value;
                        user.Sid = sid;
                        var login = fakeLosgin??wi.Name.Remove(0, wi.Name.IndexOf("\\", StringComparison.CurrentCulture) + 1);
                        user.Login = login;
                        var userPrincipal = UserPrincipal.FindByIdentity(domain, login);
                        if (userPrincipal != null)
                        {
                            var mail = userPrincipal.EmailAddress;
                            var name = userPrincipal.DisplayName;
                            user.Email = mail;
                            user.FullName = name;
                            //user.AdGroups = new List<AdGroup>();
                            //var wp = new WindowsPrincipal(wi);
                            //foreach (var role in AdUserGroup.GetList())
                            //{
                            //    var grpSid = new SecurityIdentifier(role.Sid);
                            //    if (wp.IsInRole(grpSid))
                            //    {
                            //        user.AdGroups.Add(role.Group);
                            //    }
                            //}
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return user;
        }
Example #7
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;
            }
        }