How can I tell if code will run on the client or server with Blazor?

Viewed 1895

I am new to Blazor development and I'm probably missing something obvious here, but searching google, reading documentation and searching this site has not turned up an answer for me.

I have some code that I need to ensure is run server-side for security reasons (hashing passwords for example). I know Blazor typically determines automatically where code will be run, but in this situation can I ensure that some functions are run in the client and some server-side?

1 Answers

Blazor Server is the application model where all code is run on the server, including Blazor UI components and related code. This is translated into HTML diff messages which are sent via a SignalR.

Blazor WASM (WebAssembly) is where Blazor UI components are downloaded to, and run on the client. So any sensitive or confidential code should not be included in that project.

The selection between the two is initially done when selecting the project type: enter image description here

If you have a Blazor WASM application then you can decide to run certain functions on the server if you wish. The standard "ASP.NET Core hosted" example does this by implementing the weather forecast service as a Web API method (see WeatherForecastController.cs).

You can safely host code in the ASP.NET Core server application where it is never accessible to the client, except via the methods you provide, e.g. Web API methods.

Anything to do with security or validation is a good example. Any password hashing code should always be on the server and never on the client.

Hope this helps clear it up

Related