Hi,
I'm trying to integrate Forms Authentication using Active Directory in an application. I've created a login page, added the necessary code my global.asax file which i got from Microsofts Site. Most of it is working properly but it seems like there's an issue with either setting the HttpCookie or retrieving the data from the Cookie because once i log in and get authenticated properly instead of getting redirected to the appropriate page, i get redirected back to the login page again. I used the following article to create the forms authentication:
http://support.microsoft.com/kb/326340 I know that the AD part is working fine and that i'm actually getting authenticated but for some reason I cannot go anywhere within my application and i keep getting auto-redirected back to the login page like it isn't detecting my AuthCookie. Here's what i have in my Global.asax file:
Protected Sub Application_AuthenticateRe
quest(ByVa
l sender As Object, ByVal e As System.EventArgs)
' Fires upon attempting to authenticate the user
Dim cookieName As String = FormsAuthentication.FormsC
ookieName
Dim authCookie As HttpCookie = Context.Request.Cookies(co
okieName)
If (authCookie Is Nothing) Then
'There is no authentication cookie.
' MsgBox("There is no authentication cookie!")
Return
End If
Dim authTicket As FormsAuthenticationTicket = Nothing
Try
authTicket = FormsAuthentication.Decryp
t(authCook
ie.Value)
Catch ex As Exception
'Write the exception to the Event Log.
Return
End Try
If (authTicket Is Nothing) Then
'Cookie failed to decrypt.
MsgBox("Couldn't decrypt the cookie!")
Return
End If
'When the ticket was created, the UserData property was assigned a
'pipe-delimited string of group names.
Dim groups As String() = authTicket.UserData.Split(
New Char() {"|"})
'Create an Identity.
Dim id As System.Security.Principal.
GenericIde
ntity = New System.Security.Principal.
GenericIde
ntity(auth
Ticket.Nam
e, "LdapAuthentication")
'This principal flows throughout the request.
Dim principal As System.Security.Principal.
GenericPri
ncipal = New System.Security.Principal.
GenericPri
ncipal(id,
groups)
Context.User = principal
End Sub
so i have another file in my project that i try to surf to when i load the application and the Global.asax file steps in like it should and says i'm not authenticated so i get redirected to the login page (which is what i want), however when i log in, i want to get redirected to the appropriate page like the following is supposed to do for me:
'You can redirect now.
Response.Redirect(FormsAut
henticatio
n.GetRedir
ectUrl(txt
Username.T
ext, False))
Any ideas? Here's the code in my logon.aspx page too in case you want to see that. I copied straight from the microsoft site:
<%@ Page language="vb" AutoEventWireup="true" %>
<%@ Import Namespace="FormsAuthAd.For
msAuth" %>
<html>
<body>
<form id="Login" method="post" runat="server">
<asp:Label ID="Label1" Runat="server">Domain:</as
p:Label>
<asp:TextBox ID="txtDomain" Runat="server"></asp:TextB
ox><br>
<asp:Label ID="Label2" Runat="server">Username:</
asp:Label>
<asp:TextBox ID="txtUsername" Runat="server"></asp:TextB
ox><br>
<asp:Label ID="Label3" Runat="server">Password:</
asp:Label>
<asp:TextBox ID="txtPassword" Runat="server" TextMode="Password"></asp:
TextBox><b
r>
<asp:Button ID="btnLogin" Runat="server" Text="Login" OnClick="Login_Click"></as
p:Button><
br>
<asp:Label ID="errorLabel" Runat="server" ForeColor="#ff3300"></asp:
Label><br>
<asp:CheckBox ID="chkPersist" Runat="server" Text="Persist Cookie" />
</form>
</body>
</html>
<script runat="server">
Sub Login_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim adPath As String = "LDAP://fortisproperties.c
om/OU=Cana
da,DC=fort
isproperti
es,DC=com"
'Path to your LDAP directory server
Dim adAuth As FormsAuth.LdapAuthenticati
on = New FormsAuth.LdapAuthenticati
on(adPath)
Try
If (True = adAuth.IsAuthenticated(txt
Domain.Tex
t, txtUsername.Text, txtPassword.Text)) Then
Dim groups As String = adAuth.GetGroups()
'Create the ticket, and add the groups.
Dim isCookiePersistent As Boolean = chkPersist.Checked
Dim authTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(
1, _
txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(60
), isCookiePersistent, groups)
'Encrypt the ticket.
Dim encryptedTicket As String = FormsAuthentication.Encryp
t(authTick
et)
'Create a cookie, and then add the encrypted ticket to the cookie as data.
Dim authCookie As HttpCookie = New HttpCookie(FormsAuthentica
tion.Forms
CookieName
, encryptedTicket)
If (isCookiePersistent = True) Then
authCookie.Expires = authTicket.Expiration
End If
'Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authC
ookie)
'You can redirect now.
Response.Redirect(FormsAut
henticatio
n.GetRedir
ectUrl(txt
Username.T
ext, False))
Else
errorLabel.Text = "Authentication did not succeed. Check user name and password."
End If
Catch ex As Exception
errorLabel.Text = "Error authenticating. " & ex.Message
End Try
End Sub
</script>
Any help would be greatly appreciated.
Thanks
Start Free Trial