配置 ASP.NET 登录页面时收到 500 错误

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

我想将登录页面添加到 ASP.NET 框架站点,但编译器甚至不加载视图,而是在配置上抛出错误。

web.config包含以下配置:

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

我的 LoginController 包含以下内容:

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

但这是我收到的错误:

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

<authentication/> 元素作为 Forms-Based Authentication 的一部分首次在 ASP.NET 2.0 中引入,这似乎是您要配置的内容。

如果是的话,预计在<system.web />元素下(source):

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

您没有提供 web.config 文件的完整上下文,但根据错误消息的屏幕截图,您似乎无意中将 <authentication /> 元素作为 元素的 <system.web /> 元素,而不是 child.

这里的错误信息肯定不直观。但是将 <authentication /> 元素放在 system.web 元素下应该可以解决您的问题。

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.