© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago
FacePalmGamer

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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Blazor web app in console
C#CC# / help
3y ago
Blazor web assembly app
C#CC# / help
2y ago
blazor web app using C#
C#CC# / help
12mo ago
Blazor Web App and auth from separate webApi with Identity
C#CC# / help
3y ago