Wednesday, November 30, 2005

PHP script Example

Below is basic PHP script which demonstrates MySQL and Active Directory communication :

";
mysql_close($dblink);


/* Going a step forward now we will connect to ldap server */
$ldapserver = "xxxx";

$user = "cn=Ashish Srivastava,cn=Users,dc=mydomamin,dc=com";
$passwd = "ashish";
$base = "cn=Users,dc=mydomamin,dc=com";


// Connect to ldap server
$dsCon = ldap_connect($ldapserver);


// Make sure we connected
if (!($dsCon))
{

print("Sorry, cannot contact LDAP server");
exit(2);

}


// Set Active Directory Specific Options
ldap_set_option($dsCon,LDAP_OPT_REFERRALS, 0);


// Bind LDAP
$bindresult = ldap_bind($dsCon,$user,$passwd);

if(!$bindresult){
$errno = ldap_errno($dsCon);
$error = ldap_error($dsCon);

print "LDAP Error ($errno): $error";
exit(3);
}


// Now Perform Search
$attributes = array("cn"); // attributes which you want

$search_filter = "(&(objectclass=user)(objectcategory=user)(sAMAccountName=ashish.srivastava))";
$search_result=ldap_search($dsCon, $base, $search_filter, $attributes);
if(!$search_result){
$errno = ldap_errno($dsCon);
$error = ldap_error($dsCon);

print "LDAP Error ($errno): $error";
exit(4);
}


// Getting Entries
$entry = ldap_get_entries($dsCon,$search_result);

if(!$entry){
$errno = ldap_errno($dsCon);
$error = ldap_error($dsCon);

print "LDAP Error ($errno): $error";
exit(4);
}


$cn = $entry[0]["cn"][0];
//var_dump($entry);
print "Common Name: $cn
";
ldap_close($dsCon);
?>

Tuesday, November 29, 2005

Codus - Code generation tool for .NET

Codus, a code generation tool by Adapdev Technologies provide excellent integration with variety of databases like SQL Server, Oracle and MySQL.

Cool Tools for .NET developers

Check out these cool tools. They are really handy.
Cool Tools

C# Interface Explained

I have never got so good explaination even in the good C# books. Interview in this article beautifully explains C# Interface.
http://dotnet.org.za/brianwilson/archive/2005/04/19/18306.aspx

Custom HTTP Modules

Custom HTTP Modules

HTTP Modules provides an opportunity to examine each incoming and outgoing request before request processing.
A typical use of HTTP module is URL rewriting on the basis of content of URL string. For example if i want to convert URL like "mywebproject/ashish/default.aspx" into "mywebproject/default.aspx?user=ashish" or vice-versa.

Creating custom HTTP Modules:

  • Make a class for implementing your custom module and implement IHttpModule Interface.
  • Add event handlers for BeginRequest and EndRequest events
  • Register your custom HTTP Module using web.config
Make a class and adding Event Handlers:
 public class UrlMapperModule : IHttpModule
{
private EventHandler onBeginRequest;

public UrlMapperModule()
{
onBeginRequest = new EventHandler(this.HandleBeginRequest);
}

void IHttpModule.Dispose()
{
}

void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += onBeginRequest;
}


private void HandleBeginRequest( object sender, EventArgs evargs )
{
// Now check every request
string requestUrl = app.Context.Request.Url.PathAndQuery;
}
}
Registering Custom HTTP Module in web.config: