2

new to Blazor I'm not sure to understand how user session is working.

I'm trying to do a web site with Blazor Server Hosting (.NET Core 3.1). I have a service to Store a Customer like this :

 public class AppData
    {   
        public Customer CurrentCustomer { get; set; }

        public AppData()
        {         
            this.CurrentCustomer = new Customer() { IsWebConnected = false };
        }
    }

On Startup.cs I add :

 public void ConfigureServices(IServiceCollection services)
{
            services.AddRazorPages();
            services.AddServerSideBlazor();

            AppData data = new AppData();
            services.AddSingleton<Service.AppData>(data);
}

What does it mean ? When a CurrentCustomer is set (with a login page for exemple), I can access it with relative data on different pages when I launch on a browser. But when I launch another browser on my pc, I cab see the same user set (with same vaues). It seems to be shared.

I have been working on old ASP.NET Webform and I'm trying to do the same things than Session (One session by user), but is it the same way with Blazor ?

Thanks for your help.

1 Answer 1

2
AppData data = new AppData();
services.AddSingleton<Service.AppData>(data);

This adds a Singleton instance. And yes, that is shared between all users. Cookies are not involved. This behaves like the Application[] object in WebForms.

Session[] has no direct counterpart, but there are many solutions. The docs list them here.

Note that you should try to be as 'stateless' as possible, so consider fetching your data from the Db every time the page Initializes.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.