简体   繁体   English

尽管使用UserManager扩展方法,FindAsync仍返回null

[英]FindAsync returns null despite UserManager extension method

I'm currently working from this excellent guide for the purpose of migrating an existing web app that uses SQL Membership authentication to ASP.NET Identity authentication: 我目前正在使用这个出色的指南,目的是将使用SQL成员身份验证的现有Web应用程序迁移到ASP.NET身份验证:

http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity

With the difference being that I am not using a Web Forms project, so I've adapted it to work with an MVC 5 project. 不同之处在于我没有使用Web窗体项目,因此我将其用于MVC 5项目。 Here's my UserManager class, which is supposed to check for SQL Membership passwords and update them when necessary (see link above for details): 这是我的UserManager类,它应该检查SQL成员资格密码并在必要时更新它们(有关详细信息,请参阅上面的链接):

public class MyUserManager : UserManager<ApplicationUser>
{
    public MyUserManager()
        : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
    {
        this.PasswordHasher = new SQLPasswordHasher();
    }
    public class SQLPasswordHasher : PasswordHasher
    {
        public override string HashPassword(string password)
        {
            return base.HashPassword(password);
        }

        public override PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword)
        {
            string[] passwordProperties = hashedPassword.Split('|');
[SNIP]

The problem is that the /Account/Login method does not seem to use the PasswordHasher extension. 问题是 / Account / Login方法似乎没有使用PasswordHasher扩展。 Here's the code (straight from the MVC 5 template, though the UserManager object is an instance of the MyUserManager class above): 这是代码(直接来自MVC 5模板,尽管UserManager对象是上面MyUserManager类的一个实例):

    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.UserName, model.Password);
            if (user != null)
            {
                await SignInAsync(user, model.RememberMe);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
[SNIP]

It won't authenticate. 它不会进行身份验证。 I imagine I'm not the only one trying to make this work. 我想我不是唯一一个试图做这项工作的人。 Any pointers? 有什么指针吗? For MVC 5, do I need to write a different extension? 对于MVC 5,我是否需要编写不同的扩展名?

Thanks for any help you can provide. 感谢您的任何帮助,您可以提供。

Here is the way this should be set up for a new MVC 5 project: 以下是为新的MVC 5项目设置的方法:

IdentityModels.cs IdentityModels.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("ApplicationServices")
    {
    }
}

public class MyUserManager : UserManager<ApplicationUser>
{
    public MyUserManager()
        : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
    {
        this.PasswordHasher = new SQLPasswordHasher();
    }
    public class SQLPasswordHasher : PasswordHasher
[etc.]

The SQLPasswordHasher code can be found here . 可以在此处找到SQLPasswordHasher代码。

AccountController.cs AccountController.cs

public class AccountController : Controller
{
    public AccountController()
        : this(new MyUserManager())
    {
    }

    public AccountController(MyUserManager userManager)
    {
        UserManager = userManager;
    }

    public MyUserManager UserManager { get; private set; }

[etc.]

The rest is exactly as it appears on the blog mentioned above and/or as it appears in the default MVC 5 template. 其余部分与上面提到的博客和/或默认MVC 5模板中显示的完全相同。 Once hooked up to a context with SQL Membership info, it works like a charm. 一旦连接到具有SQL成员资格信息的上下文,它就像一个魅力。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM