【问题标题】:ASP.NET MVC5 with existing Membership database具有现有成员资格数据库的 ASP.NET MVC5
【发布时间】:2015-06-10 00:39:46
【问题描述】:

我有一个现有的 ASP.NET 成员数据库,我必须坚持使用它。我正在使用新的 MVC 网站项目模板在 VS2013(更新 4)中开发新的 MVC5 网站。我已修改 web.config 以确保指定旧的成员身份验证类型。我还修改了生成的 [HttpPost]Login 操作,以确保我登录到我的会员数据库 - 它正确登录并根据需要生成身份验证 cookie。

但是,由于我未通过身份验证,该网站仍将我重定向到登录页面。 Request.IsAuthenticated 确实表明我没有通过身份验证。我错过了什么?我有哪些选择? 编辑: web.config 更改(仅更改):

<connectionStrings>
  <add name="MyCS" connectionString="Data Source=sql1;Initial Catalog=MyDB;Persist Security Info=True;User ID=mysa;Password=mypw" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
  <authentication mode="Forms">
    <forms timeout="20" name="seAdmin" loginUrl="~/Account/Login" />
  </authentication>
  <roleManager enabled="true" defaultProvider="CustomizedRoleProvider">
    <providers>
      <clear />
      <add name="CustomizedRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="MyCS" applicationName="/seAdmin" />
    </providers>
  </roleManager>
  <membership defaultProvider="CustomizedMembershipProvider">
    <providers>
      <clear />
      <add name="CustomizedMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MyCS" applicationName="/seAdmin" />
    </providers>
  </membership>
  <machineKey validationKey="63A7C07B191BA3EF02DD4866C420DCAB81C9FFCCC617DE40ED6E2B89A2FC2BA3FA32C39D183FE0708E9279C14E58318D0C5E171C0AF802F154430679D1778485" decryptionKey="F5B9049DECB8C9A23B1D131E63D2ED5C15FF0AEB3C3E96FC" validation="SHA1" />
</system.web> 

登录操作:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    if (Membership.ValidateUser(model.Email, model.Password))
    {
        FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1)
        {
            return Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }
    else
    {
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
    }
    return View(model);
}

FilterConfig 添加全局授权属性

public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());

            filters.Add(new AuthorizeAttribute() { Roles = "CWA" });
        }
    }

【问题讨论】:

  • 您的问题没有足够的信息来获得好的答案,而且我不认为从会员迁移到身份是那么简单,用代码向我们展示您所做的更改并采取看看这个:asp.net/identity/overview/migrations/…
  • 抱歉不够详细。我不会从会员身份迁移到身份。我想做的是使用默认项目模板的修改版本在 MVC 5 中实现成员身份验证。如果这样的修改太复杂,我很乐意接受除了使用 MVC 4 而不是 MVC 5 之外的任何其他想法。我将通过代码更改来更新我的问题。
  • 您是否从移除 FormsAuthentication 模块 IIS 的 web.config 的 Modules 部分中删除了“remove”条目?仅供参考,FormsAuthentication 与成员身份无关。
  • 致埃里克 - 不,我没有。

标签: asp.net asp.net-mvc


【解决方案1】:

我有一些关于它为什么不起作用的理论,首先在web.config中有一个&lt;system.webServer&gt;标签,必须有一行删除FormsAuthentication,尝试将其注释掉,或者重新添加一个,像这样:

<system.webServer>
<modules>
  <remove name="FormsAuthentication" />
  <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
</modules>

其次试试User.Identity.IsAuthenticated 看看也是假的,你在你的应用中有什么角色吗?

【讨论】:

  • 非常感谢 Eric 和 Xellarix,注释掉 &lt;remove name="FormsAuthentication" /&gt; 解决了这个问题,我现在可以成功登录了。
猜你喜欢
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 2012-03-06
相关资源
最近更新 更多