我已經按照下面的帖子 Token Based Authentication in ASP.NET Core的Web API令牌認證拋出異常,無認證處理程序被配置爲處理方案:Microsoft.AspNet.Identity.Application
提到的解決方案來實現令牌認證使用ASP.Net核心Web API爲了實現認證邏輯,我已經定義下面的方法
public async Task<bool> AuthenticateUser(string email, string password)
{
UserManager<ApplicationUser> _userManager = HttpContext.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>)) as UserManager<ApplicationUser>;
SignInManager<ApplicationUser> _signInManager = HttpContext.ApplicationServices.GetService(typeof(SignInManager<ApplicationUser>)) as SignInManager<ApplicationUser>;
var result = await _signInManager.PasswordSignInAsync(email, password, isPersistent: false, lockoutOnFailure: false);
if (result.Succeeded)
{
return true;
}
else
{
return false;
}
}
和Post方法與調用是
[HttpPost]
public dynamic Post([FromBody] AuthRequest req)
{
string email = req.username;
string password = req.password;
try
{
bool isAuthenticated = false;
//implement the authentication logic over here
isAuthenticated = AuthenticateUser(email, password).Result;
if (isAuthenticated)
{
DateTime? expires = DateTime.UtcNow.AddDays(2);
var token = GetToken(req.username, expires);
return new { authenticated = true, entityId = 1, token = token, tokenExpires = expires };
}
}
catch (Exception ex)
{
return new { authenticated = false, message = "Exception: " + ex.Message, detailedmessage = ex.InnerException};
}
return new { authenticated = false };
}
現在的問題......
崗位執行罰款第一次調用和返回所需的結果,但是,在第二個電話,它會引發以下異常
無驗證處理程序被配置爲處理方案:Microsoft.AspNet.Identity.Application
在調試我發現,執行以下線的時候這個異常被拋出
var result = await _signInManager.PasswordSignInAsync(email, password, isPersistent: false, lockoutOnFailure: false);
它在第一次調用時工作正常,但在所有後續調用中拋出異常。
我一直在尋找這個問題的過去2天,我發現的所有是Startup.cs app.UseIdentity();應該在添加認證中間件之前調用。它已經在我的代碼中發生了。
請建議我在這裏錯過什麼。