Issue with Auth princliples not being able to write to response in blazor app through an injected service
LoginModal:
@using Microsoft.AspNetCore.Components.Authorization
@using MtgDeckBuilderServices
@inject NavigationManager Navigation
@inject IMtgApiAuthorizationService AuthService
@inject IHttpContextAccessor HttpContextAccessor
@if (IsOpen)
{
<div class="modal-backdrop fade show"></div>
<div class="modal show d-block" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Log In</h5>
<button type="button" class="btn-close" @onclick="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input id="username" class="form-control" @bind="Username" />
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input id="password" type="password" class="form-control" @bind="Password" />
</div>
@if (!string.IsNullOrEmpty(ErrorMessage))
{
<div class="alert alert-danger">@ErrorMessage</div>
}
</div>
<div class="modal-footer">
<button class="btn btn-primary" @onclick="Login">Log In</button>
<button class="btn btn-secondary" @onclick="Close">Cancel</button>
</div>
</div>
</div>
</div>
}
@code {
[Parameter] public bool IsOpen { get; set; }
[Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
[Parameter] public EventCallback OnLoginSuccess { get; set; }
private string Username { get; set; }
private string Password { get; set; }
private string ErrorMessage { get; set; }
private async Task Login()
{
ErrorMessage = string.Empty;
var httpContext = HttpContextAccessor.HttpContext;
if (httpContext == null)
{
ErrorMessage = "Unable to access HttpContext.";
return;
}
var result = await AuthService.AuthenticateAndSignInAsync(httpContext, Username, Password);
if (result != null)
{
await IsOpenChanged.InvokeAsync(false);
await OnLoginSuccess.InvokeAsync();
}
else
{
ErrorMessage = "Invalid credentials.";
}
}
private async Task Close()
{
await IsOpenChanged.InvokeAsync(false);
}
}
Then in the Auth Service, this is relevant:
public void SetAuthCookie(HttpContext context, string token)
{
var options = new CookieOptions
{
Expires = DateTimeOffset.UtcNow.AddDays(7),
HttpOnly = true,
IsEssential = true,
Domain = context.Request.Host.Host,
SameSite = SameSiteMode.Strict,
Secure = true
};
#if DEBUG
options.Secure = false;
#endif
try
{
context.Response.Cookies.Append("mtgdb_auth_cookie", token, options);
}
catch (Exception ex)
{
}
}
public async Task<UserCredential?> AuthenticateAndSignInAsync(HttpContext context, string username, string password)
{
var userCredential = await AuthenticateAsync(username, password);
if (userCredential == null)
return null;
var token = CreateToken(context, userCredential);
SetAuthCookie(context, token);
userCredential.TokenExpiration = DateTime.UtcNow.AddDays(7).ToString();
userCredential.AuthData = token;
return userCredential;
}
Is this possible to do without an API call? Keep in mind this service works perfectly in an ASP.NET Core 8 Web API.
The problem tracks to this (response not writable... lmao it hasn't returned yet, what a joke).
context.Response.Cookies.Append("mtgdb_auth_cookie", token, options);