C# &引用;用户名/密码对无效";
正在尝试使用postman登录用户。我已经使用OAuth2令牌验证中间件实现了OpenIdDict身份验证解决方案。但是,当我尝试访问时,我收到以下错误消息: 用户名/密码对无效 我有以下测试用户C# &引用;用户名/密码对无效";,c#,asp.net,oauth,C#,Asp.net,Oauth,正在尝试使用postman登录用户。我已经使用OAuth2令牌验证中间件实现了OpenIdDict身份验证解决方案。但是,当我尝试访问时,我收到以下错误消息: 用户名/密码对无效 我有以下测试用户 using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope()) { var db = serviceScope.ServicePr
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 = "user@test.com",
Email = "user@test.com", PasswordHash = hasher.HashPassword(user, "Password1") },
);
db.SaveChanges();
}
您的代码是否采用异步方法?我猜它并没有阻塞等待,而是在用户查找返回之前测试user==null。尝试在其中添加一个延迟,看看它是否会改变它。是因为您从未真正将您的使用写入数据库吗?似乎缺少一个db.SaveChanges()?很抱歉,我的代码示例中忘记了包含这一点。代码已更新。我还检查了表,它更新了表。这很可能是问题所在,但我如何使用延迟?您的代码使用什么方法?尝试向方法签名添加async,即私有async BadRequest login(){…}
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."
});
}