Simple forms authenticated website does not redire

2019-08-05 17:21发布

问题:

I have a website built on a simple authentication model. It runs fine locally and on local IIS, but it is not redirecting to the default page after typing in the correct credentials, all this in the test server.

The code goes as follows.

Web.config

<compilation targetFramework="4.0" />
        <httpRuntime maxRequestLength="50000" />
        <customErrors mode="On" defaultRedirect="exc/exc.aspx?e=1">
            <error statusCode="404" redirect="exc/404.aspx?e=404"/>
        </customErrors>
        <authentication mode="Forms">
            <forms loginUrl="admin/login.aspx" defaultUrl="admin/main.aspx" />
        </authentication>
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
    <location path="admin">
        <system.web>
            <authorization>
                <deny users="?"/>
            </authorization>
        </system.web>
    </location>
    <location path="admin/css/cms.css">
        <system.web>
            <authorization>
                <allow users="?"/>
            </authorization>
        </system.web>
    </location>
    <location path="admin/css/styles.css">
        <system.web>
            <authorization>
                <allow users="?"/>
            </authorization>
        </system.web>
    </location>
    <location path="admin/images">
        <system.web>
            <authorization>
                <allow users="?"/>
            </authorization>
        </system.web>
    </location>
    <location path="exc/exc.aspx">
        <system.web>
            <authorization>
                <allow users="?"/>
            </authorization>
        </system.web>
    </location>

login.aspx

The form action property is blank and AutoEventWireup is true.

login.aspx.cs

protected void btnAuth_Click1(object sender, EventArgs e)
{
        //Some code not shown...

        //User Validation
            ValidateUserResponse response = new ValidateUserResponse();
            response = service.ValidateUser(request);

            if (response.State == true)
                FormsAuthentication.RedirectFromLoginPage(request.Alias, False);            
}

SignOut

protected void Page_Load(object sender, EventArgs e)
{           
            FormsAuthentication.SignOut();
            Response.Redirect(FormsAuthentication.LoginUrl);            
}

On the test server the URL looks something like this:

http://192.168.1.58/TestSite/admin/login.aspx

On VS2010 works well as well on my local IIS once published, but don't understand why isn't redirecting to the default page after the login when published on the TestServer. Showing that the website requires to log in and a 403 forbidden error.

Locally I have tested it with both URL forms and works (the virtual folder name is different):

http://192.168.1.58/TestSite1/admin/login.aspx

http://localhost/TestSite1/admin/login.aspx

I'd appreciate any suggestions on the matter to make it work.

Thank you.

----------Update-----------

I solved the problem this way:

  1. Right Click on the Virtual Directory containing the WebApp
  2. Go to the Directory Security Tab
  3. Click on Modify
  4. Unchecked Integrated Windows Authentication
  5. Anonymous Access has to be checked as well

This made the WebForms Authentication settings on Web.Config to start having effect on the WebApp.

Thank you.

回答1:

Make sure forms authentication is enabled in IIS on the web server.