Authentication And Authorization (Part 2)

 






Configuring the Blazor WebAssembly Project

The WebAssembly project offers similar functionalities. It is slightly more complex because it also necessitates API authentication. If we opt to include authentication when setting up the project, IdentityServer will be used by default to authenticate both the client and the API. IdentityServer is an open-source initiative that will assist us in managing authentication for both our website and our API.

Updating MyBlogWebAssembly.Server

  • Execute the following steps to update the MyBlogWebAssembly.Server project:

  • In the MyBlogWebAssembly.Server project, open Startup.cs.

  • Add the following namespaces-

using MyBlog.Data.Models;

using Microsoft.AspNetCore.Authentication;

  • Open appsetting.json and add the following just after the first curly brace-

"ConnectionStrings": {

"MyBlogDB": "Data Source=../../MyBlog.db" },

  • In the Configure method, just above app.UseEndpoints, add code. The codes need to be between app.UseRouting(); and app.UseEndpoints, otherwise you will get a warning that things might not work as you expect.

  • Now we need _LoginPartial, just as we did for the Blazor Server project. Right click on the Pages folder and select Add | Folder; name the folder Shared.

  • Right-click on Pages/Shared and click Add | Razor page. Name the page _ LoginPartial.cshtml.

  • Add code to the file we just created.

  • Inside of the <ul> tag, add code. Based on the user's current status, it is shown whether the user is logged in. If the user is logged in, then a greeting and a Log out link is shown.

  • We also need to create a controller for Open ID Connect. Right-click on the Controllers folder and select Add | Class. name the class OidcConfigurationController.cs.

  • Open the file we just created and replace the content. The controller is responsible for sending the token to the client when the client requests it.

  • Open appsettings.json and add the name of the client. The name should be specified and used in the API.

Updating MyBlogWebAssembly.Client

Now we need to tell the client project that we want to tell the project to use authentication:

  • First, we need to add a couple of NuGet packages. Right-click on the MyBlogWebAssembly.Client project and click Manage NuGet Packages.

  • Search for Microsoft.AspNetCore.Components.WebAssembly. Authentication and click Install.

  • Search for Microsoft.Extensions.Http and click Install.

  • Open Program.cs and replace the line builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder. HostEnvironment.BaseAddress) }), with the code block.

  • Add the following using statement-

using Microsoft.AspNetCore.Components.WebAssembly. Authentication;

  • Now we need to add a couple of files. Right-click on the Pages folder, select Add | Razor component, and name the file Authentication.razor.

  • Replace the content of the file. The component will redirect and log in on the server.

  • Right-click on the Shared folder, select Add | Razor component, and name the file LoginDisplay.razor.

  • Replace the content of the file. This file differs a bit from the file we created for Blazor Server. It uses the authentication component we just created and makes calls back to the server. It will log out the client and the server.

  • Right-click on the Shared folder, select Add | Razor component, and name the file RedirectToLogin.razor.

  • Open _Imports.razor.

  • Now we need to activate authentication by opening App.Razor and replacing the content with code.

  • Open Pages/FetchData.razor and inject IhttpClientFactory after the @page directive.

  • Change the content of the OnInitializedAsync method. This will call the weather forecast service, and if the token is missing, it will redirect to the login page.

  • Add the following namespace-

@using Microsoft.AspNetCore.Components.WebAssembly. Authentication

  • Now it's time to run the project. Make sure to set MyBlogWebAssembly.Server as a startup project and press F5.

  • Now navigate to the weather forecast service and you will be redirected to the login/ registration page, where you can create an account or log in.

Conclusion

The Blazor WebAssembly project is successfully configured.








































Comments

Popular posts from this blog

Information Protection Scanner: Resolve Issues with Information Protection Scanner Deployment

How AMI Store & Restore Works?

Create A Store Image Task