Skip to content

Error when using auth with prerendering after trimming #44007

Closed
@Jon-Hilton

Description

@Jon-Hilton

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have a Blazor app which is prerendered and uses a third party Oidc provider for auth (Auth0).

I've encountered an issue with .NET 7 RC1 where, after trimming the app for production, I run into this error:

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: ConstructorContainsNullParameterNames, Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3+JavaScriptLoggingOptions[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState,Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount,Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions] SerializationNotSupportedParentType, System.Object Path: $.
System.NotSupportedException: ConstructorContainsNullParameterNames, Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3+JavaScriptLoggingOptions[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState,Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount,Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions] SerializationNotSupportedParentType, System.Object Path: $.
 ---> System.NotSupportedException: ConstructorContainsNullParameterNames, Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3+JavaScriptLoggingOptions[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState,Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount,Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions]
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_ConstructorContainsNullParameterNames(Type )
   at System.Text.Json.Serialization.Metadata.ReflectionJsonTypeInfo`1[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3.JavaScriptLoggingOptions[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],

The client app is set up to use Oidc Authentication (via Auth0) like so:

ClientApp\Program.cs

builder.Services.AddAuthorizationCore().AddOidcAuthentication(options =>
{
    builder.Configuration.Bind("Auth0", options.ProviderOptions);

    options.ProviderOptions.ResponseType = "code";
    options.ProviderOptions.AdditionalProviderParameters.Add("audience", "https://practical-dot-net.io");
});

The server app has the following configuration (to ensure the app doesn't fall over when prerendering).

ServerApp\Program.cs

...

builder.Services.AddApiAuthorization();
builder.Services.AddTransient<IJSRuntime, NullJSRuntime>();

...

Where NullJsRuntime is a simple class which does nothing (apart from returning ValueTasks).

public class NullJSRuntime : IJSRuntime
{
    public ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args)
    {
        return new ValueTask<TValue>();
    }

    public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object[] args)
    {
        return new ValueTask<TValue>();
    }
}

_Host.cshtml is configured to skip prerendering when attempting to access any Urls which begin with /authentication for good measure...

_Host.cshtml

@if (HttpContext.Request.Path.StartsWithSegments("/authentication"))
{
    <component type="typeof(App)" render-mode="WebAssembly" />
}

else
{
    <component type="typeof(App)" render-mode="WebAssemblyPrerendered" />
}

If I publish this (with linking/trimming enabled) and run the app I get the error mentioned above.

Expected Behavior

The app runs without error, after linking, when prerendering and integrating with a third party auth provider.

Steps To Reproduce

Clone https://github.com/Jon-Hilton/BlazorPreRenderAuthIssue

Enter valid OIDC server values for Authority and ClientId in Client\wwwroot\appsettings.json:

https://github.com/Jon-Hilton/BlazorPreRenderAuthIssue/blob/master/Client/wwwroot/appsettings.json

You may also need to update the audience as configured in Client\Program.cs here:

https://github.com/Jon-Hilton/BlazorPreRenderAuthIssue/blob/fce45fc9533200dfe8a4b0844a8b8d5bc64bf81f/Client/Program.cs#L15

With OIDC configured, now:

  • Publish BlazorApp1.Server
  • Launch the published app
  • Observe error in console

Exceptions (if any)

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: ConstructorContainsNullParameterNames, Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService3+JavaScriptLoggingOptions[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState,Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount,Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions] SerializationNotSupportedParentType, System.Object Path: $. System.NotSupportedException: ConstructorContainsNullParameterNames, Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService3+JavaScriptLoggingOptions[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState,Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount,Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions] SerializationNotSupportedParentType, System.Object Path: $.
---> System.NotSupportedException: ConstructorContainsNullParameterNames, Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService3+JavaScriptLoggingOptions[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState,Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount,Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions] at System.Text.Json.ThrowHelper.ThrowNotSupportedException_ConstructorContainsNullParameterNames(Type ) at System.Text.Json.Serialization.Metadata.ReflectionJsonTypeInfo1[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService3.JavaScriptLoggingOptions[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]], Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].GetParameterInfoValues() at System.Text.Json.Serialization.Metadata.JsonTypeInfo.Configure() at System.Text.Json.Serialization.Metadata.JsonTypeInfo.<EnsureConfigured>g__ConfigureLocked|138_0() --- End of stack trace from previous location --- at System.Text.Json.Serialization.Metadata.JsonTypeInfo.<EnsureConfigured>g__ConfigureLocked|138_0() at System.Text.Json.Serialization.Metadata.JsonTypeInfo.EnsureConfigured() at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type , Boolean , Boolean ) at System.Text.Json.WriteStackFrame.InitializePolymorphicReEntry(Type , JsonSerializerOptions ) at System.Text.Json.Serialization.JsonConverter.ResolvePolymorphicConverter(Object , JsonTypeInfo , JsonSerializerOptions , WriteStack& ) at System.Text.Json.Serialization.JsonConverter1[[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryWrite(Utf8JsonWriter , Object& , JsonSerializerOptions , WriteStack& )
at System.Text.Json.Serialization.Converters.ArrayConverter2[[System.Object[], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnWriteResume(Utf8JsonWriter , Object[] , JsonSerializerOptions , WriteStack& ) at System.Text.Json.Serialization.JsonCollectionConverter2[[System.Object[], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnTryWrite(Utf8JsonWriter , Object[] , JsonSerializerOptions , WriteStack& )
at System.Text.Json.Serialization.JsonConverter1[[System.Object[], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryWrite(Utf8JsonWriter , Object[]& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.Serialization.JsonConverter1[[System.Object[], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].WriteCore(Utf8JsonWriter , Object[]& , JsonSerializerOptions , WriteStack& )
Exception_EndOfInnerExceptionStack
at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& , NotSupportedException )
at System.Text.Json.Serialization.JsonConverter1[[System.Object[], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].WriteCore(Utf8JsonWriter , Object[]& , JsonSerializerOptions , WriteStack& ) at System.Text.Json.JsonSerializer.WriteCore[Object[]](Utf8JsonWriter , Object[]& , JsonTypeInfo1 )
at System.Text.Json.JsonSerializer.WriteString[Object[]](Object[]& , JsonTypeInfo1 ) at System.Text.Json.JsonSerializer.Serialize[Object[]](Object[] , JsonSerializerOptions ) at Microsoft.JSInterop.JSRuntime.InvokeAsync[IJSVoidResult](Int64 , String , CancellationToken , Object[] ) at Microsoft.JSInterop.JSRuntime.<InvokeAsync>d__161[[Microsoft.JSInterop.Infrastructure.IJSVoidResult, Microsoft.JSInterop, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime , String , Object[] )
at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService3.<EnsureAuthService>d__29[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext() at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService3.d__20[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticatorViewCore1.<ProcessLogIn>d__84[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext() at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticatorViewCore1.d__83[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

.NET Version

7.0.100-rc.1.22431.12

Anything else?

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions