我正在编写将用于外部应用程序的代码。我正在尝试使用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, 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。
我现在肯定在我的头脑中,如果有人拥有一些很好的资源,我真的很感激它的链接。
再次提前感谢!
答案 0 :(得分:3)
您在登录功能中使用Email
作为UserName
。确保在注册时,电子邮件会同时插入UserName
和Email
字段。我也喜欢在插入之前和之后使用Trim()
和ToLower()
,但这只是为了让我对脑中存储的内容有所了解,而且可能没有必要。祝你好运。
答案 1 :(得分:1)
您将电子邮件用作用户名,则需要在 AspNetUsers 表中将 EmailConfirmed 字段设置为“ true”