Search code examples
c#asp.netasp.net-mvcweb-configforms-authentication

Receiving a 500 error when configuring an ASP.NET login page


I want to add a login page to an ASP.NET Framework site, but the compiler doesn’t even load the view, instead throwing an error on the configuration.

The web.config contains the following configuration:

<authentication mode="Forms">
    <forms loginUrl="/Login/Login"></forms>
</authentication>

My LoginController contains the following:

public class LoginController : Controller
{
    [HttpGet]
    public ActionResult Login()
    {
        return View();
    }
}

But this is the error I receive:

The configuration section 'authentication' cannot be read because it is missing a section declaration

Here is the error


Solution

  • The <authentication/> element was first introduced in ASP.NET 2.0 as part of Forms-Based Authentication, which appears to be what you’re trying to configure.

    If so, it is expected under the <system.web /> element (source):

    <configuration>
      <system.web>
        <authentication mode="Forms">
          <forms loginUrl="/Login/Login"></forms>
        </authentication>
      </system.web>
    </configuration>
    

    You don’t provide the full context for your web.config file, but according to the screenshot of the error message, it appears as though you’ve inadvertently placed your <authentication /> element as a sibling of the <system.web /> element, not as a child.

    The error message here certainly isn’t intuitive. But placing the <authentication /> element under the system.web element should resolve your issue.

    Note: Since Internet Information Server (IIS) 7, there is also an <authentication /> element located under the <system.webServer /> element, under <security/> (source). This is a used to configure IIS’s authentication, and is independent of the ASP.NET Framework’s Form-Based Authentication.