嘗試使用郵遞員登錄用戶。我已經使用OAuth2令牌驗證middelware實施了OpenIdDict身份驗證解決方案。然而,當我嘗試獲得訪問,我得到了以下錯誤消息:「用戶名/密碼對無效」
用戶名/密碼夫婦無效
我有以下的測試用戶。
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var db = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
var user = new User();
var hasher = new PasswordHasher<User>();
db.AddRange(
new ApplicationUser { FirstName = "Thomas", LastName = "Smith", UserName = "[email protected]",
Email = "[email protected]", PasswordHash = hasher.HashPassword(user, "Password1") },
);
db.SaveChanges();
}
當我在authorizationController調試,我的用戶變量原來是空,即使輸入變量是正確的。
if (request.IsPasswordGrantType())
{
var user = await _userManager.FindByNameAsync(request.Username);
if (user == null){
return BadRequest(new OpenIdConnectResponse {
Error = OpenIdConnectConstants.Errors.InvalidGrant,
ErrorDescription = "The username/password couple is invalid."
});
}
是因爲你從來沒有真正將自己的用法寫入數據庫嗎?看起來像你缺少一個db.SaveChanges(); ? – Kris
對不起,忘記在我的代碼示例中包含它。代碼已更新。還檢查了表格,並更新了表格。 – ebk