【问题标题】:Injecting IAccessTokenProvider failing when using custom AuthenticationStateProvider - Blazor Wasm使用自定义 AuthenticationStateProvider - Blazor Wasm 时注入 IAccessTokenProvider 失败
【发布时间】:2022-06-22 04:25:02
【问题描述】:

我正在开发 Wasm Blazor 应用程序,并且我正在通过身份服务器使用 OidcAuthentication。为了进行测试,我通过实现一个自定义 AuthenticationStateProvider 来使用 Mock 身份验证,它只是向用户上下文添加了一些声明。在我的一些组件中,我注入 TokenProvider 以获取我的令牌并将其传递给一些需要令牌的 javascript 报告库。当我在没有自定义 AuthenticationStateProvider 的情况下使用普通安全性时,一切正常,但是当我切换到自定义 AuthenticationStateProvider 时,注入 IAccessTokenProvider 时出现错误

public class MockAuthenticationStateProvider : AuthenticationStateProvider
{
    public IOptionsMonitor<WasmMockClaimsPrincipalOptions> MockClaimsPrincipalOptions { get; }
    public MockAuthenticationStateProvider(IOptionsMonitor<WasmMockClaimsPrincipalOptions> options)
    {
        MockClaimsPrincipalOptions = options;
    }
    //https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-5.0
    public override Task<AuthenticationState> GetAuthenticationStateAsync()
    {
        var mcp = MockClaimsPrincipalOptions.CurrentValue;

        if (!string.IsNullOrEmpty(mcp.Selected))
        {
            var claims = mcp.Pool[mcp.Selected].ToClaimEnumerable();
            var identity = new ClaimsIdentity(claims, "mockAuth", "username", "role");
            var user = new ClaimsPrincipal(identity);
            return Task.FromResult(new AuthenticationState(user));
        }
        var anonymous = new ClaimsIdentity();
        return Task.FromResult(new AuthenticationState(new ClaimsPrincipal(anonymous)));
    }
}

services.AddScoped<AuthenticationStateProvider, MockAuthenticationStateProvider>();
services.AddAuthorizationCore();

services.AddAuthorizationCore(options =>
{
     options.AddAuthorizationPolicies(config);
});

 services.AddOidcAuthentication(options =>
 {
            config.Bind($"{configKey}:Provider", options.ProviderOptions);
            config.Bind($"{configKey}:User", options.UserOptions);
  });

暴击:Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的异常呈现组件:指定的强制转换无效。 System.InvalidCastException:指定的强制转换无效。 在 Microsoft.Extensions.DependencyInjection.WebAssemblyAuthenticationServiceCollectionExtensions.c__03[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=5.0.9.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=5.0.9.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=5.0.9.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].&lt;AddRemoteAuthentication&gt;b__0_1(IServiceProvider sp) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext,Microsoft.Extensions.DependencyInjection,版本=5.0.0.1,Culture=neutral,PublicKeyToken=adb9793829ddae60],[ System.Object,System.Private.CoreLib,版本=5.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite,RuntimeResolverContext 参数) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite,RuntimeResolverContext 上下文,ServiceProviderEngineScope serviceProviderEngine,RuntimeResolverLock lockType) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite,RuntimeResolverContext 上下文) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeServiceProviderEngine.&lt;&gt;c__DisplayClass1_0.&lt;RealizeService&gt;b__1(ServiceProviderEngineScope p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeServiceProviderEngine.&lt;&gt;c__DisplayClass1_0.&lt;RealizeService&gt;b__0(ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) at Microsoft.AspNetCore.Components.ComponentFactory.&lt;&gt;c__DisplayClass6_0.&lt;CreateInitializer&gt;g__Initialize|2(IServiceProvider serviceProvider, IComponent component) at Microsoft.AspNetCore.Components.ComponentFactory.PerformPropertyInjection(IServiceProvider serviceProvider, IComponent instance) at Microsoft.AspNetCore.Components.ComponentFactory.InstantiateComponent(IServiceProvider serviceProvider, Type componentType) at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateComponent(Type componentType) at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateChildComponentOnFrame(RenderTreeFrame&amp; frame, Int32 parentComponentId) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame(DiffContext&amp; diffContext, Int32 frameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext&amp; diffContext, Int32 frameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext&amp; diffContext, Int32 newFrameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext&amp; diffContext, Int32 oldFrameIndex, Int32 newFrameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext&amp; diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext&amp; diffContext, Int32 oldFrameIndex, Int32 newFrameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext&amp; diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext&amp; diffContext, Int32 oldFrameIndex, Int32 newFrameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext&amp; diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext&amp; diffContext, Int32 oldFrameIndex, Int32 newFrameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext&amp; diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext&amp; diffContext, Int32 oldFrameIndex, Int32 newFrameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext&amp; diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.ComputeDiff(Renderer renderer, RenderBatchBuilder batchBuilder, Int32 componentId, ArrayRange1 oldTree, ArrayRange`1 newTree) 在 Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder,RenderFragment renderFragment) 在 Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry) 在 Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()

【问题讨论】:

  • 你能显示自定义AuthenticationStateProvider的代码吗?
  • 我在原帖中添加了代码。谢谢
  • 例外情况是关于RemoteAuthenticationState。你也有那个代码吗?
  • RemoteAuthenticationState 是 IAccessTokenProvider 的默认实现。它是由 .Net 注入的

标签: c# .net blazor-webassembly


【解决方案1】:

试试这个https://stackoverflow.com/a/62798071 与 Duende 和自定义 AuthenticationStateProvider 有完全相同的问题。

【讨论】:

    猜你喜欢
    • 2021-04-30
    • 2020-10-29
    • 2021-03-02
    • 2020-04-18
    • 2020-05-01
    • 2021-05-14
    • 2023-01-19
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多