tonyyip1969 / Blorc.OpenIdConnect

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blorc.OpenIdConnect

The right way to use OpenIdConnect on Blazor.

Usage

  1. Install Blorc.OpenIdConnect via NuGet.

  2. Include Blorc.Core/injector.js the index.html file:

     <head>
             ...
             <script src="_content/Blorc.Core/injector.js"></script>
             ...
     </head>        
    
  3. Update App.razor content like this:

     @using Microsoft.AspNetCore.Components.Authorization
    
     <CascadingAuthenticationState>
             <Router AppAssembly="@typeof(Program).Assembly">
                     <Found Context="routeData">
                             <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
                     </Found>
                     <NotFound>
                             <LayoutView Layout="@typeof(MainLayout)">
                             <p>Sorry, there's nothing at this address.</p>
                             </LayoutView>
                     </NotFound>
             </Router>
     </CascadingAuthenticationState>
    
  4. Add the required service and update Program.cs file as follow:

     builder.Services.AddBlorcCore();
     builder.Services.AddAuthorizationCore();
     builder.Services.AddBlocOpenIdConnect(
                     options =>
                     {
                             builder.Configuration.Bind("IdentityServer", options);
                     });
    
     var webAssemblyHost = builder.Build();
    
     await webAssemblyHost
             .ConfigureDocumentAsync(
                     async documentService =>
                     {
                             await documentService.InjectBlorcCoreJsAsync();
                             await documentService.InjectOpenIdConnectAsync();
                     });
    
     await webAssemblyHost.RunAsync();
    
  5. Add a configuration file wwwroot\appsettings.json

     {
       "IdentityServer": {
         "ResponseType": "id_token token",
         "Scope": "openid profile %API-NAME%",
         "RedirectUri": "%APPLICATION_URL%",
         "PostLogoutRedirectUri": "%APPLICATION_URL%",
         "Authority": "%IDENTITY_SERVER_URL%",
         "ClientId": "%CLIENT_ID%",
         "AutomaticSilentRenew": true | false,
       }
     }
    

About

License:MIT License


Languages

Language:C# 96.0%Language:HTML 2.1%Language:JavaScript 1.0%Language:CSS 0.8%Language:PowerShell 0.1%