services.AddAuthentication(options => { options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.Authority = "https://localhost:5001"; options.Audience = "blazor_webassembly_client"; });
其中,JWTBearerDefaults 为 Microsoft.AspNetCore.Authentication.JwtBearer 命名空间下的静态类,包含身份验证和授权相关的常量值。Authority 是认证服务器的地址,Audience 是客户端的标识符。
{ "Authentication": { "Authority": "https://localhost:5001", "Audience": "blazor_webassembly_client" } }
builder.Services.AddOidcAuthentication(options => { options.ProviderOptions.Authority = "https://localhost:5001"; options.ProviderOptions.ClientId = "blazor_webassembly_client"; });
其中,AddOidcAuthentication 是 Microsoft.AspNetCore.Components.WebAssembly.Authentication 命名空间下的扩展方法,用于使用 OpenID Connect 协议进行身份验证。
@using Microsoft.AspNetCore.Components.Authorization @using System.Security.Claims
@inject AuthenticationStateProvider AuthenticationStateProvider
其中,AuthorizeView 是 Microsoft.AspNetCore.Components.Authorization 命名空间下的组件,用于授权视图的渲染