C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Active Directory Application Mode With C#


Posted Date: 19 Oct 2004    Resource Type: Articles    Category: .NET Framework
Author: RajMember Level: Silver    
Rating: 1 out of 5Points: 5



Introduction

Active Directory Application Mode? (ADAM) is an independent mode of Active Directory, minus infrastructure features, that provides directory services for applications. ADAM provides dedicated directory services for applications. It provides a data store and services for accessing the data store. It uses standard application programming interfaces (APIs) for accessing the application data. The APIs include those of Active Directory, Active Directory Service Interfaces, Lightweight Data Access Protocol, and System.DirectoryServices. ADAM operates independently of Active Directory and independently of Active Directory domains or forests. It operates either as a standalone data store, or it operates with replication. Its independence enables local control and autonomy of directory services for specific applications. It also facilitates independent, flexible schemas, and naming contexts.
ADAM does not include directory services for the Windows operating system, so it concentrates on the requirements of specific applications. If ADAM operates in an Active Directory environment, it can use Active Directory for authentication. Because ADAM does not support the Messaging Application Programming Interface, Microsoft Exchange cannot use ADAM. Although ADAM and Active Directory can operate concurrently within the same network, ADAM serves the requirements of specific applications. An instance of ADAM can be created for a specific application without concern for the dependencies required by Active Directory. Multiple instances of ADAM, each supporting a separate application, can run on a single ADAM installation.

Sample

using System;
using System.DirectoryServices;

namespace UsrMgr
{
///
/// Summary description for AuthenticateUser.
///

public class AuthenticateUser
{
public string FirstName = "";
public string LastName = "";
public string Title= "";
public string Telephone= "";
public string Office = "";
public string ErrorMessage= "";
private DirectoryEntry oOrg = null;
private DirectoryEntry oEntry=null;

public AuthenticateUser()
{
oOrg = new DirectoryEntry("LDAP://localhost:389/o=Microsoft,c=US");
}

public bool AddUser()
{
string strName = "" ;
try
{
DirectoryEntry oUsr;
strName = FirstName + " "+ LastName;
oUsr = oOrg.Children.Add("CN="+ strName, "user");
GetValues(oUsr);
oUsr.CommitChanges();
}
catch(Exception oException)
{
ErrorMessage = oException.Message;
return false;
}
return true;
}

public bool DeleteUser()
{
try
{
FindUser();
string strName = oEntry.Properties["givenName"].Value + " " + oEntry.Properties["sn"].Value;
DirectoryEntry oParent = oEntry.Parent;
oParent.Children.Remove(oEntry);
ErrorMessage = strName + " was Removed";
}
catch(Exception oException)
{
ErrorMessage = oException.Message;
return false;
}
return true;
}

public bool UpdateUser()
{
try
{
FindUser();
GetValues(oEntry);
oEntry.CommitChanges();
}
catch(Exception oException)
{
ErrorMessage = oException.Message;
return false;
}
return true;
}

public bool FindUser()
{
try
{
DirectorySearcher oSrc = new DirectorySearcher(oOrg, SetQuery(), new string[] {"ADsPath"} );
SearchResult oResult = oSrc.FindOne();
if ( oResult == null )
{
throw new Exception("Object not found");
}

DirectoryEntry ent = oResult.GetDirectoryEntry();
SetProperties(ent);

}
catch(Exception oException)
{
ErrorMessage = oException.Message;
return false;
}
return true;
}

private string SetQuery()
{
String s="";
String sFilter="(&(objectCategory=Person)(objectClass=User)";


s += SetQuery("givenName", FirstName);
s += SetQuery("sn", LastName );
s += SetQuery("telephoneNumber", Telephone);
s += SetQuery("title", Title);
s += SetQuery("physicalDeliveryOfficeName", Office );

if ( s.Length > 0 )
{
sFilter += s + ")";
}
else
{
sFilter += ")";
}

return sFilter;
}


private void SetProperties(DirectoryEntry currentEntry)
{
if ( currentEntry == null )
{
return;
}

FirstName = currentEntry.Properties["givenName"].Value.ToString();
LastName = currentEntry.Properties["sn"].Value.ToString();
Telephone= currentEntry.Properties["telephoneNumber"].Value.ToString();
Title = currentEntry.Properties["title"].Value.ToString();
Office =currentEntry.Properties["physicalDeliveryOfficeName"].Value.ToString();

oEntry = currentEntry;

}

private string SetQuery(String attribute, string strText )
{
if (strText.Length > 0)
{
return "(" + attribute + "=" + strText +")";
}
return "";

}

private void GetValues( DirectoryEntry ent)
{
ent.Properties["title"].Value = Title;
ent.Properties["telephoneNumber"].Value = Telephone;
ent.Properties["physicalDeliveryOfficeName"].Value = Office;
ent.Properties["sn"].Value = LastName;
ent.Properties["givenName"].Value = FirstName;

}

}
}

Invoke the Method of above class and Add, Update and Delete User

AuthenticateUser obj = new AuthenticateUser();

obj.FirstName = "Raj";
obj.LastName = "Taslim";
obj.Title= "Hello";
obj.Telephone= "234324324";
obj.Office = "534543534";

if ( obj.AddUser() )
Console.WriteLine("Success");
else
Console.WriteLine("fail");


obj.FirstName = "Raj";
obj.LastName = "Taslim";
if ( obj.FindUser() )
Console.WriteLine("Success");
else
Console.WriteLine("fail");

obj.FirstName = "Raj";
obj.LastName = "Taslim";
obj.Title= "Hello";
obj.Telephone= "234324324";
obj.Office = "534543534";
if ( obj.UpdateUser() )
Console.WriteLine("Success");
else
Console.WriteLine("fail");

obj.FirstName = "Raj";
obj.LastName = "Taslim";
if ( obj.DeleteUser() )
Console.WriteLine("Success");
else
Console.WriteLine("fail");






Responses

Author: farshid afzali    25 Jul 2007Member Level: Bronze   Points : 0
thanks my friend


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: A glance to C# - What, When, How
Previous Resource: Building Windows Application (Address Book) - Part VII (New)
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use