Solved

How do I use form authentication ASP.NET  intranet site using Active Directory

Posted on 2010-11-08
16
414 Views
Last Modified: 2012-05-10
Can someone help, I have been browsing the web and nothing seem to work. My applicaition is a 3.5 asp.net vb web site. And is hosted locally in the same network that the active directory server is ...

How do I create the connection string?
What information do I need , and how do I get it. e.g. domain name, port???
how do I link this so I can use the login asp.net control

?

thanks!!!
0
Comment
Question by:TonyReba
  • 9
  • 6
16 Comments
 
LVL 9

Expert Comment

by:puru1981
Comment Utility
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
I been trying all those codes and walkthroughs but I have had not luck to make my site work?

can you please look in some of the specific questions I asked?
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
How do I create the connection string?
--> Create a test page and put the code suggested in the link in the pageload and output it in the browser.
http://forums.asp.net/p/943717/1340605.aspx

What information do I need , and how do I get it. e.g. domain name, port???
--> You might also ask you IT admin for this admin

how do I link this so I can use the login asp.net control
---> Linking with Login Control will be easy. Handle OnAuthentication event. Inside the handler you do this:

string domain = "SomeDomain";
e.Authenticated = DoADAuthentication(Login1.UserName,Login1.Password, domain);

--->DoADAuthentication is the method that will do AD authentication
---> Also if you are using AD MembershipProvider, LoginControl should work out-of-the box without the code I suggest above but you will have to modify your web.config to make the ADMembershipProvider your default provider.
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
Create a test page and put the code suggested in the link in the pageload and output it in the browser.

So if I test by placing the code in the browser by itself it opens a Find People window, but can't find people???

What does that mean??
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
Ok I have the server name ,

can you exlpain how this piece of code would authenticate , I am using .net 3.5. vb


string domain = "SomeDomain";
e.Authenticated = DoADAuthentication(Login1.UserName,Login1.Password, domain);

0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
Sorry I think I provided incomplete info. Place this code in the code-behind of your testpage:

DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");

using (root)
{
    string dnc = root.Properties["defaultNamingContext"][0].ToString();
    string server = root.Properties["dnsHostName"][0].ToString();

    string adsPath = String.Format(
        "LDAP://{0}/{1}",
        server,
        dnc
        );
     Response.Write(adsPath);
}

It should output something like this: LDAP://abcd.xyz.com/DC=abcd,DC=xyz,DC=com
You can try that as your connectionstring.
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
Yes I am following and now I have the connection string defined

can you please explain me the event handler for authenticate
string domain = "SomeDomain";
e.Authenticated = DoADAuthentication(Login1.UserName,Login1.Password, domain);

0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
Comment Utility
Check the code here: http://support.microsoft.com/kb/326340

So your function should be: "IsAuthenticated" instead of "DoADAuthentication"

And your code would be:

Protected Sub Login1_Authenticate(sender As Object, e As AuthenticateEventArgs) Handles  Login1.Authenticate

Dim domain As String = "SomeDomain"
e.Authenticated = IsAuthenticated(domain,Login1.UserName,Login1.Password)

End Sub
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

Author Comment

by:TonyReba
Comment Utility
hi I am almost there bu is giving me the following error
:

Error      1      'Public Event Authenticate(sender As Object, e As System.Web.UI.WebControls.AuthenticateEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.      C:\Documents and Settings\gtrrra00\My Documents\Visual Studio 2010\WebSites\intranetLogin\Default.aspx.vb      52      
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
can you share your login page code?
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
this is my full code::
Imports System

Imports System.DirectoryServices

Imports System.Web.Security.MembershipProvider

Imports System.Text

Imports System.Collections





Partial Class _Default

    Inherits System.Web.UI.Page



    Dim _path As String

    Dim _filterAttribute As String



    Public Sub New(ByVal path As String)

        _path = path

    End Sub



    Public Function IsAuthenticated(ByVal domain As String, ByVal username As String, ByVal pwd As String) As Boolean



        Dim domainAndUsername As String = domain & "\" & username

        Dim entry As DirectoryEntry = New DirectoryEntry(_path, domainAndUsername, pwd)



        Try

            'Bind to the native AdsObject to force authentication.			

            Dim obj As Object = entry.NativeObject

            Dim search As DirectorySearcher = New DirectorySearcher(entry)



            search.Filter = "(SAMAccountName=" & username & ")"

            search.PropertiesToLoad.Add("cn")

            Dim result As SearchResult = search.FindOne()



            If (result Is Nothing) Then

                Return False

            End If



            'Update the new path to the user in the directory.

            _path = result.Path

            _filterAttribute = CType(result.Properties("cn")(0), String)



        Catch ex As Exception

            Throw New Exception("Error authenticating user. " & ex.Message)

        End Try



        Return True

    End Function





    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

        Dim domain As String = "SomeDomain"

        e.Authenticated = IsAuthenticated(domain, Login1.UserName, Login1.Password)



    End Sub

End Class

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
what kind of project is that? WebApplication or WebSite?
do this...go to designer, select your LoginControl, in properties, go to it's Events list and double click OnAuthenticate.
See if that changes anything.

Also I would like to point out:
1: "SomeDomain" should be replace with appropriate value for your case. e.g. it could be the domain you use when you login in your window machine in your network.
2: set _path = "You Ldap connectionstring" before you call IsAuthenticated method
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
I am sorry for my ignorance,,what is the difference between ? WebApplication or WebSite?

I beliveve is an intranet web site?

I dont see the OnAuthenticate event  only Logging_In

this lines seems to be the conflict,,,
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
sorry should be "Authenticate" and the is no reason for it to not appear..it has to...so may be you are not seeing it in the right place.
Ssomething like this listing:
http://www.aspnettutorials.com/images/controls/HowtoAddErrorstoLoginASP4-CSharp/ss1.jpg

To see if it is a Web App of Website... check the icon of your application in VS Solution Explorer.
It is just "Globe+Document" or "Globe+vb" or you can check your physical folder to see if the is a file .vb.proj

0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
HI guru sammi , it is working now. Thanks this is very valuable
0
 
LVL 9

Author Closing Comment

by:TonyReba
Comment Utility
great stuff
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows server 2008 5 30
ASP.NET Web Form Variables being reset 2 17
getting id from database 5 23
Do we need servers??? 5 102
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Resolve DNS query failed errors for Exchange
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now