升级到ASP.NET 4.5 / MVC 4窗体身份验证失败 [英] Upgrading to ASP.NET 4.5/MVC 4 forms authentication fails

查看:134
本文介绍了升级到ASP.NET 4.5 / MVC 4窗体身份验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚downoaded一个VS 2012使用ASP.NET 4.5和MVC 4.0一起,并用一个示例应用程序初涉发现窗体身份验证与ASP.NET工程完美4.0 / MVC 3似乎不再在最新发布的工作。

当我做出的动作控制器登录函数调用时,WebSecurity.Login调用失败:

 公众的ActionResult登录(LoginModel型号,串RETURNURL)
{
    如果(ModelState.IsValid&安培;&安培; WebSecurity.Login(model.UserName,model.Password,persistCookie:model.RememberMe))
    {
        返回RedirectToLocal(RETURNURL);
    }    //如果我们走到这一步,事情失败了,重新显示形式
    ModelState.AddModelError(,提供的用户名或密码不正确。);
    返回查看(模型);
}

我把它换成这个code,在我的VS 2010源等同,这也失败(使用德现在precated FormsAuthentication.Authenticate功能)。

我的问题是:有没有人移植一个MVC3到MVC4应用程序,并找到了解决方法这个问题?我使用的是IIS防爆preSS,所以我想这可能在某种程度上造成了一些问题,但如果你有什么想法,我想AP preciate它。

我复制我的配置从我的工作asp.net 4 / MVC3应用程序如下,但没有运气(这里的相关部分):

 <&是connectionStrings GT;
    <添加名称=DefaultConnection的connectionString =数据源= TCP:sql2k1201.dbprovider.net;初始目录= SQL2012_db;用户ID = SQL2012_db_user;密码= DBPASSWORD;的providerName =System.Data.SqlClient的/>
  < /&是connectionStrings GT;  <&的System.Web GT;
    <编译调试=真targetFramework =4.5/>
    <的httpRuntime targetFramework =4.5/>    <身份验证模式=表格>
      <形式loginUrl =〜/帐号/登录超时=2880/>
    < /认证>    <会员和GT;
      <供应商>
        <清/>
        <添加名称=AspNetSqlMembershipProviderTYPE =System.Web.Security.SqlMembershipProvider的connectionStringName =DefaultConnection
           enablePasswordRetrieval =假enablePasswordReset设置=真requiresQuestionAndAnswer =假requiresUniqueEmail =假
           maxInvalidPasswordAttempts =5minRequiredPasswordLength =6minRequiredNonalphanumericCharacters =0passwordAttemptWindow =10
           的applicationName =//>
      < /供应商>
    < /会员>    <型材>
      <供应商>
        <清/>
        <添加名称=AspNetSqlProfileProviderTYPE =System.Web.Profile.SqlProfileProvider的connectionStringName =DefaultConnection的applicationName =//>
      < /供应商>
    < / profile文件>    < roleManager启用=真正的>
      <供应商>
        <清/>
        <添加名称=AspNetSqlRoleProviderTYPE =System.Web.Security.SqlRoleProvider的connectionStringName =DefaultConnection的applicationName =//>
      < /供应商>
    < / roleManager>


解决方案

这里的问题是,默认的mvc4互联网模板使用SimpleMembership管理会员/角色的信息。在模板中code的的这个假定并只能与simplemembership工作。当您安装通用的供应商账户控制器code鼓起,因为它无法理解通用的供应商。看看这个帖子里面进一步解释在这种情况下<一个href=\"http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx\" rel=\"nofollow\">http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx

I've just downoaded a VS 2012 along with ASP.NET 4.5 and MVC 4.0 and was kicking the tires with a sample app and found that the forms authentication that works perfectly with ASP.NET 4.0/MVC 3 no longer seems to work with the latest release.

When I make a call to the Login function in the action controller, the WebSecurity.Login call fails:

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);
}

I've replaced this code with the equivalent in my VS 2010 source, and that also fails (using the now deprecated FormsAuthentication.Authenticate function).

My question is: Has anybody ported a MVC3 to MVC4 app and found a workaround to this issue? I'm using IIS Express, so I guess that may be causing some problem somehow, but if you have any ideas, I'd appreciate it.

I copied my configuration from my working asp.net 4/MVC3 app as follows, but no luck (here's the relevant parts):

  <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>

解决方案

The issue here is that the default mvc4 internet template is using SimpleMembership to manage membership/roles information. The code in the template has assumption of this and can only work with simplemembership. When you install universal providers the account controller code blows up since it cannot understand universal providers. Look at this post which explains further on this scenario http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx

这篇关于升级到ASP.NET 4.5 / MVC 4窗体身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