我正在尝试使用 blazor 和 .net 核心构建应用程序。我有用于用户凭据验证的帐户控制器。如果我从 VS (IIS) 在调试中运行应用程序并从我的网页发送登录凭据,那么 userManger 将返回正确的用户。但是,如果我从 cmd (dotnet watch run) 运行我的应用程序,那么 userManager.FindByEmailAsync 将以异常结束:
System.OperationCanceledException:操作已取消。
这是来自 cmd 的调用堆栈:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常。System.OperationCanceledException:操作已取消。在 System.Threading.CancellationToken.ThrowOperationCanceledException() 在 Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
9.FindByEmailAsync(String normalizedEmail, CancellationToken cancellationToken) at Microsoft.AspNetCore.Identity.UserManager
1.C:\Users\lukha\source\repos\ProjectManagement\ProjectManagement.BlazorClient.Server\Controllers\AccountController.cs:line 中 ProjectManagement.BlazorClient.Server.Controllers.AccountController.Login(LoginRequest 请求)的 FindByEmailAsync(字符串电子邮件) 43 在 Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper 映射器,ObjectMethodExecutor 执行器,对象控制器,Object[] 参数) 在 System.Threading.Tasks.ValueTask`1.get_Result() 在 Microsoft.AspNetCore.Mvc .Internal.ControllerActionInvoker.InvokeActionMethodAsync() 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) 在 Microsoft.AspNetCore.Mvc.Internal。ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() 在 Microsoft.AspNetCore.Mvc .Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() 在 Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware。调用(HttpContext 上下文)
这是我的控制器代码:
[Route("api/[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager;
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
ILoggerFactory loggerFactory
)
{
_signInManager = signInManager;
_userManager = userManager;
}
[HttpPost("[action]")]
[AllowAnonymous]
public async Task<IActionResult> Login([FromBody]LoginRequest request)
{
var user = await _userManager.FindByEmailAsync(request.UserName);
}
}
编辑:如果我从 cmd 运行应用程序并从 Postman 调用这个控制器,那么一切正常。这是我的页面代码:
<form>
<div>
<label>Email: </label>
<input required name="email" bind="@email" />
</div>
<div>
<label>Password: </label>
<input required type="password" name="password" bind="@password" />
</div>
<button onclick=@(() => ButtonLogin_click())>Login</button>
</form>
@functions{
string email;
string password;
protected async Task ButtonLogin_click()
{
var result = await Http.PostJsonAsync<object>("api/account/Login", new LoginRequest { UserEmail = email, Password = password});
}
}
知道我的代码有什么问题吗?