public static IEnumerable <Specialist> GetSpecialistListS(AdGroup grp) { var list = new List <Specialist>(); 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 fullName = userPrincipal.DisplayName; var displayName = StringHelper.ShortName(userPrincipal.DisplayName); var sid = userPrincipal.Sid.Value; list.Add(new Specialist() { SpecialistSid = sid, FullName = fullName, DisplayName = displayName }); } } } return(list); } }
public static Specialist GetUserBySid(string sid) { var result = new Specialist(); if (String.IsNullOrEmpty(sid)) { return(new Specialist()); } using (WindowsImpersonationContextFacade impersonationContext = new WindowsImpersonationContextFacade( nc)) { var context = new PrincipalContext(ContextType.Domain); var userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.Sid, sid); if (userPrincipal != null) { result.SpecialistSid = sid; result.FullName = userPrincipal.DisplayName; result.DisplayName = StringHelper.ShortName(result.FullName); result.Email = userPrincipal.EmailAddress; } } return(result); }
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); } }
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 = StringHelper.ShortName(userPrincipal.DisplayName); var sid = userPrincipal.Sid.Value; list.Add(sid, name); } } } return(list.OrderBy(x => x.Value)); } }
public static IEnumerable<Specialist> GetSpecialistListS(AdGroup grp) { var list = new List<Specialist>(); 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 fullName = userPrincipal.DisplayName; var displayName = StringHelper.ShortName(userPrincipal.DisplayName); var sid = userPrincipal.Sid.Value; list.Add(new Specialist() { SpecialistSid = sid, FullName = fullName, DisplayName = displayName }); } } } return list; } }
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 = StringHelper.ShortName(userPrincipal.DisplayName); var sid = userPrincipal.Sid.Value; list.Add(sid, name); } } } return list.OrderBy(x => x.Value); } }
public AdUser GetCurUser() { AdUser user = new AdUser(); try { using (WindowsImpersonationContextFacade impersonationContext = new WindowsImpersonationContextFacade( AdHelper.GetAdUserCredentials())) { 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); }
public AdUser GetCurUser() { AdUser user = new AdUser(); try { using (WindowsImpersonationContextFacade impersonationContext = new WindowsImpersonationContextFacade( AdHelper.GetAdUserCredentials())) { 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; }
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; } }
public static Specialist GetUserBySid(string sid) { var result = new Specialist(); if (String.IsNullOrEmpty(sid)) return new Specialist(); using (WindowsImpersonationContextFacade impersonationContext = new WindowsImpersonationContextFacade( nc)) { var context = new PrincipalContext(ContextType.Domain); var userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.Sid, sid); if (userPrincipal != null) { result.SpecialistSid = sid; result.FullName = userPrincipal.DisplayName; result.DisplayName = StringHelper.ShortName(result.FullName); result.Email = userPrincipal.EmailAddress; } } return result; }