Advertisement

08.30.2007 at 08:12AM PDT, ID: 22797502
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.4

ASP.NET - VB.NET, IE 7 - Forms Authentication (with Active Directory) Issue

Asked by JasonBarrett in Active Server Pages (ASP), Programming for ASP.NET, .NET Framework 2.x

Tags: ,

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_AuthenticateRequest(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Fires upon attempting to authenticate the user
       
        Dim cookieName As String = FormsAuthentication.FormsCookieName
        Dim authCookie As HttpCookie = Context.Request.Cookies(cookieName)
       
        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.Decrypt(authCookie.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.GenericIdentity = New System.Security.Principal.GenericIdentity(authTicket.Name, "LdapAuthentication")

        'This principal flows throughout the request.
        Dim principal As System.Security.Principal.GenericPrincipal = New System.Security.Principal.GenericPrincipal(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(FormsAuthentication.GetRedirectUrl(txtUsername.Text, 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.FormsAuth" %>


<html>
      <body>
            <form id="Login" method="post" runat="server">
                  <asp:Label ID="Label1" Runat="server">Domain:</asp:Label>
                  <asp:TextBox ID="txtDomain" Runat="server"></asp:TextBox><br>
                  <asp:Label ID="Label2" Runat="server">Username:</asp:Label>
                  <asp:TextBox ID="txtUsername" Runat="server"></asp:TextBox><br>
                  <asp:Label ID="Label3" Runat="server">Password:</asp:Label>
                  <asp:TextBox ID="txtPassword" Runat="server" TextMode="Password"></asp:TextBox><br>
                  <asp:Button ID="btnLogin" Runat="server" Text="Login" OnClick="Login_Click"></asp: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.com/OU=Canada,DC=fortisproperties,DC=com" 'Path to your LDAP directory server
        Dim adAuth As FormsAuth.LdapAuthentication = New FormsAuth.LdapAuthentication(adPath)
        Try
            If (True = adAuth.IsAuthenticated(txtDomain.Text, 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.Encrypt(authTicket)
               
                'Create a cookie, and then add the encrypted ticket to the cookie as data.
                Dim authCookie As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
                               
                If (isCookiePersistent = True) Then
                    authCookie.Expires = authTicket.Expiration
                End If
                'Add the cookie to the outgoing cookies collection.
                Response.Cookies.Add(authCookie)

                'You can redirect now.
                Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, 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
[+][-]08.30.2007 at 08:15AM PDT, ID: 19800896

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.30.2007 at 08:52AM PDT, ID: 19801330

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Active Server Pages (ASP), Programming for ASP.NET, .NET Framework 2.x
Tags: ASP.NET - VB.NET, IE 7
Sign Up Now!
Solution Provided By: ICINTRACOM
Participating Experts: 1
Solution Grade: B
 
 
[+][-]08.30.2007 at 10:47AM PDT, ID: 19802443

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.30.2007 at 10:54AM PDT, ID: 19802505

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.30.2007 at 11:00AM PDT, ID: 19802555

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.30.2007 at 11:03AM PDT, ID: 19802575

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.30.2007 at 11:07AM PDT, ID: 19802608

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.30.2007 at 11:08AM PDT, ID: 19802618

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.30.2007 at 11:12AM PDT, ID: 19802644

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628