SecurityService.User not available to ExportController

Hello,

I am writing a SAAS application for my business clients. This application manages several organizations and each user is associated with only one organization.


Fig. ER Diagram

All entities that are created afterwards (e.g. machines, components, etc.) are associated at the time of their creation to the same organization as the one to which the current user belongs.


Fig. SecurityService injection in main application DB Service


Fig. Machine creation with association with the current user's organization in main application DB Service

This makes it possible to display only the entities that belong to the organization to which the current user belongs. This is essential for an application that will manage dozens of different organizations. It works very well when reading, for example to display a grid of machines.


Fig. Machine are filtered by current user's organization in main application DB Service

However, it does not work when I try to export data with the Export button of a DataGrid.


Fig. The return value of 'mach' is null in ExportController when we try to export machines

The reason is that when I call the 'GetMachines' method for example, which in turn calls the 'OnMachineRead' method, the user found by 'security.user' is not the current user. But rather a user named Anonymous. This causes the 'OnMachineRead' method to return no machines, since it has no idea what organization the user Anonymous belongs to.

Do you have any idea why the current user is available when reading a table for display but not available for data export?

Thanks :slight_smile:

The export controller does not run in Blazor context and this is why any Blazor scoped services are not available. You can try using the User property of the controller instead.

Thank you for the information.

However, after opening User in the debugger, it seems that it does not contain any useful information about the user (name, organization, etc.). It seems to be more about the claims from what I can see.

Please check the Claims and or Identity. If you use Blazor server the user should be the currently logged one.

The only data available in the User object is the user's name. It is always possible to find the complete user with code, but I would have liked to make it simpler and more convenient.

Since the controller makes a call to a method of the DB Service module of the application, shouldn't the complete user be available in DB Service, since SecurityService is injected in it?

Thanks

SecurityService is initialized via method call which accepts a certain object available only in a Blazor context. This is why it isn't working in a controller. You would need to retrieve the user by its user name similar to what SecurityService does - check its code.

1 Like

It works,

I used 'User.Identity.Name' of the export controller to call a new 'GetUserByName' method that I created in the 'SecurityService'. It's neat and simple :slight_smile:

Thanks