IsAuthenticate is false for servicestack calls but true for mvc controllers

Viewed 46

I've setup .Net Core so that I can successfully login and get access to an MVC API controller behind the Microsoft.AspNetCore.Authorization [Authorize()] attribute and see the logged in identity.

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication()
                .AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options))
                .AddAzureADB2CBearer(options => Configuration.Bind("AzureAdB2C", options));

            services.AddAuthorization(options =>
            {
                options.DefaultPolicy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .AddAuthenticationSchemes(AzureADB2CDefaults.AuthenticationScheme, AzureADB2CDefaults.BearerAuthenticationScheme)
                .Build();
            });

            services.AddControllers();
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();
            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.Use(async (context, next) =>
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write("Auth Info: ");
                Console.ResetColor();
                Console.WriteLine(context.User.Identity.IsAuthenticated ? "Logged In" : "Anon");

                await next();
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseServiceStack(new AppHost
            {
                AppSettings = new NetCoreAppSettings(Configuration)
            });
        }
    }

I then added servicestack and am able to access endpoints anonymously.

Lastly, I added the NetCoreIdentityAuthProvider to link servicestack with the .netcore identity.

        Plugins.Add(new AuthFeature(() => new AuthUserSession(),
                new IAuthProvider[] { new NetCoreIdentityAuthProvider(AppSettings) { }}));
  • With a Microsoft.AspNetCore.Authorization [Authorize()] attribute I just get an anon call.
  • With a ServiceStack [Authenticate()] attribute I get redirected to Account/Login?ReturnUrl=%2f

I was expecting that I could use the aspnet core authentication and combine that with service stack. Is this not the case?

1 Answers
Related