gpt4 book ai didi

forms-authentication - 升级到 ASP.NET 4.5/MVC 4 表单例份验证失败

转载 作者:行者123 更新时间:2023-12-03 15:11:56 25 4
gpt4 key购买 nike

我刚刚下载了 VS 2012 以及 ASP.NET 4.5 和 MVC 4.0,并且正在使用示例应用程序进行测试,发现与 ASP.NET 4.0/MVC 3 完美配合的表单例份验证似乎不再适用于最新发布。

当我在操作 Controller 中调用 Login 函数时,WebSecurity.Login 调用失败:

public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}

// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}

我已经用我的 VS 2010 源代码中的等效代码替换了这段代码,但这也失败了(使用现已弃用的 FormsAuthentication.Authenticate 函数)。

我的问题是:是否有人将 MVC3 移植到 MVC4 应用程序并找到了解决此问题的方法?我正在使用 IIS Express,所以我想这可能会导致一些问题,但如果您有任何想法,我将不胜感激。

我从我的工作 asp.net 4/MVC3 应用程序中复制了我的配置,如下所示,但没有运气(这是相关部分):
  <connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=tcp:sql2k1201.dbprovider.net;Initial Catalog=SQL2012_db;User ID=SQL2012_db_user;Password=dbpassword;" providerName="System.Data.SqlClient" />
</connectionStrings>

<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />

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

<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="DefaultConnection"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>

<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>

<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</roleManager>

最佳答案

如果您的表单例份验证票需要在使用旧版 .NET 框架的应用程序之间共享,您必须明确配置您的 .NET 4.5 应用程序以使用早期的机器兼容模式,否则它们将无法加密/解密表单例份验证票。

在 .net 4.5 应用程序的 web.config 中,设置兼容模式属性:

<system.web>
<machineKey compatibilityMode="Framework20SP2" />
</system.web>

这将允许您的 .NET 4.5 应用程序使用由早期 .NET 版本生成的表单例份验证票证。

注意:如果您的任何服务器没有安装 .NET Framework 2.0 SP2,您需要将兼容模式设置为“Framework20SP1”。

MSDN - MachineKeySection.CompatibilityMode Property

关于forms-authentication - 升级到 ASP.NET 4.5/MVC 4 表单例份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12021863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com