MVC5 Where to put authentication forms in web.conf

2019-07-19 19:03发布

问题:

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>

回答1:

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>


回答2:

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>


回答3:

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:

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