I'm trying to access a ASP.NET Core (1.1.2) WebAPI from Angular (version 7) which uses cookie authentication. The backend for the Angular application uses ASP.NET Core 2.1.1 and JWT token authentication. Do I need to return the cookie in response and if so how? And how do I access/store that cookie from Angular? Both applications are currently hosted under localhost.
If the cookie is set by logging in manually (Login page from angular app which uses the WebAPI I'm trying to acess) everything works fine. The login from the angular app happens via an Razor HTTP form which generates an input for __RequestVerificationToken
. What also works is if I'm setting the src
attribute of an <iframe>
to the WebAPI login request. Then the login does work as well.
But if I'm trying to do the login process directly from an angular application (GET request with login data) the login process itself runs through, but the login doesn't seem to work, because no cookie has been set.
Request from Angular:
loginToVtsWebConfig(loginUrl: string): Observable<HttpResponse<any>> {
return this.http.get<any>(loginUrl, { observe: 'response' });
}
This is what the request url looks like:
http://localhost:65204/SilentLogin?loginData=g7NfYz4%2fI5bbfPWEdsXoGYtcU8cpGprHaVr2c5m3H81nj6j4gE0YrxwmAyn8c%2ba1YLY55CwkKWP55gKict5J4g%3d%3d
Login at WebAPI which needs to be accessed:
[HttpGet("SilentLogin")]
public async Task<IActionResult> SilentLogin([FromQuery] string loginData)
{
if (string.IsNullOrEmpty(loginData))
{
return Unauthorized();
}
var loginModel = ExtractModelFromQuery(loginData);
if (loginModel == null)
{
return NotFound();
}
return await Login(loginModel, null);
}
Sign in:
HttpContext.Authentication.SignInAsync(CookieIdentifier, new ClaimsPrincipal(claimsIdentity), new AuthenticationProperties() { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddDays(1) }).Wait();
What I'm trying to achieve is to login to the WebAPI from angular (and store cookie) and access any WebAPI methods afterwards.