How to implement guide with asp.net core identity integration in a blazor server app
I'm having a hard time understanding I'm optimizing some code but Hey everyone, I'm running into an issue that's driving me crazy... I'm deploying to production and This might be a silly question, but I'm relatively new to this, so bear with me..... I'm currently working with a scenario integrating ASP.NET Core Identity into my Blazor Server application. I'm using ASP.NET Core 6.0, and although I've followed the official documentation, I keep running into a `NullReferenceException` when trying to access user claims after login. The setup seems correct as I'm able to register and log in users without any issues, but when I try to fetch user claims in a component, I get the following behavior: ``` System.NullReferenceException: Object reference not set to an instance of an object. at MyApp.Pages.UserProfile.OnInitializedAsync() ``` Hereโs a snippet of my `UserProfile` component where I'm trying to fetch the user claims: ```csharp @inject SignInManager<IdentityUser> SignInManager @inject UserManager<IdentityUser> UserManager @code { private ICollection<Claim> _claims; protected override async Task OnInitializedAsync() { var user = await SignInManager.UserManager.GetUserAsync(HttpContext.User); if (user != null) { _claims = await UserManager.GetClaimsAsync(user); } } } ``` I have verified that the user is indeed logged in, as I can access `HttpContext.User.Identity.IsAuthenticated`, which returns `true`. However, when trying to get the user object, it seems to return null. I did configure services in my `Program.cs` as follows: ```csharp builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); builder.Services.AddIdentity<IdentityUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); ``` I've tried adding `[Authorize]` attribute to my component, but it doesnโt seem to solve the question. Any insights on what could be going wrong or what I might be missing in the configuration? I'm working on a web app that needs to handle this. Am I missing something obvious? I'm coming from a different tech stack and learning Csharp. Any suggestions would be helpful. Thanks for your help in advance! I'm developing on macOS with Csharp. For context: I'm using Csharp on Windows 10.