【发布时间】:2012-08-18 20:03:45
【问题描述】:
我刚刚下载了一个 VS 2012 以及 ASP.NET 4.5 和 MVC 4.0,并正在使用示例应用程序踢轮胎,发现与 ASP.NET 4.0/MVC 3 完美配合的表单身份验证似乎不再适用使用最新版本。
当我在动作控制器中调用 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>
【问题讨论】:
标签: forms-authentication asp.net-mvc-4 iis-express asp.net-4.5