-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Accessing AuthenticationStateProvider from DbContext or Startup.cs #30982
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
@mrlife thanks for contacting us. I'm not sure what you are trying to achieve, but the answer in general is no, since it needs to be accessed within the scope/lifetime of a Blazor application. |
@javiercn Thanks for getting back to me. What ultimately may help is to understand why accessing a scoped service in |
@mrlife Blazor creates its own scope every time a new session is established (when you start a circuit) and is not associated with an HTTP request. You can only operate within the context of the Blazor scope either inside a component or a circuit handler. In addition to that, If your component inherits from |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Thanks for your feedback, @mrlife. |
I don't know where to write, but I have a similar problem in Blazor Server application, so I will describe my situation. In short -> I need to get logged in user Id from dbcontext, why -> Inside dbcontext class I have the following method:
loggedUserService previously worked fine and looks like this ->
Now, in .Net 6 - httpContextAccessor is not working anymore. Rewriting the loggedUserService to start using AuthenticationStateProvider doesn't bring results because I get the exception in this case: GetAuthenticationStateAsync was called before SetAuthenticationState. So, what should I do? Any help? |
Not having a way to access user-level data on code that runs on the server (outside of Blazor's scope) is an ongoing issue that I hope will be addressed one day. |
@mrlife @Bobetti The user identity established on the server is typically stored on the HttpContext. AuthenticationStateProvider is used for getting the authentication state on the client, which doesn't operate in the context of a request. What prevents you from flowing the user from the HttpContext to where you need to do your user specific database queries? |
Hi @danroth27, I really appreciate you jumping in on this. I am currently pulling the user from the initial HttpContext into Blazor's scope through Host.cshtml.cs -> App.razor. That is working great. The issue is kind of in the other direction. With the Audit.NET audit framework package, there is a configuration that's set up once in Startup.cs. There's code in that configuration that runs on the server after EF Core's SaveChanges completes that needs to access the user in order to log who made the change. Currently, the only option is to use HttpContextAccessor, which is not recommended because it is not guaranteed to work in Blazor apps. |
@danroth27 Possible to have an official response so we know whether to hold out or look for an alternative to Audit.NET? |
@mrlife You can't access the AuthenticationStateProvider in Startup. AuthenticationStateProvider is a scoped service that is only valid in the context of the Blazor session (circuit). The issue is similar as trying to access the HttpContext outside of an HTTP request using IHttpContextAccessor. At that time the service is not setup correctly. |
@javiercn Thanks for writing. I'm not asking for AuthenticationStateProvider to be available in Startup, explicitly. Any solution would be helpful.
|
@mrlife what you are asking is fundamentally impossible. There are not users when Startup is running. I'm not sure what you are trying to accomplish here. The only thing that you could do today to access user-level data from outside of the circuit scope would be to register a singleton service on DI and have the circuit (maybe via a circuit handler) notify that service that a new circuit has been created. If that's what you are looking for, that's something you can already do today and not something we plan to offer out of the box. |
@mrlife Does your configured Audit.NET code always run in the context of a request? If it does, then I think using |
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. |
@danroth27 Daniel, it would be a great help if you could guide how to do this. I do not really understand how to pass user data into dbconext. |
Hi @danroth27,
I tell that code to find the user id with the following in Program.cs (app doesn't use Startup.cs): app.Services.GetService<IHttpContextAccessor>()?.HttpContext?.User?.FindFirstValue("UserId"); |
Is it possible to access Blazor's
AuthenticationStateProvider
from a DbContext or inStartup.Configure()
?I receive this error:
@Blackleones, did you ever get this line to work in your DbContext? ref #17442 (comment)
The text was updated successfully, but these errors were encountered: