jasminie

jasminie

  • NA
  • 78
  • 0

C#.net two active directory comparisons

Aug 30 2011 5:51 PM
My goal is to try to use the same logic when verifying groups in the active directory for users if they are on the web or in a windows application. However my current problem is the web application is currently using a config file and having users enter their user name and password. The windows application is having the user click on a desktop shortcut and the application obtains the user infromation from windows authenication.

Is there a way to make code listed in the windows application be more like the web application?
If not,why not?
If so, how would you change the code to make to the windows application?

The windows code is the following:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
hread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if ((!Thread.CurrentPrincipal.IsInRole("TEST"))
{
MessageBox.Show("Please contact your network administrator if you have any questions",MessageBoxIcon.Error);
return;

}


else

{
Application.Run(new newm());
break;
}


The web code is the following:
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace RtSup
{
public class Validator
{
private string _path;
private string _filterAttribute;

public Validator(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
{
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;
}
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return true;
}

}
}

Answers (1)