You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an ASP .NET Core 9 Razor Pages web app which uses Identity. I've created it from a template, attached my custom DbContext and scaffolded Identity/Pages/Account/Login page. And I've tried to add Web API to the same project to be able to have other consumers (iOS/Android applications, PHP/Go clients, etc.). With minimal API syntax I've implemented LoginUser:
app.MapGet("/api/LoginUser", async (string username, string password, SignInManager<IdentityUser> signInManager, IHttpContextAccessor httpContextAccessor) =>
{
var result = await signInManager.PasswordSignInAsync(username, password, false, false);
//IsLoggedIn is true here
var IsLoggedIn = httpContextAccessor?.HttpContext?.User?.Identity?.IsAuthenticated;
if (result.Succeeded)
{
return Results.Ok();
}
return Results.BadRequest($"SignInResult: {result}");
});
And now on posting a login form I perform an API call via HttpClient like that:
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl ??= Url.Content("~/");
using (var client = new HttpClient())
{
var url = $"{Request.Scheme}://{Request.Host}{Request.PathBase}/api/LoginUser?username={Input.Email}&password={Input.Password}";
var response = await client.GetAsync(url);
switch (response.StatusCode)
{
case System.Net.HttpStatusCode.OK:
//But IsLoggedIn becomes FALSE here
var IsLoggedIn = _httpContextAccessor?.HttpContext?.User?.Identity?.IsAuthenticated;
return Redirect(returnUrl);
break;
case System.Net.HttpStatusCode.BadRequest:
var errorText = response.Content.ReadAsStringAsync().Result;
ModelState.AddModelError(string.Empty, errorText);
return Page();
break;
}
}
// If we got this far, something failed, redisplay form
return Page();
}
Problem here is that from PageModel point of view user remains not authenticated even after rage refresh.
If for example I navigate in browser to URL with a proper query string (https://localhost:7210/api/[email protected]&password=!TestPassword123) user becomes logged in after page refresh.
So how to login from a Razor Page via Web API in a correct way?
My sample repo to reproduce that (login with [email protected] and !TestPassword123).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have an ASP .NET Core 9 Razor Pages web app which uses Identity. I've created it from a template, attached my custom DbContext and scaffolded
Identity/Pages/Account/Login
page. And I've tried to add Web API to the same project to be able to have other consumers (iOS/Android applications, PHP/Go clients, etc.). With minimal API syntax I've implemented LoginUser:And now on posting a login form I perform an API call via HttpClient like that:
Problem here is that from PageModel point of view user remains not authenticated even after rage refresh.
If for example I navigate in browser to URL with a proper query string (
https://localhost:7210/api/[email protected]&password=!TestPassword123
) user becomes logged in after page refresh.So how to login from a Razor Page via Web API in a correct way?
My sample repo to reproduce that (login with [email protected] and !TestPassword123).
Beta Was this translation helpful? Give feedback.
All reactions