473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

UserName through Active Directory(Internal) or LoginPage(External Users)

For external users, the login Page is what I want. Credentials are verified against database.dbo.table. Some of these users might not even be part of Active Directory. So that's fine. But for internal users, whom are part of Active Directory, I want to bypass login Page amd get username from Active Directory and go directly to process Application. How do I get that userName from Active directory. Everything I've tried comes up empty, unless I'm debugging from localhost. Thanks.
Aug 24 '12 #1
1 1995
PsychoCoder
465 Expert Mod 256MB
.NET offers the System.DirectoryServices Namespace for working with Active Directory (making common tasks much simpler). Here's a short example (crude but simple) of authenticating a user against AD:

Expand|Select|Wrap|Line Numbers
  1. public bool Authenticate(string domain, string username, string pwd)
  2.     {
  3.       string domainAndUsername = domain + @"\" + username;
  4.       DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
  5.  
  6.       try
  7.       {
  8.         //Bind to the native AdsObject to force authentication.
  9.         object obj = entry.NativeObject;
  10.  
  11.         DirectorySearcher search = new DirectorySearcher(entry);
  12.  
  13.         search.Filter = "(SAMAccountName=" + username + ")";
  14.         search.PropertiesToLoad.Add("cn");
  15.         SearchResult result = search.FindOne();
  16.  
  17.         if(null == result)
  18.         {
  19.           return false;
  20.         }
  21.  
  22.         //Update the new path to the user in the directory.
  23.         _path = result.Path;
  24.         _filterAttribute = (string)result.Properties["cn"][0];
  25.       }
  26.       catch (Exception ex)
  27.       {
  28.         throw new Exception("Error authenticating user. " + ex.Message);
  29.       }
  30.  
  31.       return true;
  32.     }
  33.  
  34.  
Hope that helps :)
Aug 24 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Hermes | last post by:
Hi all, We have a web page which validates users againt active directory (kinda), Effectively the code we use will check the user's password and if successful creates some session's for what...
1
by: Frank00 | last post by:
I run a .NET based portal product. I am using windows authentication. This mode of authentication and the string specifying the connection to AD is specified in the application's web.config file....
3
by: daniel | last post by:
Hi, i want to use dotnet web form to do SSO with AD now, but i don't know to do it. where can i look for relative article or sample code ? thanks.
4
by: Ê÷Éϲä»Ò | last post by:
'rar' is not recognized as an internal or external command, operable program or batch file. import os import time source = target_dir = r'e:\temp\bak' target =...
9
by: Ernest Griffin | last post by:
I am trying to determine if the logged in user belongs to an Active Directory Group. I have started with these code snippets: WindowsIdentity id = WindowsIdentity.GetCurrent();...
2
by: Jim in Arizona | last post by:
My goal, somehow, is to populate a dropdownlist with all the user names in active directory. I don't even know where to begin, really. I added a reference to System.DirectoryServices so I could...
4
by: IainM | last post by:
How can I enumerate AD objects (only in a given OU, not sub OUs) using the DirectoryEntry object? Let me know of this is the wrong forum for this question. Thanks, Iain
1
by: mondalep | last post by:
Is there any way using AD to set object level permissions down to the object level in MS Access, the way it is done internally via the Access security workgroup? Does Active Directory offer the...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.