jasminie

jasminie

  • NA
  • 78
  • 0

C# active directory

Sep 1 2011 6:31 PM


I have a C#.net 2008 desktop application that I modified to read the active directory to obtain what group(s) each user has access to. My company told me the windows and web applications should use the same logic when accessing the active directory. Since the web application was completed first, I need to find a way to use the web method of accessing the active directory.

Thus I have the following questions about the desktop code listed below versus the web code listed below also:

1. Thus can you tell me if there is a way to use the web code in the windows version of accessing the active directory? If so, can you tell me how to modify the code so it would work in the windows application?
2. Is there a way to use at least part of the web code. If so, can you show me what code can be used?
3. If there is no way to use the web code and I should use the windows code that works, can you tell me why the web code would not work?

--------------
DESKTOP CODE
--------------

The following code is called from various portions of the desktop application. Right after the following class module returns from the application, the following line of code is executed in each section for the vatious groups that have been setup.

if ((Thread.CurrentPrincipal.IsInRole("testi1")
then do some process.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Threading;
using System.Web;
using System.Windows.Forms;
using System.Security.Principal;


namespace Common.Area
{
public class ActiveDirectoryUser
{
public ActiveDirectoryUser()
{
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());

}

}
}
----------

WEB CODE
----------
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace Sup
{
public class ActiveDirectoryValidator
{
private string _path;
private string _filterAttribute;

public ActiveDirectoryValidator(string path)
{
_path = path;
}

public bool IsAuthenticated(string domainName, string userName, string password)
{
string domainAndUsername = domainName + @"\" + userName;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, password);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + userName + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return true;
}

}
}

Reply
Quote




















































Answers (4)