Hi @Arnold Mendoza ,
If you don't want to use identity for authentication, the authentication in the Blazor server application can be done by registering a cookie or JWT authentication handler. Because the Blazor server runs in an ASP.NET Core application, cookie authentication is simpler. You can do this by adding Cookie AuthenticationHandler to the ConfigureServices method in Startup.cs:
services.AddAuthentication()
.AddCookies();
You then also need to instruct the Middleware to inspect incoming Request wheter they have a valid Cookie so that then the ClaimsPrincipal property on the HttpContext can be set:
app.UseAuthentication();
By using this configuration every User that requests your app with a Cookie you issued to them by calling SignInAsync from a controller is authenticated. You cant append a Cookie to a Http response inside a Razor Component because the Response has already finished (all changes are send over the WebSockets Connection). You then can use the built in AuthorizeView component to only render content to authenticated users. Without further configuration every user that you issued a cookie to is authenticated.
You can also use JWT authentication. As it is the case with Cookies ASP.NET Core has also built-in middleware to achieve that. In such case I would build a custom AuthenticationProvider that implements the abstract class AuthenticationStateProvider. Inside the GetAuthenticationStateAsync() method you retrieve the token from LocalStorage. You then need to check its validity so the according AuthenticationState can be returned (Its has a property of type ClaimsPrincipal).
You can build your own Authorization Filter and check for the tokens validity.
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
ChaoDeng