Solved

FormsAuthentication.Authenticate authenticates but doesn't redirect

Posted on 2011-02-12
2
368 Views
Last Modified: 2013-12-17
Hi,
I have an asp.net application in whose pages are free to view to every one however in it I have added a folder called "Demo" that needs protection and a login page.

In my web.config i have:

    <authentication mode="Forms" >
      <forms loginUrl="Admin.aspx" defaultUrl="DemoFolder/Demo.aspx" name=".ASPNETAUTH" protection="None" path="/" timeout="20" >
        <credentials passwordFormat="Clear">
          <user name="joe" password="test" />
        </credentials>
      </forms>
    </authentication>

In the location section I have:

  <location path="Demo">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>

And in my login_click() event I have:

            if (FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text))
            {
                Response.Redirect("~/DemoFolder/Demo.aspx");    
            }
            else
            {
                Msg.Text = "Login failed. Please check your user name and password and try again.";
            }

The Authenticate method definitely works because if I put the wrong passowrkd the Msg.Text will be executed. I have very similar code elsewhere and it works.

Any ideas welcome.
0
Comment
Question by:ilyrian
2 Comments
 
LVL 8

Accepted Solution

by:
Yiogi earned 500 total points
Comment Utility
Everything looks ok to me, except from the fact that you don't set your authentication cookie anywhere. Since you have a set url to redirect to and you don't want to use RedirectFromLoginPage method you should manually set the cookie. So before Response.Redirect please add this line:

        FormsAuthentication.SetAuthCookie(userName, false);

You can replace false with true to persist the authentication cookie for 50 years. RedirectFromLoginPage sets the cookie for you but you are not using it.
0
 

Author Closing Comment

by:ilyrian
Comment Utility
Spot on Yiogi, I know it was something to do with the Authentication cookie but it was not doing it for me. You line of code was all that was missing.
Thanks
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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

14 Experts available now in Live!

Get 1:1 Help Now