BlazorWebAssembly项目在ASP.NETCore项目上托管时出现身份验证问题。
创始人
2024-12-22 00:30:27
0
  1. 确保 ASP.NET Core 项目已启用身份验证。可以在 Startup.cs 文件中的 ConfigureServices 方法中添加以下代码:

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 是客户端的标识符。

  1. 确保 Blazor WebAssembly 项目中的 appsettings.json 文件中的认证配置与 ASP.NET Core 项目中的配置一致。例如:

{ "Authentication": { "Authority": "https://localhost:5001", "Audience": "blazor_webassembly_client" } }

  1. 在 Blazor WebAssembly 项目中的 Program.cs 文件中,使用以下代码启用身份验证:

builder.Services.AddOidcAuthentication(options => { options.ProviderOptions.Authority = "https://localhost:5001"; options.ProviderOptions.ClientId = "blazor_webassembly_client"; });

其中,AddOidcAuthentication 是 Microsoft.AspNetCore.Components.WebAssembly.Authentication 命名空间下的扩展方法,用于使用 OpenID Connect 协议进行身份验证。

  1. 在 Blazor 页面中使用以下代码获取身份验证的用户信息:

@using Microsoft.AspNetCore.Components.Authorization @using System.Security.Claims

@inject AuthenticationStateProvider AuthenticationStateProvider

Hello, @context.User.FindFirstValue(ClaimTypes.Name)! Welcome to our site!
You are not authorized to access this page.

其中,AuthorizeView 是 Microsoft.AspNetCore.Components.Authorization 命名空间下的组件,用于授权视图的渲染

相关内容

热门资讯

黑科技攻略(德州之星)外挂辅助... 黑科技攻略(德州之星)外挂辅助神器(透视)透明挂教程(竟然有挂)1、起透看视 德州之星透明视辅助2、...
黑科技代打(wepOkE)黑科... 黑科技代打(wepOkE)黑科技透明挂辅助挂(透视)透牌教程(竟然真的是有挂)1、用户打开应用后不用...
黑科技系统!(aapokEr)... 黑科技系统!(aapokEr)透明挂辅助器,(AAPOkER)从来真的有挂,2025版教程(有挂技巧...
黑科技工具(we poker)... 黑科技工具(we poker)黑科技透明挂辅助软件(透视)黑科技教程(一直是真的有挂)1、不需要AI...
黑科技讲解!(德扑)外挂辅助a... 黑科技讲解!(德扑)外挂辅助app,(德扑)原来存在有挂,解密教程(有挂方法);致您一封信;亲爱德扑...
黑科技攻略(红龙扑克)外挂辅助... 黑科技攻略(红龙扑克)外挂辅助方法(透视)揭秘教程(其实有挂)1、红龙扑克透视辅助简单,红龙扑克软件...
黑科技有挂!(wEpoKe)软... 黑科技有挂!(wEpoKe)软件透明挂,(WePoKer)素来是有挂,解说技巧(有挂技巧),支持语音...
黑科技好牌(微扑克私人局)外挂... 黑科技好牌(微扑克私人局)外挂透视辅助神器(透视)新2025教程(总是是真的有挂);1、该软件可以轻...
黑科技插件!(wPK)透视辅助... 黑科技插件!(wPK)透视辅助器,(wpK)原生存在有挂,攻略教程(有挂教程);《WPK辅助透视》‌...
黑科技线上(WepOke)黑科... 黑科技线上(WepOke)黑科技透明挂辅助挂(透视)可靠技巧(一直真的是有挂)1、用户打开应用后不用...