Persist values by user

Hoping someone has done this before...

My app has a main screen where a user selects a company and a contact within that company. From there, the user gets access to a collection of pages that shows (read-only) views of various bits of data.

I need to hold on to the CompanyID and the ContactID across all these pages. All of the pages use at the very least the CompanyID. Both are string values.

I thought at first I would use Session, so I loaded up the extended package, configured it in Startup and found that I could not use it - mainly because I can't change the constructor signature for any of the .RAZOR.CS files. If I could, then I could possibly use dependency injection to get to the Session object.

I also am not able to see/set a TempData object from the .RAZOR.CS files.

And I can't get to the HttpContext object because, again, no way to use dependency injection so no way to get to a non-null version of that object (and I think it is already out of scope by the time the .RAZOR.CS class is called).

I don't have any way to identify the user so I can't use a cache. I need some way for the user to identify themselves, and as this app is using AD authentication, I just know they are authenticated, not who they are.

There has got to be a way to do this on a per-user basis. Anyone have any ideas how to do this?

Hi @dferreira042,

Maybe you can use Protected Browser Storage package:

We have partial void OnConfigureServices(IServiceCollection services) in our Startup class that can be used to execute services.AddProtectedBrowserStorage().

Best Regards,
Vladimir

I thought at first I would use Session, so I loaded up the extended package, configured it in Startup and found that I could not use it - mainly because I can't change the constructor signature for any of the .RAZOR.CS files.

  1. Session doesn't work in Blazor applications (because they use SignalR).
  2. Dependencies in razor pages aren't specified via constructor parameters. You can use properties in the razor.cs files decorated with the [Inject] attribute.

I also am not able to see/set a TempData object from the .RAZOR.CS files.

TempData is also not supported in Blazor.

I don't have any way to identify the user so I can't use a cache. I need some way for the user to identify themselves, and as this app is using AD authentication, I just know they are authenticated, not who they are.

You can get the current user from the SecurityService that Radzen provides. It is available as the Security property in all generated pages.

If the current user isn't enough then you should consider the solution that @enchev linked.

Thanks for the help!

My client is moving away from an on-premise AD to Azure AD, so the security service doesn't work for me. Instead I have to use the Azure AD client which means no security service. Thankfully they expose a way to secure all pages from within the Startup class, so in that respect I don't have to change the RAZOR code.

So I've implemented a cache because I had trouble getting the server pages to display using the protected storage (any time I accessed the storage in the Load event the page would not display - no error, just nothing rendered). The cache approach seems to be working just fine.

FYI, the Azure AD client version 3.1.1 (the current and what Microsoft deems to be "stable") does not work. When you try to add the client service in Startup you get an error that the RAZOR page code can't be found. The only version of the Azure AD client that works is 3.1.0, the previous version. That seems to work quite nicely.

Hi Radzen Team,

I am trying to use the Protected Browser Storage, but not getting it right. I have added the service and all the other recommendation on the Microsoft page.

My method in the xxxx.razor.cs files gives the following error when I call it:

image

Tried everything here, no luck

Does anyone have an idea?

Regards
Riaan

I ended up bailing out on both the in-memory cache and protected storage in favor of a per-instance persisted object. As long as the SignalR connection remains open, the object exists for that connection. Working beautifully so far, and it seems to scale well enough.

Thanks David for the reply, I will have a look at your approach.