加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-5 – ASP.NET MVC 5 Identity 2 PasswordSignInAsyn

发布时间:2020-12-16 09:55:45 所属栏目:asp.Net 来源:网络整理
导读:我正在编写将用于外部应用程序的代码.我正在尝试使用Microsoft Identity 2.0和ASP.NET MVC 5.我已经自定义UserViewModel来保存FirstName和LastName以及其他几个参数.每当我注册用户时,它都会成功登录,但是当我尝试使用电子邮件/密码进行正常登录时. SignInMa
我正在编写将用于外部应用程序的代码.我正在尝试使用Microsoft Identity 2.0和ASP.NET MVC 5.我已经自定义UserViewModel来保存FirstName和LastName以及其他几个参数.每当我注册用户时,它都会成功登录,但是当我尝试使用电子邮件/密码进行正常登录时. SignInManager.PasswordSignAsync始终返回false.由于检查框架内部的所有代码,我试图弄清楚如何调试情况.我将在这里提供一些代码来帮助任何愿意看的人.提前致谢!

这是Microsoft在其示例中提供的通用登录功能.我没有改变任何东西.但这是具有错误消息的函数.

public async Task<ActionResult> Login(LoginViewModel model,string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout,change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email,model.Password,model.RememberMe,shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode",new { ReturnUrl = returnUrl,RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("","Invalid login attempt.");
                return View(model);
        }
    }

这是登录功能

public async Task<ActionResult> Login(LoginViewModel model,"Invalid login attempt.");
                return View(model);
        }
    }

这是自定义的身份模型

public class ApplicationUser : IdentityUser
{
    [DisplayName("Prefix")]
    public string Prefix { get; set; }

    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [DisplayName("Last Name")]
    public string LastName { get; set; }

    [DisplayName("Suffix")]
    public string Suffix { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection",throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<JBIPlacementTracking.Models.PlacementViewModel> PlacementViewModels { get; set; }

    public System.Data.Entity.DbSet<JBIPlacementTracking.Models.Placement> Placements { get; set; }

    public System.Data.Entity.DbSet<JBIPlacementTracking.Models.PacketStatus> PacketStatus { get; set; }
}

我想知道问题可能是我没有自定义LoginViewModel.

我现在肯定在我的头脑中,如果有人拥有一些好的资源,我真的很感激它的链接.

再次感谢!

解决方法

您在登录功能中使用Email作为UserName.确保在注册时,电子邮件将同时插入用户名和电子邮件字段.我也喜欢在插入之前和阅读之后使用Trim()和ToLower(),但这只是为了让我对脑中存储的内容有所了解,而且可能没有必要.祝好运.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读