Wednesday, May 31, 2006

IF

There are some things in life that just change the way you think and live your life from there on. Each one finds comfort in its own way. Here is one thing that has changed me and surley is bound to have an impact on you.

If - by Rudyard Kipling.

"
If you can keep your head when all about you
Are losing theirs and blaming it on you;
If you can trust yourself when all men doubt you,
But make allowance for their doubting too:
If you can wait and not be tired by waiting,
Or, being lied about, don't deal in lies,
Or being hated don't give way to hating,
And yet don't look too good, nor talk too wise;

If you can dream - and not make dreams your master;
If you can think - and not make thoughts your aim,
If you can meet with Triumph and Disaster
And treat those two impostors just the same:.
If you can bear to hear the truth you've spoken
Twisted by knaves to make a trap for fools,
Or watch the things you gave your life to, broken,
And stoop and build'em up with worn-out tools;

If you can make one heap of all your winnings
And risk it on one turn of pitch-and-toss,
And lose, and start again at your beginnings,
And never breathe a word about your loss:
If you can force your heart and nerve and sinew
To serve your turn long after they are gone,
And so hold on when there is nothing in you
Except the Will which says to them: "Hold on!"

If you can talk with crowds and keep your virtue,
Or walk with Kings - nor lose the common touch,
If neither foes nor loving friends can hurt you,
If all men count with you, but none too much:
If you can fill the unforgiving minute
With sixty seconds' worth of distance run,
Yours is the Earth and everything that's in it,
And - which is more - you'll be a Man, my son!

"

Labels:

Tuesday, May 30, 2006

Adding Members to the Active Directory Group using .NET

Recently on my project we had a requirement to add members to an Active Directory group.Now after going through every possible place on help and google, i finally found the holy grail and thought to share some findings and code bits.
We had to add computer names(implementation is same for user_name also) to the active directory group.
Some important points to be kept in mind while working active directory groups:
1. Get the Group name. Make sure you are an admin on that group (or use impersonation), if you want to make changes.Imp: You also need domain name to which the group belongs. just a group name will not do.
2. We have to find the exact path on which this group exist.
3. After finding the exact location of the group, we can add the members to the group.
4. Only exisitng members in the active directory can be added to an active directory group. Members can belong to any domain in the organization. Only important thing is that member does exist in the active directory. For this we also need the path of the member where it exists in the Active Directory.

Lets just take a look at the path and try to understand:1. CN=machineName,OU=Servers,OU=Computers,OU=Unit1,DC=dc1,DC=dc2,DC=com
this is my machine name path in the active directory.
2. LDAP://domains/CN=groupName,OU=Groups,OU=Users,OU=Unit1,DC=dc1,DC=dc2,DC=com
this is my group name path in the active directory.
Now you can see the difference between the two. Lets try and think how they are logically placed

O = Organization
|_____OU = Servers
|_____ OU = Computers
|______ OU = Unit1
|_____ DC = dc1.dc2.com

O = Organization
|_______ OU = Groups
|_____ OU = Users
|______ OU = Unit1
|_____ DC = dc1.dc2.com

CN is the class name.

.NET with Active Directory

Namespace:using System.DirectoryServices

Get Group Path name. Also if you know the domain\, you can find the path for that also.

/// Method is used to get the group path.
/// param name="groupName" Domain\GroupName
/// returns LDAP://domain/CN= groupName,OU=Groups,OU=Users,OU=Unit1,DC=dc1,DC=dc2,DC=com

