abp icon indicating copy to clipboard operation
abp copied to clipboard

Error in Blazor project when using custom AuthenticationStateProvider and no identity server

Open bluprints-todd opened this issue 5 years ago • 4 comments

Version: 4.2.1 Template: Blazor

Hi,

I am trying to use Jwt Token authentication in my Blazor project. Also, I have removed the identity server from my project as I do not have requirements where IDS4 would be needed. So I am trying to use my own AuthenticationStateProvider, but getting an error on startup when I run the project -

Autofac.Core.DependencyResolutionException: An exception was thrown while activating λ:Volo.Abp.IAsyncInitialize[] -> Volo.Abp.AspNetCore.Components.WebAssembly.WebAssemblyCachedApplicationConfigurationClient -> λ:Volo.Abp.Http.Client.DynamicProxying.IHttpClientProxy`1[[Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.IAbpApplicationConfigurationAppService, Volo.Abp.AspNetCore.Mvc.Contracts, Version=4.2.1.0, Culture=neutral, PublicKeyToken=null]] -> Volo.Abp.Castle.DynamicProxy.AbpAsyncDeterminationInterceptor`1[[Volo.Abp.Http.Client.DynamicProxying.DynamicHttpProxyInterceptor`1[[Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.IAbpApplicationConfigurationAppService, Volo.Abp.AspNetCore.Mvc.Contracts, Version=4.2.1.0, Culture=neutral, PublicKeyToken=null]], Volo.Abp.Http.Client, Version=4.2.1.0, Culture=neutral, PublicKeyToken=null]] -> Volo.Abp.Http.Client.DynamicProxying.DynamicHttpProxyInterceptor`1[[Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.IAbpApplicationConfigurationAppService, Volo.Abp.AspNetCore.Mvc.Contracts, Version=4.2.1.0, Culture=neutral, PublicKeyToken=null]] -> Volo.Abp.Http.Client.IdentityModel.WebAssembly.AccessTokenProviderIdentityModelRemoteServiceHttpClientAuthenticator -> λ:Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider -> XXXXX.Services.Authentication.PwAuthenticationStateProvider -> λ:System.Net.Http.HttpClient.

ConfigureServices() (in module):

 public override void ConfigureServices(ServiceConfigurationContext context)
{
    var environment = context.Services.GetSingletonInstance<IWebAssemblyHostEnvironment>();
    var builder = context.Services.GetSingletonInstance<WebAssemblyHostBuilder>();

    //ConfigureAuthentication(builder);
    ConfigureHttpClient(context, environment);
    ConfigureMudBlazor(context);
    ConfigureRouter(context);
    ConfigureUI(builder);
    ConfigureMenu(context);
    ConfigureAutoMapper(context);

    var services = context.Services;
    services.AddBlazoredLocalStorage();
    //services.AddAlwaysAllowAuthorization();
    //services.AddAuthorizationCore();
    services.AddScoped<AuthenticationStateProvider, PwAuthenticationStateProvider>();
    services.AddScoped<IAuthenticationService, AuthenticationService>();
    //services.RemoveAll(x => x.ServiceType.Name == "IAbpApplicationConfigurationAppService");
}

private void ConfigureRouter(ServiceConfigurationContext context)
{
    Configure<AbpRouterOptions>(options =>
    {
        options.AppAssembly = typeof(PwBlazorModule).Assembly;
    });
}

private void ConfigureMenu(ServiceConfigurationContext context)
{
    Configure<AbpNavigationOptions>(options =>
    {
        options.MenuContributors.Add(new PwMenuContributor(context.Services.GetConfiguration()));
    });
}

private void ConfigureMudBlazor(ServiceConfigurationContext context)
{
    context.Services.AddMudServices();
}

private static void ConfigureAuthentication(WebAssemblyHostBuilder builder)
{
    builder.Services.AddOidcAuthentication(options =>
    {
        builder.Configuration.Bind("AuthServer", options.ProviderOptions);
        options.UserOptions.RoleClaim = JwtClaimTypes.Role;
        options.ProviderOptions.DefaultScopes.Add("Patientwing");
        options.ProviderOptions.DefaultScopes.Add("role");
        options.ProviderOptions.DefaultScopes.Add("email");
        options.ProviderOptions.DefaultScopes.Add("phone");
    });
}

private static void ConfigureUI(WebAssemblyHostBuilder builder)
{
    builder.RootComponents.Add<App>("#app");
}

private static void ConfigureHttpClient(ServiceConfigurationContext context, IWebAssemblyHostEnvironment environment)
{
    //context.Services.AddTransient(sp => new HttpClient
    //{
    //    BaseAddress = new Uri(environment.BaseAddress)
    //});
    context.Services.AddHttpClient<HttpClient>(options =>
    {
        options.BaseAddress = new Uri(environment.BaseAddress);
    });
}

private void ConfigureAutoMapper(ServiceConfigurationContext context)
{
    Configure<AbpAutoMapperOptions>(options =>
    {
        options.AddMaps<PwBlazorModule>();
    });
}

NOTE: if I remove the line where I inject my own auth state provider, and uncomment ConfigureAuthentication the error goes away.

Any idea what the issue is? Does abp framework support Blazor and not using IDS4? I do not need ICurrentUser, etc., and can manage that without abp. Is there a way to turn off the out-of-the-box abp authentication in Blazor?

I have searched in issues, and looked through the documentation but do not see any info on this topic. Any help would be appreciated.

Thanks

bluprints-todd avatar Feb 27 '21 14:02 bluprints-todd

Small update on this:

Turns out if this line of code is not in the startup of the Blazor project, then the app fails to start with the error I showed above -

builder.Services.AddOidcAuthentication(options =>
        {
            builder.Configuration.Bind("AuthServer", options.ProviderOptions);
            options.UserOptions.RoleClaim = JwtClaimTypes.Role;
            options.ProviderOptions.DefaultScopes.Add("Patientwing");
            options.ProviderOptions.DefaultScopes.Add("role");
            options.ProviderOptions.DefaultScopes.Add("email");
            options.ProviderOptions.DefaultScopes.Add("phone");
        });`

And if this line of code is in the project regardless of the previous line, then it will also fail to start - services.AddScoped<AuthenticationStateProvider, PwAuthenticationStateProvider>();

Is AddOidcAuthentication() method and/or the AuthenticationStateProvider required for the abp framework to load? Is there a way around this?

Thanks

bluprints-todd avatar Feb 27 '21 22:02 bluprints-todd

The Blazor project is a front-end application, similar to Angular, it needs to be authenticated by OIDC.

maliming avatar Mar 02 '21 02:03 maliming

Thanks @maliming !

Ok, thanks for the info. Would be awesome in the future to support JWT and custom authentication state providers.

BTW, I was able to get it to work but feels like a hack. In my module, I got rid of the DependsOn AbpAspNetCoreComponentsModule or AbpAspNetCoreComponentsWebAssemblyModule, and only added the AbpUiNavigationModule & AbpAutofacWebAssemblyModule (because I was using the IMenuManager).

Then I was able to add my own auth implementation: `public override void ConfigureServices(ServiceConfigurationContext context) { var environment = context.Services.GetSingletonInstance(); var builder = context.Services.GetSingletonInstance();

        builder.Services.AddOptions();
        builder.Services.AddAuthorizationCore();
        context.Services.AddScoped<IAuthenticationService, AuthenticationService>();
        context.Services.AddScoped<AuthenticationStateProvider, PwAuthenticationStateProvider>();
    }`

I know this is probably not recommended, but I was not able to get it to work any other way.

Thanks

bluprints-todd avatar Mar 02 '21 04:03 bluprints-todd

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 01 '21 18:05 stale[bot]