I'm building a Blazor application using the interactive server rendermode and I want to implement a custom login page using ASP.NET Core Identity. For authentication, I'm configuring IdentityCore like this:
builder.Services.AddAuthentication(options =>{ options.DefaultScheme = IdentityConstants.ApplicationScheme; options.DefaultSignInScheme = IdentityConstants.ExternalScheme;}).AddIdentityCookies();builder.Services.AddIdentityCore<ApplicationUser>(options =>{ options.SignIn.RequireConfirmedAccount = false;}).AddRoles<IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddSignInManager().AddDefaultTokenProviders();I’m aware that the recommended approach is to use the default Razor Pages login flow with static render mode, posting directly to the server. However, my goal is to build a interactive server-rendered login page that authenticates the user and sets an authentication cookie.
My Idea so far:
- I try to use the
SignInManager<ApplicationUser>.PasswordSignInAsync(...)method to authenticate the user on the server. This however throws this exceptionHeaders are read-only, response has already started. - After SignIn I was planning to send the value of the cookie to the client using SignalR and then using JsInterlop to set the cookie and continue as normal.
.
Questions:
- Is it possible to create a cookie-based loginpage in Interactive server rendermode?
- Do I have to create my own AuthenticationStateProvider?
- Is there a better way other than falling back to the static loginpage?Any guidance or code examples would be greatly appreciated.