Ciao a tutti,

io dovrei recuperare alcuni attributi utente da Active Directory dopo che l'utente ha inserito le credenziali nella Login.... ma quanto arrivo alla riga di codice

SearchResultCollection results = deSearch.FindAll();

mi va in errore...unkonwn error tra l'altro..... vi posto le 2 classi che uso... nella prima praticamente controllo se l'utente è autenticato...nella seconda cerco di recuperare gli attributi su AD..ma con scarso successo...



codice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
using System.Security.Permissions;
using System.DirectoryServices.AccountManagement;
using System.Data;
using System.Data.SqlClient;

[DirectoryServicesPermission(SecurityAction.LinkDemand, Unrestricted = true)]

public partial class Account_Login : System.Web.UI.Page
{
    static LdapConnection ldapConnection;
    static string ldapServer;
    static NetworkCredential credential;
    static string targetOU;

    protected void Login(object sender, EventArgs e)
    {
        ldapServer = "mydomain.it";
        credential = new NetworkCredential("user", "pwd", "mydomain.it");
        targetOU = "mydomain/myou";


        using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "mydomain.it"))
        {
            // validate the credentials
            try
            {
                bool isValid = pc.ValidateCredentials(LoginUser.UserName, LoginUser.Password);


                //// Create the new LDAP connection
                ldapConnection = new LdapConnection(ldapServer);
                ldapConnection.Credential = credential;

                //Response.Write("LdapConnection is created successfully.");

                DirectoryEntry de = new DirectoryEntry();
                de.Path = "LDAP://mydomain/myou/CN=Users,DC=mydomain,DC=it";
                de.AuthenticationType = AuthenticationTypes.Secure;

                DirectorySearcher deSearch = new DirectorySearcher(de);

                ActiveDirUtenteADO U = new ActiveDirUtenteADO(de, deSearch);
                string nome = U.FindName(LoginUser.UserName);
                Response.Write("Nome utente: " + nome);

            }
            catch (Exception)
            {
                Response.Write("errore");

            }

        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {


    }
}



codice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;

/// <summary>
/// Summary description for ActiveDirUtente
/// </summary>
public class ActiveDirUtenteADO
{
    private DirectoryEntry de;
    private DirectorySearcher deSearch;
    
    public ActiveDirUtenteADO(DirectoryEntry de1, DirectorySearcher deSearch1)
	{
        de = de1;
        deSearch = deSearch1;
	}


    public static string GetProperty(SearchResult searchResult, string PropertyName)
  {
   if(searchResult.Properties.Contains(PropertyName))
   {
    return searchResult.Properties[PropertyName][0].ToString() ;
   }
   else
   {
    return string.Empty;
   }
  }


    public String FindName(String userAccount)
    {
     
        try
        {

          
            deSearch.SearchRoot = de;
            deSearch.Filter = "(&(objectClass=user)(|(cn=user)(sAMAccountName=" + userAccount + ")))";

           SearchResultCollection results = deSearch.FindAll(); <---- ERRORE!!

           string nome = "";
           foreach (SearchResult sResultSet in results)
           {
               nome = GetProperty(sResultSet, "givenName");
           }
          
         

            if (nome != null && !nome.Equals(""))
            {
                return nome;
            }
            else
            {
                return "Unknown User";
            }
        }
        catch (Exception ex)
        {
            string debug = ex.Message;

            return "";
        }
    }

}