MVC5 Where to put authentication forms in web.conf

2019-07-19 19:00发布

Following this title, in my web.config, it's generated by VS 2012.

Now, i don't know where i put the below code in web.config through i saw someone put it in <system.web> but in my web.config it have only <system.web.webPages.razor> and <system.webServer>. When i put this code somewhere in web.config I get an error at <authentication mode="Forms">:

There's code :

<authentication mode="Forms">
    <forms loginurl="~/Comfirm/Login" timeout="2880"></forms>
</authentication>

4条回答
Juvenile、少年°
2楼-- · 2019-07-19 19:40

You are putting it in the wrong web.config file. There are two web.config files. one in Views Folder and one in the root of the site. put it in the system.web tag of the web.config file in the site root

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings  />
  <appSettings >
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="owin:AutomaticAppStartup" value="true" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>      

  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Comfirm/Login" timeout="2880" />
    </authentication>
  </system.web>

  <!--other configuration-->
<configuration>
查看更多
冷血范
3楼-- · 2019-07-19 19:47

If I am not mistaken, VS only makes the tags it will use. Have you tried creating <system.web>? It won't auto generate empty sections to the config.

<system.web>
    <authentication mode="Forms">
        <forms loginurl="~/Comfirm/Login" timeout="2880"></forms>
    </authentication>
</system.web>
查看更多
男人必须洒脱
4楼-- · 2019-07-19 19:52

in MVC 5 the default value is :

<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.7.1" />
<httpRuntime targetFramework="4.7.1" />
<httpModules>
  <add name="ApplicationInsightsWebTracking" 
  type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, 
 Microsoft.AI.Web" />
 </httpModules>
</system.web>

You can notice the Line

  <authentication mode="None" />

Change it to :

 <authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="1440" />
</authentication>

And timeout="1440" its Mean one day

查看更多
放我归山
5楼-- · 2019-07-19 19:56

You need to add <system.web> to the config and put the authentication section within it:

<system.web>
  <authentication mode="Forms">
      <forms loginurl="~/Comfirm/Login" timeout="2880"></forms>
  </authentication>
</system.web>
查看更多
登录 后发表回答