public static List<AdGroup> GetUserAdGroups(User user) { List<AdGroup> result = new List<AdGroup>(); // establish domain context PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain); // find your user UserPrincipal up = UserPrincipal.FindByIdentity(yourDomain, user.Login); // if found - grab its groups if (up != null) { //PrincipalSearchResult<Principal> groups = up.GetAuthorizationGroups(); //// iterate over all groups //foreach (Principal p in groups) //{ // // make sure to add only group principals // if (p is GroupPrincipal) // { // result.Add(new AdGroup() { SID = p.Sid.Value, Name = p.DisplayName }); // } //} PrincipalSearchResult<Principal> groups = up.GetAuthorizationGroups(); var iterGroup = groups.GetEnumerator(); using (iterGroup) { while (iterGroup.MoveNext()) { try { Principal p = iterGroup.Current; //result.Add((GroupPrincipal)p); result.Add(new AdGroup() { SID = p.Sid.Value, Name = p.DisplayName }); } catch (PrincipalOperationException) { continue; } } } } return result; }
public static User GetUserBySid(string sid) { User user; SqlParameter pSid = new SqlParameter() { ParameterName = "user_sid", Value = sid, DbType = DbType.AnsiString }; DataTable dt = ExecuteQueryStoredProcedure(sp, "getUserBySid", pSid); if (dt.Rows.Count > 0) { DataRow dr = dt.Rows[0]; int id = (int)dr["id_user"]; string login = dr["login"].ToString(); string userSid = dr["sid"].ToString(); string fullName = dr["full_name"].ToString(); string displayName = dr["display_name"].ToString(); string mail = dr["mail"].ToString(); bool enabled = (bool)dr["enabled"]; string company = dr["company"].ToString(); if (String.IsNullOrEmpty(displayName)) displayName = fullName; user = new User(id, login, fullName, displayName, mail) { Enabled = enabled, AdSid = userSid, Company = company }; SqlParameter pUserId = new SqlParameter() { ParameterName = "id_user", Value = user.Id, DbType = DbType.Int32 }; dt = ExecuteQueryStoredProcedure(sp, "getEtUserByUserId", pUserId); EtalonUser etUser; if (dt.Rows.Count > 0) { dr = dt.Rows[0]; int etId = (int)dr["id_et_user"]; string etLogin = dr["et_login"].ToString(); string etPassword = dr["et_password"].ToString(); string etDisplayName = dr["et_display_name"].ToString(); string adSid = dr["ad_sid"].ToString(); etUser = new EtalonUser(etId, etLogin, etPassword, etDisplayName) { AdSid = adSid }; } else { string etDisplayName = "Не зарегистрирован"; etUser = new EtalonUser() { DisplayName = etDisplayName }; } user.EtUser = etUser; } else { user = new User(); } return user; }
protected void Page_PreLoad(object sender, EventArgs e) { if (!IsPostBack) { if (IsProxyUser) { string proxySid = Request.QueryString[qspPrxusr]; User = new User(proxySid); } else { User = new User(true); } } }