Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpContext with user is null #4

Open
jareksgit opened this issue Mar 30, 2021 · 2 comments
Open

HttpContext with user is null #4

jareksgit opened this issue Mar 30, 2021 · 2 comments

Comments

@jareksgit
Copy link

Hey and thanks for the effort and the illustration.
But the whole thing works a bit limited.
When the application is initially loaded, the server-side httpContext with the user is empty, so an exception is thrown.

public Task<CurrentUser> CurrentUserInfo() { **var user = httpContextAccessor.HttpContext.User;** return Task.FromResult(new CurrentUser { IsAuthenticated = user.Identity.IsAuthenticated, UserName = user.Identity.Name, Claims = user.Claims .ToDictionary(c => c.Type, c => c.Value) }); }

@jdtcn
Copy link
Owner

jdtcn commented Mar 31, 2021

Hi,

It's a strange situation because HttpContext.User must have non-null value regardless authentication state. Have you made any changes in the project files? What environment do you use? Is httpContextAccessor null or httpContextAccessor.HttpContext is null?

изображение

@jareksgit
Copy link
Author

Hello,

in my local environment the whole thing ran without any problems and I can also reproduce your debugging result locally.
After deploying it on Azure, I became aware of the problem. Except for the database and some settings, I made no changes and deployed it 1 to 1.

After my research, see Stackoverflow Answer 1 with a small link in it to Stackoverflow Answer 2.
And there I found a link to the official documentation on this matter: Documentation Link
And in it the note that the HttpContext isn't guaranteed to be available within the IHttpContextAccessor, nor is it guaranteed to be holding the context that started the Blazor app.

The Blazor Server Starter via Visual Studio does not visibly access the httpcontext to query the user, but uses a helper method (RevalidatingServerAuthenticationStateProvider) to get our current user state. Maybe this is an approach to bypass the whole thing with the HttpContext?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants