Why can I use UserManager & SignInManager in a Blazor server app

David Thielen 2,796 Reputation points
2023-05-12T08:00:48.5966667+00:00

I am successfully using UserManager & SignInManager in a Blazor server page:

[Inject]
private SignInManager<IdentityUser> SignInManager { get; set; } = default!;

[Inject]
private UserManager<IdentityUser> UserManager { get; set; } = default!;

I'm calling them to create a new user in the Identity DB and they work fine. And then yesterday I read this:

ASP.NET Core abstractions, such as SignInManager and UserManager, aren't supported in Razor components. For more information on using ASP.NET Core Identity with Blazor, see Scaffold ASP.NET Core Identity into a Blazor Server app.

So what's going on here? Is using these something that will fail on me at times? Or is this warning about something else?

thanks - dave

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,499 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 27,686 Reputation points
    2023-05-12T10:42:40.15+00:00

    The SinginManager has many methods that are dependent on an injected HTTP context. These methods may not function as expected in Blazor Server.

    Avoid IHttpContextAccessor/HttpContext in Razor components

    As far as I know, the UserManger does not have dependencies on the HTTP Context and should be okay.

    The source code is on GitHub.

    https://github.com/dotnet/aspnetcore/blob/main/src/Identity/Core/src/SignInManager.cs

    https://github.com/aspnet/AspNetIdentity/blob/main/src/Microsoft.AspNet.Identity.Core/UserManager.cs

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,491 Reputation points
    2023-05-12T21:34:50.6466667+00:00

    A blazor server has an injected HttpContext, but there is no access to the response stream (as it’s completed before the blazor components are loaded. All blazor browser communication is over the signalr connection.

    the identity library is razor page library and assumes it can do redirects or set cookies using context. But the blazor HttpContext is basically read only.

    so the database routines will work, but not any that require a ui or a redirect.