Search code examples
c#asp.net-coreasp.net-core-identityblazor

.NET CORE FindByEmailAsync operation was canceled for REST call


I'm trying to build application with blazor and .net core. I have account controller which I'm using for user credentials verification. If I run application in debug from VS (IIS) and send login credentials from my web page then userManger will return correct user. But if I run my application from cmd (dotnet watch run) then userManager.FindByEmailAsync will end with exception:

System.OperationCanceledException: The operation was canceled.

This is call stack from cmd:

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. System.OperationCanceledException: The operation was canceled. at System.Threading.CancellationToken.ThrowOperationCanceledException() at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9.FindByEmailAsync(String normalizedEmail, CancellationToken cancellationToken) at Microsoft.AspNetCore.Identity.UserManager1.FindByEmailAsync(String email) at ProjectManagement.BlazorClient.Server.Controllers.AccountController.Login(LoginRequest request) in C:\Users\lukha\source\repos\ProjectManagement\ProjectManagement.BlazorClient.Server\Controllers\AccountController.cs:line 43 at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at System.Threading.Tasks.ValueTask`1.get_Result() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

This is my controller code:

[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);
    }        
}

Edit: If I run application from cmd and I call this controller from Postman then everything works fine. This is my page code:

<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});
  }
}

Any idea what is wrong with my code?


Solution

  • @lukesss:

    If I run application from cmd and I call this controller from Postman then everything works fine

    You mean that you're Web API works fine, don't you ? Well, there is no reason why it shouldn't.

    Is this a Blazor Component above ? If it is, you must not use the Html form tag. Also you must add the type attribute to the button element with the value "button".

    Now, when you hit the button control the Http.PostJsonAsync will be called, performing an Ajax call (using the Fetch API) to your Web API, instead of making a post back.

    No form tags, no post back in Blazor.

    Hope this helps...