private string GetLdapPath(string domainGroupName)
{
try
{
//separate the domain name and the groupname
string domain = domainGroupName.Substring(0, globalGroupName.IndexOf(@"\"));
string groupName = domainGroupName.Substring(globalGroupName.IndexOf(@"\")+1);

DirectoryEntry oEntry = new DirectoryEntry();
oEntry.Path = "LDAP://" + domain;

//search the directory on the specified path.
DirectorySearcher oSearcher = new DirectorySearcher(oEntry);

//this is a default format for searching a group name.. .dont change "SAMAccountName"
oSearcher.Filter = String.Format("(SAMAccountName={0})",groupName );
oSearcher.PropertiesToLoad.Add("cn");

SearchResult oResult = oSearcher.FindOne();

if(oResult == null)
{
throw new Exception("Group: " +groupName+ " not found in the domain: "+domain);
}
else
{
return oResult.Path;
}
}
catch(Exception ex){
}

Adding member to the active directory group. In the code e.g. i am adding a machine name. But you can also add an already exisitng user name. Whoever be the member, you need the Ldap path for that. Use the same method GetLdapPath to get the user_name path or machine_name path. Make sure you have domain name for them.

/// Method is used to add members to the global group.
/// param name="domainGroupName"
/// param name="machineName"
public void AddMembersToGroup(string domainGroupName, string machineName)
{
//get path for group name and machine name.
string groupDirectoryPath = GetLdapPath(domainGroupName);
string machineLdapPath = GetLdapPath(machineName)

//create the instance of the Directory Entry on the path found.
DirectoryEntry oGroupEntry = new DirectoryEntry();
oGroupEntry.Path = groupDirectoryPath;

try
{
//add a new member
oGroupEntry.Properties["member"].Add(machineLdapPath);
//commit the changes to reflect in the directory.
oGroupEntry.CommitChanges();
}
catch(Exception ex){}
}

Delete memebers from the Group.

/// Method is used to delate all the memebrs from the global group.
/// param name="groupName"
public void DeleteAllMembers(string groupName)
{
string groupDirectoryPath = GetLdapPath(groupName);

DirectoryEntry oEntry = new DirectoryEntry();
oEntry.Path = groupDirectoryPath;

try
{
//clear all the members.
oEntry.Properties["member"].Clear();
//commit changes.
oEntry.CommitChanges();
}
catch(Exception ex){}
}

Read the members from the Active Directory Group. The method below will write on the console all the members in the group.
This is really if good if you application authorization and authentication is based on the role of members of the active directory as it saves the time of creation of user database for your application.

/// Method is used to loop through the group and find members.
/// param name="groupName"
public void SearchGlobalGroup(string groupName)
{

//instance of the directory entyry
DirectoryEntry oEntry = new DirectoryEntry();
oEntry.Path = GetLdapPath(groupName);
//impersonate if the security context the app is running does not have permission on the Directory.
oEntry.Username = "user_name";
oEntry.Password = "pswd";
oEntry.AuthenticationType = AuthenticationTypes.Secure;


try
{
SearchResult result;
DirectorySearcher oSearcher = new DirectorySearcher(oEntry);
oSearcher.PropertiesToLoad.Add("member");
result = oSearcher.FindOne();

if (result != null)
{
for (int counter = 0; counter < result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];
Console.WriteLine(user);
}
}


}
catch(Exception ex){}
}

For more info check these links:
http://www.csharphelp.com/archives3/archive582.html
http://www.ondotnet.com/pub/a/dotnet/2003/08/04/activedir.html
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdirectoryservices.asp

Labels:

Wednesday, May 24, 2006

ANOTHER BRICK IN THE BLOG

Ok this is what happens when you don’t have anything to write but don’t want to give up and end up writing on what you want to write!!!!
(don’t know if it makes sense..)

What to Blog?

It should be something that other people like!!!! Isn't this what it should be, since u are not writing for yourself but for other people to read.
aaaeeehhh on a serious note I feel it should be something u are really interested in. People come to read your blog because they want to read your opinion, if they want to read what they like let them read their own.
We cannot forget the content as it is the single most and only important thing. Make sure your writing has retained value to which people come again and again and don’t loose interest.

When to Blog?

May be once a day or being reasonable alternate day. Key here is not to burden yourself that you have TO write. Blog it when you are ready.
If you are really a serious writer to make a difference may be once a week can also do, but then underline thing is content. For such people writing every day may be just too much as it takes lot of time to conceive, write and edit.
Too less frequency means too less traffic and less people interested in you. people may simply loose interest in what you write since nothing new hasn’t come in long time.
If you keep you blog more like a repository of other links, then go on blog it every hour.

Blog management

If your blog does not follow a specific trend and you keep moving between topics, it can surely keep the readers interest and show your versatility. This is what i like the most, or else you may just end writing a blog which is not much more than a Java Help Forum.
If you really want to connect with your users keep the interest, each entry may be independent but can have some associations with old ones, so that a reader can associate and try to get inside your mind. Because believe me that’s what the readers want.

Some people have very techie techie blogs and some write on every possible news thats found on the net, some are story writers, some more opinion oriented, some are intellect and write on various industries and trends, but all and all they are just ANOTHER BRICK IN THE BLOG (read WALL).

Go on pick up your comfort zone and blog the world.

Labels:

Tuesday, May 23, 2006

Time to buy some BOXES

Me and my friend Rakesh met this weekend, just old college friends get together and topic shifted from other college buddies to how Japan has revolutionized the world of manufacturing, setting high standards not only in production but also processes and techniques.
And then something strange happened when he asked me do u know how a steam engine works. The most important, very basic and probably very elementary of the machine designed by man. Strange though and I agreed that I don’t remember anything that I read, except for something called as cylinder and piston and some energy flow that moves the piston to generate some other kind of energy.
Shameful I thought, and felt more humiliated thinking of the amount of time I spent in front of internet but still know so little.
Think of the discoverer, innovators, scientists in the beginning of 18th and 19th century who made something which was at that time totally inconceivable. The effect those innovations had, is something that has changed the world.
Exploration that they did to find the land of spices and gold, efforts in the field of science, mathematics, medicine is just what today’s world is made up of. Off course things have improved and changed in last 100 years but remember its always easy to eat served food. It is what it takes to make it.
I dont know what i want to derive out of this, but I always used to believe that I like to think out of the box but now feel that my box is just too small.
May be the time has come to buy some new BOXES.

Labels:

Wednesday, May 17, 2006

"Hello World"

public static void Main()
{
Console.WriteLine("Hello World");
}

I just finsihed reading Thinking in C# and you can already see the effect...
Ok i have to come back to the fact that this is my first post and have few things to say..

Who am I?
I would say "I am Software Engineer with a heart of a Rock Star".
Qualification: MCA, MBA
Interests: Guitar is my life, my DVD collection is my air, books are my food... in short NO Girl friend right now!!!

Why?
WhyNOT!! In the world of lost thoughts and opinions i would love to be lost only to be found by some one more lost...

How?
aaeehhh lets just skip this

When?
Starts today 17 May 2006 10:00 pm India time

Rules
Everything GOES,
Pink Floyd ROCKS,
Sachin is GOD,
France is HEAVEN,
Schumi is the FASTEST,
and when i think of it... I have miles to go before i sleep...