windows authentication with forms

D

dennisG

Hi,

I have a problem with windows authentication, with a web based form. I
can logon with the function logonuser with my username, password and
domain, but I don't get the windows rights of my windows account. I've
looked to different websites, but it doesn't solved my problem.

My code, function that works on the login button
Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cmdLogin.Click
Dim username As String = txtUsername.Text
Dim password As String = txtPassword.Text
Dim domain As String = "Domainname"

If ValidateLogin(username, password, domain) = True Then
FormsAuthentication.RedirectFromLoginPage(username,
chkRemember.Checked)
Else
lblError.Visible = True
End If
End Sub


And validation function:
Private Function ValidateLogin( _
ByVal Username As String, _
ByVal Password As String, _
ByVal Domain As String) As Boolean

Try
Dim token1 As New IntPtr
Dim loggedOn As Boolean = LogonUser(Username, Domain,
Password, 2, 0, token1)
Dim token2 As New IntPtr
token2 = token1
Dim wi As WindowsIdentity
Dim wp As WindowsPrincipal
wi = New WindowsIdentity(token2)
wp = New WindowsPrincipal(wi)
HttpContext.Current.User = wp
Return True

Catch When Err.Number <> 0
Return False
End Try
End Function


What I'm doing wrong, what I'm missing?

Thanks,
Dennis
 
J

Joe Kaplan \(MVP - ADSI\)

If you want code to execute with the security context of the user, you also
need to impersonate them as well using WindowsImpersonationContext.

Also, remember that the forms auth login here works on the initial request,
but you'll need to find a way to call LogonUser on each subsequent request
as well, presumably by storing their plaintext credentials in an encrypted
cookie or session state or something so you can retrieve them again.

Joe K.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top