我正在编写将用于外部应用程序的代码。我正在尝试使用Microsoft Identity 2.0和ASP.NET MVC5。我自定义了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, 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 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。
我现在肯定会感到头疼,如果有人有一些好的资源,我将非常感谢与它的链接。
再次感谢您!
您使用电子邮件作为用户名,则需要在AspNetUsers表中将EmailConfirmed字段设置为“ true” 。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句