C# 未处理的异常呈现组件:找不到';认证服务';在';窗口';
如果用户试图访问授权页面,我想将未授权用户重定向到登录页面。 到目前为止,我使用教程编写了这篇文章。项目建设成功。但浏览器抛出了这个错误 未处理的异常呈现组件:在“窗口”中找不到“AuthenticationService” 和页面上粘贴的授权C# 未处理的异常呈现组件:找不到';认证服务';在';窗口';,c#,blazor,C#,Blazor,如果用户试图访问授权页面,我想将未授权用户重定向到登录页面。 到目前为止,我使用教程编写了这篇文章。项目建设成功。但浏览器抛出了这个错误 未处理的异常呈现组件:在“窗口”中找不到“AuthenticationService” 和页面上粘贴的授权 <Authorizing> <div class="main">Please wait...</div> </Authorizing> Program.cs 公共类程序 { 公共静
<Authorizing>
<div class="main">Please wait...</div>
</Authorizing>
Program.cs
公共类程序
{
公共静态异步任务主(字符串[]args)
{
var builder=WebAssemblyHostBuilder.CreateDefault(args);
建筑商服务
.AddBlazorise(选项=>
{
options.ChangeTextOnKeyPress=true;
})
.AddBootstrapProviders()
.AddFontAwesomeIcons();
builder.Services.AddSingleton(新的HttpClient
{
BaseAddress=新Uri(builder.HostenEnvironment.BaseAddress)
});
builder.RootComponents.Add(“应用程序”);
addScope(sp=>newHttpClient{BaseAddress=newURI(builder.HostenEnvironment.BaseAddress)});
builder.Services.AddApiAuthorization(选项=>{
options.authenticationpath.LogInPath=“auth/login”;
options.authenticationpath.LogInCallbackPath=“auth/login回调”;
options.authenticationpath.LogInFailedPath=“auth/login失败”;
options.authenticationpath.LogOutPath=“auth/logout”;
options.authenticationpath.LogOutCallbackPath=“auth/logout callback”;
options.authenticationpath.LogOutFailedPath=“auth/logout失败”;
options.authenticationpath.LogOutSucceededPath=“auth/logged-out”;
options.authenticationpath.ProfilePath=“auth/profile”;
options.authenticationpath.RegisterPath=“auth/register”;
});
var host=builder.Build();
主机服务
.UseBootstrapProviders()的
.UseFontAwesomeIcons();
等待host.RunAsync();
}
}
Counter.cs
@page”/counter
@属性[授权]
柜台
当前计数:@currentCount
点击我
@代码{
私有int currentCount=0;
私有void IncrementCount()
{
currentCount++;
}
}
Server/Startup.cs
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Hosting;
使用KermesAPI.Helpers;
使用KermesAPI.Services;
使用自动制版机;
使用制度;
命名空间Kermes.Server
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext();
services.AddAuthentication();
services.AddControllers().AddJsonOptions(x=>x.JsonSerializerOptions.IgnoreNullValues=true);
AddAutoMapper(AppDomain.CurrentDomain.GetAssemblys());
services.addswagggen();
services.Configure(Configuration.GetSection(“AppSettings”);
services.addScope();
services.addScope();
services.AddControllersWithViews();
services.AddRazorPages();
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebAssemblyDebugging();
}
其他的
{
app.UseExceptionHandler(“/Error”);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
MapFallbackToFile(“index.html”);
});
}
}
}
试试这个:
<script src="_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script>
我也有同样的问题。在Blazor客户端项目的index.html文件中添加上面的脚本引用为我解决了这个问题。在“窗口”部分,这表明这是一个JavaScript错误,您没有发布
index.html
@mo74可能有正确的答案,但不清楚为什么没有这一行。我似乎记得pizza示例可以追溯到Blazor WebAssembly中包含身份验证之前。可能存在版本问题。当我添加此脚本时,错误更改为未处理的异常呈现组件:无法从“\u configuration/Client”
@MuhammedBalta”加载设置:下载已完成的Pizza应用程序并比较它使用的Blazor版本。确定。我上面提到的错误原因是我的服务器中缺少OIDCConfiguration Controller,因此
修复了我的问题。谢谢,如果有人想知道你是怎么知道要添加这个的,这在中提到过。我到这里来是因为我跳过了那部分。
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using Kermes.Client
@using Kermes.Client.Shared
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Blazorise
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services
.AddBlazorise(options =>
{
options.ChangeTextOnKeyPress = true;
})
.AddBootstrapProviders()
.AddFontAwesomeIcons();
builder.Services.AddSingleton(new HttpClient
{
BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
});
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddApiAuthorization(options => {
options.AuthenticationPaths.LogInPath = "auth/login";
options.AuthenticationPaths.LogInCallbackPath = "auth/login-callback";
options.AuthenticationPaths.LogInFailedPath = "auth/login-failed";
options.AuthenticationPaths.LogOutPath = "auth/logout";
options.AuthenticationPaths.LogOutCallbackPath = "auth/logout-callback";
options.AuthenticationPaths.LogOutFailedPath = "auth/logout-failed";
options.AuthenticationPaths.LogOutSucceededPath = "auth/logged-out";
options.AuthenticationPaths.ProfilePath = "auth/profile";
options.AuthenticationPaths.RegisterPath = "auth/register";
});
var host = builder.Build();
host.Services
.UseBootstrapProviders()
.UseFontAwesomeIcons();
await host.RunAsync();
}
}
@page "/counter"
@attribute [Authorize]
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using KermesAPI.Helpers;
using KermesAPI.Services;
using AutoMapper;
using System;
namespace Kermes.Server
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>();
services.AddAuthentication();
services.AddControllers().AddJsonOptions(x => x.JsonSerializerOptions.IgnoreNullValues = true);
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services.AddSwaggerGen();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.AddScoped<IAccountService, AccountService>();
services.AddScoped<IEmailService, EmailService>();
services.AddControllersWithViews();
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}
}
}
<script src="_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script>