-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[Blazor] Availability of User in Startup #35611
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
Comments
In Blazor, you could use |
My bad, it's |
Thank you. I tried
When does |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
@pranavkm Thanks for reopening this issue. From what I can tell, including a call to
Looks like I forgot I've run into this before and it wasn't supported at the time. It also looks like a number of folks are trying to get to User information outside of a Blazor page but not having a way. Is it possible to access the |
@javiercn @mkArtakMSFT In the meantime, is there any way to store a user-specific value and recall it, both actions outside of the "Blazor scope"? |
@mrlife, don't call You can access the HttpContext object from the Configure method like this:
Hope this help... |
@enetstudio Thank you, does your example use public void Configure(IApplicationBuilder app, IHttpContextAccessor contextAccessor)
{
Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
{
scope.Event.Environment.CustomFields["UserId"] = contextAccessor.HttpContext.User.FindFirstValue("UserId");
});
...
} This is how I've been using it, however Microsoft says:
|
@mrlife, my code is a middleware that accesses the HttpContext object directly from the Configure method. You can access the HttpContext.User property, and other values and do something interesting with them, as for instance, passing the User IP address to a service that may be injected into your Razor components, etc.
This simply means: Do not create, as for instance, a service which accesses the HttpContext object through IHttpContextAccessor, inject the service into a Razor component, and try to access the HttpContext object from within your component. This should not be done even if sometimes it seems to work perfectly well. Note: The initial request to your web app is always HTTP one. This is the time to capture the HttpContext and to extricate values from it to be used later in your app. This is also applicable when a user login into your app... This is the time the HttpContext object is available, and you can access it with no issues |
@enetstudio Thank you for this. Could you clarify how your |
What do you mean how it is defined ? This is a middleware from which I grab the HttpContext object passed in the HTTP pipeline. I do not define the |
@enetstudio My use case is for a third party package called Audit.NET. I need to access the |
Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue. This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue! |
@mkArtakMSFT I believe this should still be open since it is a real need. Could you reopen this issue? |
@mrlife What you are looking for is not possible, a Blazor application doesn't start until a websocket is established. The best you can probably do is to use a circuit handler to grab the user there and setup some circuit specific state. |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Is your feature request related to a problem? Please describe.
Sometimes, configuration needs to run once on the server, such as in
Startup.Configure()
. With ASP.NET Core, theIHttpContextAccessor
can be used, however, this isn't reliable in Blazor. Is there another way?Describe the solution you'd like
Any way to access properties of the current user in configuration that runs once on the server would be beneficial.
Additional context
Please see this issue for background and more info: thepirat000/Audit.NET#440
The text was updated successfully, but these errors were encountered: