简体   繁体   中英

Blazor WASM Cookie Authentication .NET5

Im building my first Blazor WASM (Client and Server) app in .NET5 and having troubles with the authentication. My goal is to not use Identity since I don't want to use localstorage and only have a JWT (or similar) cookie authentication like this picture: Chrome DevTools .

I have tried to use the

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie( ... )

on server side but then Im getting an error saying

There is no registered service of type 'Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider'. .

I got no error with

builder.Services.AddOidcAuthentication

in the Client's Program.cs but the app did not authorized me after I logged in (I got a token in my cookies).

I feel like I have tried every Microsoft Docs and tutorials out there but with no luck so if someone have a solution to this it would be very appreciated!

I have project Blazor webassembly which get authenticated by Identity server you could find the full code here https://github.com/kdehia/BlazorSecureWithIdentity_API_GRPC

in main.cs you should get the config values from appsettings.json

builder.Services.AddOidcAuthentication(options =>
        {

            builder.Configuration.Bind("oidc", options.ProviderOptions);

        });

your appsettings.json should look something like this

{
  "oidc": {
    "Authority": "https://localhost:5005/",
    "ClientId": "blazorWASM",
    "DefaultScopes": [
      "openid",
      "profile",
      "companyApi",
      "ApplicationRole",
      "GRPC"
    ],
    "PostLogoutRedirectUri": "authentication/logout-callback",
    "RedirectUri": "authentication/login-callback",
    "ResponseType": "code"
  }
}

Download the full project and let me know if you have any question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM