0

Check for User existence in Active Directory (Domain) using C# and AccountManagement API

by Syed Aziz ur Rahman 10. December 2013 08:02

 

System.DirectoryServices.AccountManagement API has been added to .net in version 3.5. The API makes it very easy to manage Active Directory (Domain) using code (e.g. C# ). The API lets you perform different operations like add user, delete user, check for user existence in very Object Oriented way as compare to the old API i.e. (DirectoryEntry)

To use the API, you have to add reference to "System.DirectoryServices.AccountManagement.dll" in your project.

Here is some basic code to check if the user exist in Active Directory

 

private string OU = "CN=Users,DC=test,DC=domainName,DC=com";
private string adminUserName = "testAdmin";
private string adminPassword = "T3st123";
string domainName = "Test.DomainName.com";

private void Form1_Load(object sender, EventArgs e)
{
    try
    {        
        if (CheckUserExist("testUser"))
        {
            MessageBox.Show("User found");
        }
    }
    catch (AuthenticationException secEx)
    {
        MessageBox.Show(secEx.ToString());
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    this.Close();
}

private bool CheckUserExist(string samAccountName)
{
    //Use admin account if current user does not have Active Directory rights.
    using (PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName, OU, ContextOptions.SimpleBind, adminUserName, adminPassword))
    {
        //validate the credentials of admin user.
        if (context.ValidateCredentials(adminUserName, adminPassword))
        {
            UserPrincipal existedUserPrincipal = new UserPrincipal(context);
            existedUserPrincipal.SamAccountName = samAccountName;
            PrincipalSearcher searcher = new PrincipalSearcher(existedUserPrincipal);
            if (searcher.FindOne() != null)
            {
                return true;
            }
        }
    }
    return false;
}

Tags:

DotNet

0

Add User to Active Directory (Domain) using C# and AccountManagement API

by Syed Aziz ur Rahman 10. December 2013 07:31

 

System.DirectoryServices.AccountManagement API has been added to .net in version 3.5. The API makes it very easy to manage Active Directory (Domain) using code (e.g. C# ). The API lets you perform different operations like add user, delete user, check for user existence in very Object Oriented way as compare to the old API i.e. (DirectoryEntry)

To use the API, you have to add reference to "System.DirectoryServices.AccountManagement.dll" in your project.

Here is some basic code to Add User

 

private string OU = "CN=Users,DC=test,DC=domainName,DC=com";
private string adminUserName = "testAdmin";
private string adminPassword = "T3st123";
string domainName = "Test.DomainName.com";

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        AddUser("testUser", "First", "Last", "testPass123");
    }
    catch (AuthenticationException secEx)
    {
        MessageBox.Show(secEx.ToString());
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    this.Close();
}

private void AddUser(string samAccountName, string firstName, string lastName, string userPass)
{
    //Use admin account if current user does not have Active Directory rights.
    using (PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName, OU, ContextOptions.SimpleBind, adminUserName, adminPassword))
    {
        //validate the credentials of admin user.
        if (context.ValidateCredentials(adminUserName, adminPassword))
        {
            UserPrincipal userPrincipal = new UserPrincipal(context, samAccountName, userPass, true);
            userPrincipal.GivenName = firstName; //FistName
            userPrincipal.Surname = lastName;  //LastName
            userPrincipal.DisplayName = userPrincipal.GivenName + " " + userPrincipal.Surname;
            userPrincipal.Name = userPrincipal.GivenName + " " + userPrincipal.Surname;
            userPrincipal.Save();
            userPrincipal.Dispose();
        }
    }
}

Tags:

DotNet

4

Visual Studio Extensions (VSX) Articles, Tutorials and Resources

by Syed Aziz ur Rahman 20. September 2012 15:59

I have started learning Visual Studio SDK and interested in developing an extension for Visual Studio. I am compiling of the resources that helped me.

Tags: , , ,

DotNet | DotNet | VSX | VSX

0

VsPackage 2010 How to host Windows Forms User Control in Visual Studio Tool Window

by Syed Aziz ur Rahman 14. September 2012 15:38

I recently started learning how to extend Visual Studio using packages. I will be posting more about it. Here is the first thing I learned today. Do not mind about the site layout. I will fix it soon.

When we create a new VS Package project with ToolWindow, by default a control MyControl.xaml will be added which will be tied to the ToolWindow. I do not have expertise in WPF and its controls. Therefore I tried to use normal winforms user controls. I added a new usercontrol in the project and changed the code in the MyToolWindow.cs to use that control but all I see is a blank Toolwindow when I debug the project.

//Normal code in auto generated MyToolWindow.cs
base.Content = new MyControl();

//The usercontrol i created is SimpleControl. I have changed the code to.
base.Content = new SimpleControl();

 

Here is the snapshot of the code and solution exploror.

Snapshot of a auto generated project

 

I searched a lot for the simple question. I was not able to get any answer. I knew that it should be very simple. I found another guy asking the same question on stackoverflow here but no answer. Finally posted the question on MSDN forum. Mr. Ed Dore was nice enough to answer it here. I am cross posting it here for more visibility in search engine.

The ToolWindowPane can be used to host WPF content or a Winform control.

For a Winform control, you just need to override the Window property get, and leave the Content property null.

For example:

public MyToolWindow() :  base(null)
    {
        this.Caption = Resources.ToolWindowTitle;
        this.BitmapResourceID = 301;
        this.BitmapIndex = 1;
        control = new MyControl();
    }

     override public System.Windows.Forms.IWin32Window Window
    {
        get
        {
            return (System.Windows.Forms.IWin32Window)control;
        }
    }



I hope this will help someone.

Tags: ,

DotNet | VSX

26

Migrate From Community Server 2.1 to BlogEngine.Net

by Syed Aziz ur Rahman 31. December 2011 18:51

Recently I need to migrate my blog (http://weblogs.com.pk/aziz)  which was running on Community Server 2.1 to my new blog here which is running on BlogEngine.Net.  I did not have access to code or database as the blog was hosted on a free blogging site. I was unable to find any direct and easy way in the control panel of Community Server therefore I started looking for other options

Now you may or may not have access to code. Therefore I will discuss both options.

How to export if code is accessible:

Keyvan Nayyeri has developed a user control that you can upload, add it to the syndication page and generate BlogML xml of your blogs. Then you can use the import option in the settings of BlogEngine.Net to migrate the data. Check Step 3 below for details about import to BlogEngine.Net. You can find the details about the converter  here. You can also download the code from BlogML codeplex site here. This link also contains other Community Server version utilities, Wordpress export utility and a RSS to BlogML convertor which we will used if we do not have access to our blog codebase.

Here is a screen shot of how it looks like.

 

How to export if code is NOT accessible:

The technique is to use the Community Server RSS feed to generate BlogML xml using a tool called RSS2BlogML, then change BlogEngine.Net code to fix on bug and then finally use the import option of BlogEngine.Net to migrate data.

 Step 1: Generation of BlogML Xml from RSS feed

Rss2BlogML , courtesy of  Simone Chiaretta,  is a simple tool that downloads RSS feed, comment RSS (if provided), and then convert everything to BlogML format. You can download the exe as well as the source code from here (Check other available download sections). It was only tested to work with .Text and with the core blog module of DNN and was having few problems with Community Server 1.2 RSS dialects. Hence I added the functionality in the original code. You can download the latest code from here which supports Community Server 2.1.

Here is the screen shot of the updated version. Please read the original post to understand how to use the tool.

 

Step 2: Fix a minor bug in BlogEngine.Net code.

If any of the blog post has comments in the generated BlogML file and the email address is not there, then BlogEngine.Net code will throw exception. Unfortunately Community Server 2.1 may not always provide email associated with comments. If comments in your blog posts have email addresses then you can skip this step.

Open \BlogEngine\DotNetSlave.BusinessLogic\Providers\DbBlogProvider.cs and go to line:2496.  If you have not changed this file earlier, you will see the code below. Otherwise search for method “UpdateComments” and search the code below.

parms.Add(conn.CreateParameter(FormatParamName("email"), comment.Email ));

and change it to

parms.Add(conn.CreateParameter(FormatParamName("email"), (comment.Email == null ? string.Empty : comment.Email.ToString())));

Compile and upload the dll to your hosting.

Import to BlogEngine.Net

  • Login to your BlogEngine.Net control panel.
  • Click on settings and then on Import & Export.
  • Click on the browse and select your BlogML file.
  • Click on import and done.



RSS2BlogMLv22

 

References

Tags: , , ,

DotNet

4

My article, SumColumn, has been updated.

by Syed Aziz ur Rahman 8. March 2006 00:27

Few days back, I have informed about my article, SumColumn published on codeproject. Now It has been updated incorporating the users comments. You can visit it here. Your comments are welcomed.

Tags: ,

ASP.Net | ASP.Net | DotNet | DotNet

2

Outsourced opportunity – SMS / MMS application

by Syed Aziz ur Rahman 27. December 2005 00:22

A Saudi Arabia based e-news/stock company wants to outsource an application dealing with SMS/MMS. The bird's eye view of the application is like.

 

1) User can subscribe to the SMS/MMS service through the website or by sending SMS with a given code to a given number

          E.g. SMS 195 BN i.e. user wants to subscribe to BN (breaking news)

 

2) SMS can be scheduled to send according to subscriptions.

 

3) User can ask something using SMS and get reply like

          E.g. SMS 195 SR 1010 i.e. user want the SR (stock rate) of the company (no 1010). He should be instantly replied back with the current stock value.

 

Above listed are some of the features that the company wants to have in the application. Please contact if your company has experience of developing these kinds of applications OR you have any out of the box solution. Urgent replies will be appreciated.

Tags:

ASP.Net | DotNet | General

0

Microsoft Products 2005 Launch in Riyadh

by Syed Aziz ur Rahman 9. December 2005 23:52
The Microsoft Products 2005 Launch Event in Riyadh was on 5th of this month i.e. last Monday. The banner says Microsoft SQL Server, Visual Studio and BizTalk but there was not a single event for BizTalk. The quality of presentations was poor (as far as technical point of view). I actually slept in two sessions, 10 min each. First the presentation was more towards marketing, covering the licensing packages, secondly the technical stuff was toooooo basic.

In VS. Net session, the guy was doing stuff like drag drop of grid and was happy with it. Man, show something new. The SQL Server guy said that the first prototype of SQL Server 2005 was developed in Jan 2001.hmmm thats new. He said that his presentation will cover business aspcets of SQL Server and he did :(.   Team System presentation by Ammar (one of the main guys in Microsoft Saudia) was good. He kept the audience involved all the time.

I have read the blog by Fahad saying that the administration in Pakistan for this event was not so good. Here nearly all of the Microsoft events like GDC, DevChat etc were organized by a third party MarcomArabia. They are good.

Last but not the least, I got my ticket confirmed for 7th Jan. I am coming home on this Eid inshAllah. I really want to pass this month as quickly as possible. It is only 20 days vacation but at least it is. Now may be I will join the coffee meeting :). Is it done? I do not think so. I did not read any news regarding the meeting.

Chao

Tags:

DotNet

1

Integration of Exchange with SharePoint

by Syed Aziz ur Rahman 12. July 2005 04:00

Tags:

DotNet

0

Compression in Dot Net (Zip format).

by Syed Aziz ur Rahman 9. July 2005 06:33

Like many of you out there, I have also used #ZipLib (pronounced as SharpZipLib) to do compression programmatically. For those who do not know about #ZipLib, it is an open source compression library for zip formats, written in C#. The code is available at their website. I was thinking that how it is possible that Microsoft did not provide any type of compression APIs in Dot Net Framework. After googling for some time, I came to know that........(read more)

Tags:

ASP.Net | DotNet

Powered by BlogEngine.NET 2.5.0.6
Original Design by Laptop Geek, Adapted by onesoft