ASP.NET MVC 4中的FormsAuthentication不起作用

时间:2014-09-01 07:40:09

标签: c# asp.net asp.net-mvc

我的来源:

的web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-LogInSystem-20140901072022.mdf;Initial Catalog=aspnet-LogInSystem-20140901072022;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="MainDbContext" connectionString="metadata=res://*/MainDbModel.csdl|res://*/MainDbModel.ssdl|res://*/MainDbModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\MainDb.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />

  </appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

登录方式:

[HttpPost]
public ActionResult LogIn(UserModel user)
{
     if (ModelState.IsValid)
     {
         if (IsValid(user.Email, user.Password))
         {
             System.Web.Security.FormsAuthentication.SetAuthCookie(user.Email, false);
             return RedirectToAction("Index", "Home");
         }
         else
         {
              ModelState.AddModelError("", "Login data is incorrect.");
         }
     }
     return View(user);
}

代码

@if (Request.IsAuthenticated)
{
     <strong>@Html.Encode(User.Identity.Name)</strong>
     @Html.ActionLink("Log Out", "Logout", "User")
 }
 else
 {
     @Html.ActionLink("Register", "Register", "User")
     <span> | </span>
     @Html.ActionLink("Log in", "Login", "User")
 }

@Edit 的登录:

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true, "Login failed. Check your login details")
    <div>
        <fieldset>
            <legend>Login form</legend>
            <div>@Html.LabelFor(u => u.Email)</div>
            <div>
            @Html.TextBoxFor(u=>u.Email)
            @Html.ValidationMessageFor(u=>u.Email)
            </div>

            <div>@Html.LabelFor(u => u.Password)</div>
            <div>
                @Html.PasswordFor(u => u.Password)
                @Html.ValidationMessageFor(u => u.Password)
            </div>

            <input type="submit" value="Log In"/>

        </fieldset>
    </div>
}

然后我点击按钮logIn(这个代码中没有显示)然后我去: public ActionResult LogIn(UserModel user)方法。然后我有vaild等...
下一步:System.Web.Security.FormsAuthentication.SetAuthCookie(user.Email, false); 但是当页面刷新时,我看不到我的user.Email,但是所有时间都看到Register | Log In,但必须是f as2@email.com | Log Out。它类似于:Request.IsAuthenticated == false

我该如何解决?

2 个答案:

答案 0 :(得分:1)

替换

<authentication mode="None" />

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

(调整timeout属性的值以满足您的需求)

答案 1 :(得分:0)

您似乎删除了web.config中的FormsAuthenticationModule:

<system.webServer>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
</system.webServer>

我对MVC中的所有内容都不熟悉,但是如果你没有其他任何东西可以处理身份验证我就不会删除FormsAuthenticationModule