public ExternalUser GetUser(string UserName) { var root = GetDirectoryEntry(); DirectorySearcher search = new DirectorySearcher(root); search.Filter = string.Format(CultureInfo.InvariantCulture, FilterSearchUser, AttributeMapUsername, UserName); SearchResult result = search.FindOne(); if (result != null && !string.IsNullOrEmpty(result.Path)) { DirectoryEntry user = result.GetDirectoryEntry(); //PrincipalContext principalContext = new PrincipalContext( // ContextType.Domain, ConnectionString); ExternalUser externalUser = new ExternalUser(); externalUser.UserName = UserName; PropertyValueCollection apellidoProperty = user.Properties["sn"]; PropertyValueCollection nombresProperty = user.Properties["givenName"]; PropertyValueCollection correoProperty = user.Properties["mail"]; if (apellidoProperty.Value != null) { externalUser.Surname = apellidoProperty.Value.ToString(); } if (nombresProperty.Value != null) { externalUser.GivenName = nombresProperty.Value.ToString(); } if (correoProperty.Value != null) { externalUser.EmailAddress = correoProperty.Value.ToString(); } return(externalUser); } else { return(null); } }
public ExternalUser GetUser(string UserName) { PrincipalContext oPrincipalContext = GetPrincipalContext(); UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, UserName); ExternalUser externalUser = new ExternalUser(); if (oUserPrincipal != null) { externalUser.UserName = oUserPrincipal.SamAccountName; externalUser.Surname = oUserPrincipal.Surname; externalUser.GivenName = oUserPrincipal.GivenName; externalUser.EmailAddress = oUserPrincipal.EmailAddress; } return(externalUser); }