473,739 Members | 9,109 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Authenticate user against active directory

Hi All,

I'm builing an application that requires domain admin access to run,
and I'm trying to allow for the application to be run as a normal user
and allow the user to provide it with a username/password that has the
access.

I have a method that will check if the username/password is correct,
however, it will only authenticate the user running the program...

Here's the method:

public static bool validatePasswor d(string adUserName, string
adPassword)
{
DirectoryEntry de = new DirectoryEntry( null, "WDE" +
"\\" + adUserName, adPassword);
try
{
object o = de.NativeObject ;
DirectorySearch er ds = new DirectorySearch er(de);
ds.Filter = "samaccountname =" + adUserName;
ds.PropertiesTo Load.Add("cn");
SearchResult sr = ds.FindOne();
if (sr == null) throw new Exception();
return true;
}
catch
{
return false;
}
}

If I check the username/password of the person that is running the
application, it works fine. If I provide any other username/password,
it fails in the "object o = de.NativeObject ;".

Any ideas as to why it's happening?

Cheers,
Nathan Manzi

Feb 23 '07 #1
1 13263
"fomalhaut" <na**********@n athanmanzi.comw rote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hi All,

I'm builing an application that requires domain admin access to run,
and I'm trying to allow for the application to be run as a normal user
and allow the user to provide it with a username/password that has the
access.

I have a method that will check if the username/password is correct,
however, it will only authenticate the user running the program...

Here's the method:

public static bool validatePasswor d(string adUserName, string
adPassword)
{
DirectoryEntry de = new DirectoryEntry( null, "WDE" +
"\\" + adUserName, adPassword);
try
{
object o = de.NativeObject ;
DirectorySearch er ds = new DirectorySearch er(de);
ds.Filter = "samaccountname =" + adUserName;
ds.PropertiesTo Load.Add("cn");
SearchResult sr = ds.FindOne();
if (sr == null) throw new Exception();
return true;
}
catch
{
return false;
}
}

If I check the username/password of the person that is running the
application, it works fine. If I provide any other username/password,
it fails in the "object o = de.NativeObject ;".

Any ideas as to why it's happening?

Cheers,
Nathan Manzi

start by a change of your catch clause into:

catch(System.Ru ntime.InteropSe rvices.COMExcep tion ex)
{
Console.WriteLi ne(ex);
return false;
}

and it will tell you why, however, there are other things wrong with your code.

This aside, you should not use this to authenticate a windows user, AD is not an
authentication service.
Use Win32 "LogonUser" or better use the SSPI in V2 of the framework . Following is a
complete sample that illustrates you how to authenticate windows users , both local and
domain users, using the WindowsIdentity and the NegotiateStream class in V2 .

using System;
using System.Net;
using System.Net.Sock ets;
using System.Net.Secu rity;
using System.Security .Principal;
class Program
{
static void Main(string[] args)
{
// pass account name, password and domain name as arguments. For local accounts,
pass the machine name as domain name.
WindowsIdentity id = SSPIHelper.Logo nUser("uuuuuu", "pppppp", "ddddd");
if(id != null)
Console.WriteLi ne("[{0}] was authenticated using [{1}], returned access token
[{2}] ",
id.Name,
id.Authenticati onType,
id.Token.ToStri ng()
);
}
}
// using NTLM authentication
public class SSPIHelper
{
public static WindowsIdentity LogonUser(strin g userName, string password, string domain)
{
// need a full duplex stream - loopback is easiest way to get that
TcpListener tcpListener = new TcpListener(IPA ddress.Loopback , 0);
tcpListener.Sta rt();
WindowsIdentity id = null;
tcpListener.Beg inAcceptTcpClie nt(delegate(IAs yncResult asyncResult)
{
using (NegotiateStrea m serverSide = new NegotiateStream (
tcpListener.End AcceptTcpClient (asyncResult).G etStream()))
{
serverSide.Auth enticateAsServe r(CredentialCac he.DefaultNetwo rkCredentials,
ProtectionLevel .None, TokenImpersonat ionLevel.Impers onation);
id = (WindowsIdentit y)serverSide.Re moteIdentity;

}
}, null);

using (NegotiateStrea m clientSide = new NegotiateStream (new
TcpClient(IPAdd ress.Loopback.T oString()
((IPEndPoint)tc pListener.Local Endpoint).Port) .GetStream()))
{
clientSide.Auth enticateAsClien t(new NetworkCredenti al(userName, password,
domain),
"", ProtectionLevel .None, TokenImpersonat ionLevel.Impers onation);
}
return id;
}
}

Willy.
Feb 23 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1964
by: GC | last post by:
I'm looking to build a web app that authenticates against my current active directory. Anyone have any examples on how to do this? Thanks!
7
5217
by: Sync Walantaji | last post by:
Hi, I would like to write a asp.net winform program to authenticate users on Active Directory. Can I do this with asp.net if the IIS server is not part of the Active directory domain? Is there a working example link that you can point me to? Thanks
0
1196
by: Lajus Norvejikus | last post by:
Hi all, I want to develop a web service to authenticate user/pass against a Active Directory domain. I only don't know how to authenticate the credentials user enters against a AD domain. Anyone knows? Maybe using LDAP? Thanks. Pedro L.
1
4560
by: kevin.vaughan | last post by:
Hello Everyone, Is it possible to authenticate the windows password through Active Directory? If so, how would this be done. I have a login screen in my application and am trying to set it up with the same login details as Windows to streamline the experience for users. It also must allow 1 user to log onto the application while another user is logged onto windows. I must know who is logged onto the
3
26652
by: dorrit.Riemenschneider | last post by:
I need to validate a user with username and password against our OpenLDAP active directory. This is my code: Private bool ValidateUser (string username, string password) { DirectoryEntry userEntry = new DirectoryEntry( ldapPath, username, password, AuthenticationTypes.Anonymous); //Bind to the native AdsObject to force authentication.
1
3509
by: Michael Howes | last post by:
I would think this would be very, very easy but in the 50 searches I've done I haven't found anything. If our application requires login and that user/password be a local windows account or more detailed, a user that has been added to the Power Users group that is either a local account or a active directory account how do I authenticate? I've found code that seems to do this against Active Directory
4
2287
by: Jon | last post by:
I am modifying an app for a customer in ASP.Net 1.1. The app is running on a server outside their network, yet they want to authenticate users against their internal active directory set up (they will open the necessary ports). So, I have a simple login page with username and password, and then I will authenticate that credentials entered against their AD server. I am having a real hard time figuring this out. We can't use Windows Forms...
2
5184
by: LIKKLE MAN | last post by:
Can anyone point me to an article that explains how to get an instance of DB2 running on AIX 5.x authenticating against an Active Directory server. There is no issue securing AIX itself in this manner, but is this even possible in the AIX world from a db perspective? Thanks
0
1940
JustRun
by: JustRun | last post by:
Hi, I'm developing a windows application using VC#, I need to authenticate user throught their credentials in the Active Dirctory, I try to let the login form "user name" take automaticaly the username in the active directory, I don't know whether I wrote is right or wrong, but at any rate it doesn't work. here is my code: public static string GetProperty(SearchResult searchResult, string PropertyName) { ...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9479
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9266
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6054
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3280
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.