MSAL 身份验证 - WASM Web 应用程序 - 无法构造 URL

问题描述 投票:0回答:1

我需要你们聪明人的帮助。

我需要使用 EntraID 进行 msal 身份验证。我有一个 net 6 wasm 应用程序,但遇到了愚蠢的错误 - 我一定已经检查了我的 appsettings.json 文件 100 次才发现拼写错误。

我收到错误:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 渲染组件未处理的异常:无法构造“URL”:无效的 URL 类型错误:无法构造“URL”:无效的 URL 在新的u(https://localhost:7151/_content/Microsoft.Authentication.WebAssembly.Msal/AuthenticationService.js:2:191011) 在l.init(https://localhost:7151/_content/Microsoft.Authentication.WebAssembly.Msal/AuthenticationService.js:2:196273)

我的网络应用程序让 msal 工作确实非常非常基本,这里没什么特别的。 Appsettings.json 文件

索引.html

AuthWeb应用程序 加载中...
<div id="blazor-error-ui">
    An unhandled error has occurred.
    <a href="" class="reload">Reload</a>
    <a class="dismiss">🗙</a>
</div>
<script src="_framework/blazor.webassembly.js"></script>
<script src="_content/Microsoft.Authentication.WebAssembly.Msal/AuthenticationService.js"></script>

程序.cs

using AuthWebApp;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

builder.Services.AddMsalAuthentication(options =>
{
    builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
    options.ProviderOptions.LoginMode = "redirect";
});


await builder.Build().RunAsync();

index.razor

@page "/"

<PageTitle>Index</PageTitle>

@attribute [Authorize]

<h1>User needs to be in AD for this page to work</h1>

身份验证.razor

@page "/Authentication/{action}"
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication

<RemoteAuthenticatorView Action="@action"></RemoteAuthenticatorView>

@code {
    [Parameter] public string ?action { get; set; }
}

我似乎无法克服这个错误,并且日志记录在浏览器上几乎没有用处。应用程序注册只是一个基本的注册,重定向设置为:

https://localhost:7151/Authentication/login-callback

有人有什么快速的想法吗?

blazor .net-6.0 webassembly azure-ad-msal
1个回答
0
投票

我遇到了同样的问题,我将以下行添加到客户端 wasm 的 csproj 中,这应该可以解决问题:

<ItemGroup>
    <TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" />
</ItemGroup>
© www.soinside.com 2019 - 2024. All rights reserved.