I asked this question in the C# group also since I do not know what forum to post this message to.
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;
}
Your Company/Organization is very right that both windows as well as web uses is common way to access Active Directory . Interface used to access active directory info is called ADSI.
Yes,only difference will lie in how you are going to show information on the interface and fact is that that part you have to separate;like web controls are different from windows.
Anyways, I believe you can use 99% of web code in windows application except some portion which is about UI.
If you can mail me your web project( which is already developed);I will change that to windows version with one day
scamperman
0 Points
3 Posts
C#.net active directory
Sep 02, 2011 02:29 PM|LINK
I asked this question in the C# group also since I do not know what forum to post this message to.
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;
}
}
</div> <input id="052ea19a-f886-4042-b024-9f75945d3078_attachments" value="" type="hidden" /></div> <div class="menu message">}

Reply

Quote
<div class="clear"></div> </div> <div class="clear"></div> <div id="nrSurvey"></div> <div id="rail">Need Help with Forums? (FAQ) <div></div> <div></div> <div> <div class="box">My Forum Links
<div class="content"> <div class="profile-usercard-inline" data-profile-userid="df1451a0d74f4b17b5b2a89ed53af685" data-profile-usercard-customlink="{"href":"http://social.technet.microsoft.com/Forums/en-US/user/threads?user=wendy elizabeth", "text":"wendy elizabeth's threads"}"> <div class="profile-usercard profile-inline"> <div class="profile-inline-header">- My Settings
- My Profile
- My Threads
- My Alerts
- My Forums
- My Forums Threads
- Forums Home
- Browse Forums Users
</div> </div> <div> <div class="box">Related Topics
How
can I programatically query list of Active Directory Security ...
multiple
domains in Active directory
Cannot
add security groups in wss using ...
Can
we migrate this custom schema to Active Directory?
Problem
on Specifying Active directory group name in Web part ...
Sharepoint
2007 Active Directory groups can be added but get no ...
Exchange
Server 2010: Active Directory Preparation
PowerShell
GUIs - Active Directory, Directory Object Picker?
Add
a user into multiple groups
basic
coding: connecting to exchange guide?
</div> </div> <div> <div class="box">Statistics
- Started: 1:55 PM
- Helpful Votes: 0
- Replies: 0
- Views: 10
</div> </div> </div> <div class="Clear"></div> <div class="Clearbottom"></div> <div class="bottomleftcorner"></div> <div class="bottomrightcorner"></div> <div id="Footer"> <div class="FooterLogoContainer"> <div class="FooterLogo" title="Microsoft Corporation"></div> </div> <div class="FooterLinks"> <div id="FooterCopyright" class="FooterCopyright">© 2011 Microsoft. All rights reserved.</div> Terms of Us</div> </div>shabirhakim1
Star
13496 Points
2145 Posts
Re: C#.net active directory
Sep 02, 2011 03:15 PM|LINK
Hi,
Your Company/Organization is very right that both windows as well as web uses is common way to access Active Directory . Interface used to access active directory info is called ADSI.
Yes,only difference will lie in how you are going to show information on the interface and fact is that that part you have to separate;like web controls are different from windows.
Anyways, I believe you can use 99% of web code in windows application except some portion which is about UI.
If you can mail me your web project( which is already developed);I will change that to windows version with one day
shabir_hakim1@hotmail.com
Regards