View Full Version : Authenticating NT userid and password against Active Directory in dot net


Bindu
I try the following code in asp.net to authenticate nt
userid and password against active directory. I am using
System.Directoryservices component. The problem is that
soemtimes this authenticates, sometimes this doesn't. I
get the following error sometimes eventhough I put my
correct userid and password. Please help.
***********************************************************
The credentials supplied conflict with an existing set of
credentials.
***********************************************************


Code
*********************************************************
Imports System.DirectoryServices

Public Class _default
Inherits System.Web.UI.Page


Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Try
'Put user code to initialize the page here
Dim arrId As Array
arrId = Split(Request.ServerVariables
("LOGON_USER"), "\")
txtUserName.Text = arrId(1)
Catch ex As Exception
lblMessage.Text = ex.Message
End Try

End Sub


Public Function AuthenticateUser(ByVal strdomain As
String, ByVal strusername As String, ByVal strpwd As
String) As Boolean

Dim dirEntry As DirectoryEntry
Dim strNameEntry As String

' Try to get the Name property from the AD. If you
can do this with the(current)
' User ID and Password, then the credentials must
be OK.
Try
Dim strdomainAndUsername As String = strdomain
& "\" & strusername
Dim strpath As String = "WinNT://" & strdomain
dirEntry = New DirectoryEntry(strpath,
strdomainAndUsername, strpwd)
strNameEntry = dirEntry.Properties
("Name").ToString
Catch ex As Exception
lblMessage.Text = ex.Message
Return False
End Try

Return True

End Function


Private Sub btnLogin_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnLogin.Click

Try
Dim arrId As Array
arrId = Split(Request.ServerVariables
("LOGON_USER"), "\")

Dim isNTAuth As Boolean
'isNTAuth = AuthenticateUser(arrId(1),
txtPassword.Text, arrId(0))
'isNTAuth = IsAuthenticated(arrId(0), arrId
(1), txtPassword.Text)
isNTAuth = AuthenticateUser(arrId(0), arrId
(1), txtPassword.Text)
If isNTAuth Then
Session.Add("ValidUser", True)
Response.Redirect
("Prepare_Approve_Email/prepare_approve_email.aspx")
Else
Session.Add("ValidUser", False)
lblMessage.Text = lblMessage.Text
& "
The userid and password do not match, please retry.
Thank you"
End If
Catch ex As Exception
lblMessage.Text = ex.Message
End Try

End Sub

End Class
*********************************************************

Brian Desmond [MVP]
Is the ASP.Net worker process impersonating a domain user? That's what the
error implies. You'll either need to run the worker process with a local
account, or give its service account rights to the AD, and bind without
specifying credentials explicitly.

--
--
Brian Desmond
Windows Server MVP
desmondb@payton.cps.k12.il.us

Http://www.briandesmond.com


"Bindu" wrote in message
news:119b701c3f56c$36f2c9e0$a001280a@phx.gbl...
> I try the following code in asp.net to authenticate nt
> userid and password against active directory. I am using
> System.Directoryservices component. The problem is that
> soemtimes this authenticates, sometimes this doesn't. I
> get the following error sometimes eventhough I put my
> correct userid and password. Please help.
> ***********************************************************
> The credentials supplied conflict with an existing set of
> credentials.
> ***********************************************************
>
>
> Code
> *********************************************************
> Imports System.DirectoryServices
>
> Public Class _default
> Inherits System.Web.UI.Page
>
>
> Private Sub Page_Load(ByVal sender As System.Object,
> ByVal e As System.EventArgs) Handles MyBase.Load
> Try
> 'Put user code to initialize the page here
> Dim arrId As Array
> arrId = Split(Request.ServerVariables
> ("LOGON_USER"), "\")
> txtUserName.Text = arrId(1)
> Catch ex As Exception
> lblMessage.Text = ex.Message
> End Try
>
> End Sub
>
>
> Public Function AuthenticateUser(ByVal strdomain As
> String, ByVal strusername As String, ByVal strpwd As
> String) As Boolean
>
> Dim dirEntry As DirectoryEntry
> Dim strNameEntry As String
>
> ' Try to get the Name property from the AD. If you
> can do this with the(current)
> ' User ID and Password, then the credentials must
> be OK.
> Try
> Dim strdomainAndUsername As String = strdomain
> & "\" & strusername
> Dim strpath As String = "WinNT://" & strdomain
> dirEntry = New DirectoryEntry(strpath,
> strdomainAndUsername, strpwd)
> strNameEntry = dirEntry.Properties
> ("Name").ToString
> Catch ex As Exception
> lblMessage.Text = ex.Message
> Return False
> End Try
>
> Return True
>
> End Function
>
>
> Private Sub btnLogin_Click(ByVal sender As
> System.Object, ByVal e As System.EventArgs) Handles
> btnLogin.Click
>
> Try
> Dim arrId As Array
> arrId = Split(Request.ServerVariables
> ("LOGON_USER"), "\")
>
> Dim isNTAuth As Boolean
> 'isNTAuth = AuthenticateUser(arrId(1),
> txtPassword.Text, arrId(0))
> 'isNTAuth = IsAuthenticated(arrId(0), arrId
> (1), txtPassword.Text)
> isNTAuth = AuthenticateUser(arrId(0), arrId
> (1), txtPassword.Text)
> If isNTAuth Then
> Session.Add("ValidUser", True)
> Response.Redirect
> ("Prepare_Approve_Email/prepare_approve_email.aspx")
> Else
> Session.Add("ValidUser", False)
> lblMessage.Text = lblMessage.Text
> & "
The userid and password do not match, please retry.
> Thank you"
> End If
> Catch ex As Exception
> lblMessage.Text = ex.Message
> End Try
>
> End Sub
>
> End Class
> *********************************************************