Julien Pinquié

ASP.NET, C#, SQL Server, Windows Live

February 2008 - Posts

How to resolve "__doPostBack is not defined" error

It just happened to me... I put my script in an file and declare it in the header like this :

<script src="js/masterpage.js" type="text/javascript" />

When I launch the page, I get a javascript error __doPostBack is not defined and none of my javascript functions is executed.

It seems that some tags need a closing tag and don't understand "/>", it's the case of the script tag but not of link, used to call my css style sheets for example. Using this code works :

<script src="js/masterpage.js" type="text/javascript"></script>
How to get domain user information from Active Directory in C#

First, we have to know the user connection name (here from an ASP.NET page).

string principal = this.Context.User.Identity.Name;

 


Then we have to define some stuff :

  • search filter including object type and connection name,
  • domain,
  • properties to retrieve from Active Directory.
string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", principal);
string domain = "DOMAIN";
string[] properties = new string[] { "fullname" };

 


To search in Active Directory, we need the following objects :

  • DirectoryEntry : represents a node or object in the Active Directory hierarchy,
  • DirectorySearcher : performs queries against Active Directory Domain Services.
DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);
DirectorySearcher searcher = new DirectorySearcher(adRoot);
searcher.SearchScope = SearchScope.Subtree;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.PropertiesToLoad.AddRange(properties);
searcher.Filter = filter;
 
SearchResult result = searcher.FindOne();
DirectoryEntry directoryEntry = result.GetDirectoryEntry();

 


Finally, once information is retrieved in the DirectoryEntry object, we can get the details of each property defined.

string displayName = directoryEntry.Properties["displayName"][0].ToString();
string firstName = directoryEntry.Properties["givenName"][0].ToString();
string lastName = directoryEntry.Properties["sn"][0].ToString();
string email = directoryEntry.Properties["mail"][0].ToString();
Posted: Feb 06 2008, 12:07 PM by jpinquie | with 21 comment(s) |
Filed under: ,
More Posts