【问题标题】:MVC Core 2.0 and ASP.NET Forms Based Authentication基于 MVC Core 2.0 和 ASP.NET 表单的身份验证
【发布时间】:2020-02-21 05:30:33
【问题描述】:

我的网站是通过桌面 (Silverlight) 应用程序管理数据的在线门户。此应用程序使用基于 ASP.NET 表单的身份验证来登录用户。

我的网站的 MVC Core 1.1 版本使用来自 System.Web.Security 命名空间的 MembershipProvider.ValidateUser 来执行此操作。由于我无法将System.Web.dll 添加到我的 ASP.NET MVC 2.0 项目中,我该如何执行此验证?

如果我在添加System.Web.dll 时有误,请告知我,但我已经调查过了,找不到解决方案。

我的 MVC Core 1.1 控制器中的登录方法:

[HttpPost]
[AllowAnonymous]
public IActionResult Login(LoginViewModel vmLogin)
{
    string loginName = $"{vmLogin.CompanyCode}\\{vmLogin.UserName}";

    MembershipProvider provider = Membership.Provider;
    if (provider.ValidateUser(loginName, vmLogin.Password))
    {
        Claim[] claims = { new Claim(ClaimTypes.Name, loginName) };
        ClaimsIdentity identity = new ClaimsIdentity(claims, "Custom");
        ClaimsPrincipal principal = new ClaimsPrincipal(identity);
        HttpContext.Authentication.SignInAsync("FundraiserCookieMiddlewareInstance", principal);

        return RedirectToAction("Index");
    }
    else
    {
        vmLogin.Password = string.Empty;
        vmLogin.Error = "Invalid Credentials";
        return View(vmLogin);
    }
}

从用于登录的数据库创建的模型:

【问题讨论】:

  • 你说得对,System.Web 程序集在 ASP.NET Core 中不可用。它仅适用于完整的 .NET Framework 版本。

标签: c# asp.net-core asp.net-core-2.0


【解决方案1】:

ASP.NET Core 2.0 有一个新的标识模型,它不向后兼容 System.Web.Security 命名空间。新的身份提供者模型是here in GitHub,它包含一个与您的代码类似的 ASP.NET MVC 示例。以下是来自their AccountController sample codeLogin方法的sn-p

// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;
    if (ModelState.IsValid)
    {
        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, set lockoutOnFailure: true
        var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
        if (result.Succeeded)
        {
            _logger.LogInformation(1, "User logged in.");
            return RedirectToLocal(returnUrl);
        }
        if (result.RequiresTwoFactor)
        {
            return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
        }
        if (result.IsLockedOut)
        {
            _logger.LogWarning(2, "User account locked out.");
            return View("Lockout");
        }
        else
        {
            ModelState.AddModelError(string.Empty, "Invalid login attempt.");
            return View(model);
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

【讨论】:

  • 这个新的身份提供者是否与 ASP.NET 基于表单的身份验证中的模型兼容(问题中现在包含来自数据库的模型)?
  • 据我所知,没有支持原始 ASP.NET Membership 提供程序的现有提供程序。您可以查看Community Maintained Store Providers 中的不同实现,了解如何适应/迁移到新模型。
  • repo 已移动:github.com/dotnet/aspnetcore/tree/master/src/Identity 请参阅“samples”文件夹以获取 (ek)samples
猜你喜欢
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 2017-05-19
相关资源
最近更新 更多