Issue with httpContext and cascadingParamter in blazor web app

I am attempting to mkae a login page on a blazor maui hybrid web app (new to the .NET 9.0 preview 5) Essentially I am just working on the webapp side and ignoring maui just trying to get this to work. I am following this tutorial but I have an issue on the login page. He makes a httpContext with the attribute (i think thats what those are called in the []) of cascading parameter. I dont see where he ever initilizes it (i dont think he does) so when he calls it later it errors saying its null here is the important code in the login page:
@code {
[CascadingParameter]
public HttpContext httpContext { get; set; }

[SupplyParameterFromForm]
public LoginViewModel Model { get; set; } = new();

private string? errorMessage;

private async Task Authenticate()
{
var userAccount = appDbContext.UserAccounts.Where(x => x.UserName == Model.UserName).FirstOrDefault();
if(userAccount is null || userAccount.Password != Model.Password)
{
errorMessage = "Invalid User Name or Password";
return;
}

var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, Model.UserName),
new Claim(ClaimTypes.Role, userAccount.Role)
};

var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);

await httpContext.SignInAsync(principal);
navigationManager.NavigateTo("/");

}
}
@code {
[CascadingParameter]
public HttpContext httpContext { get; set; }

[SupplyParameterFromForm]
public LoginViewModel Model { get; set; } = new();

private string? errorMessage;

private async Task Authenticate()
{
var userAccount = appDbContext.UserAccounts.Where(x => x.UserName == Model.UserName).FirstOrDefault();
if(userAccount is null || userAccount.Password != Model.Password)
{
errorMessage = "Invalid User Name or Password";
return;
}

var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, Model.UserName),
new Claim(ClaimTypes.Role, userAccount.Role)
};

var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);

await httpContext.SignInAsync(principal);
navigationManager.NavigateTo("/");

}
}
the await httpContext line is erroring
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server