Пример #1
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;

                        //Для прокси пользователя
                        if (ConfigurationManager.AppSettings["UserProxy"] == "True")
                        {
                            sid = ConfigurationManager.AppSettings["UserProxySid"];
                        }

                        user.Sid = sid;
                        var login =  wi.Name.Remove(0, wi.Name.IndexOf("\\", StringComparison.CurrentCulture) + 1);

                        //Для прокси пользователя
                        if (ConfigurationManager.AppSettings["UserProxy"] == "True")
                        {
                            login = ConfigurationManager.AppSettings["UserProxyLogin"];
                        }

                        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;
        }
Пример #2
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.Email = userPrincipal.EmailAddress;
                }
            }

            return result;
        }
Пример #3
0
 protected AdUser DisplayCurUser()
 {
     CurUser = GetCurUser();
     if (CurUser == new AdUser()) RedirectToAction("AccessDenied", "Error");
     ViewBag.CurUser = CurUser;
     return CurUser;
 }