IdentityServer / IdentityServer4

OpenID Connect and OAuth 2.0 Framework for ASP.NET Core
https://identityserver.io
Apache License 2.0
9.23k stars 4.02k forks source link

No authentication handler is registered for the scheme 'Windows' #4729

Closed rauntska closed 4 years ago

rauntska commented 4 years ago

Hi ,

I'm trying to use windowsauthentication, but end up with an error: InvalidOperationException: No authentication handler is registered for the scheme 'Windows'. The registered schemes are: Identity.Application, Identity.External, Identity.TwoFactorRememberMe, Identity.TwoFactorUserId, idsrv, idsrv.external, Google. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("Windows",...)?

Happens when calling: var result = await HttpContext.AuthenticateAsync(AccountOptions.WindowsAuthenticationSchemeName);

Startup.cs

using System.Collections.Generic;
using IdentityServer4.Models;
using IdentityServerApi.Data;
using IdentityServerApi.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
//using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace IdentityServerApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Block 2: Add IdentityServer4 with InMemory Configuration
            Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
            services.AddControllersWithViews();

            // configures IIS out-of-proc settings (see https://github.com/aspnet/AspNetCore/issues/14882)
            services.Configure<IISOptions>(iis =>
            {
                iis.AuthenticationDisplayName = "Windows";
                iis.AutomaticAuthentication = false;
            });

            // configures IIS in-proc settings
            services.Configure<IISServerOptions>(iis =>
            {
                iis.AuthenticationDisplayName = "Windows";
                iis.AutomaticAuthentication = false;
            });
            services.AddDbContext<ApplicationDbContext>(config =>  
            {  
                // for in memory database  
                config.UseInMemoryDatabase("InMemDb");  
            });

            // AddIdentity :-  Registers the services  
            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()  
                .AddDefaultTokenProviders();  

            services.AddCors(setup =>
            {
                setup.AddDefaultPolicy(policy =>
                {
                    policy.AllowAnyHeader();
                    policy.AllowAnyMethod();
                    policy.WithOrigins("https://localhost:44386");
                    policy.AllowCredentials();
                });
            });

            var builder = services
                .AddIdentityServer(options =>
                {
                    options.Events.RaiseErrorEvents = true;
                    options.Events.RaiseInformationEvents = true;
                    options.Events.RaiseFailureEvents = true;
                    options.Events.RaiseSuccessEvents = true;
                    options.IssuerUri = "https://localhost:44323";
                })
              .AddAspNetIdentity<ApplicationUser>()
              //  .AddInMemoryIdentityResources(Config.GetIdentityResources())
                .AddInMemoryApiScopes((IEnumerable<ApiScope>) Config.GetScopes())
                .AddInMemoryApiResources((IEnumerable<ApiResource>) Config.GetApis())
                .AddInMemoryClients((IEnumerable<Client>) Config.GetClients());
         #if DEBUG
            builder.AddDeveloperSigningCredential();
        #endif

//some code is not mentioend here for the sake of brevity
            services.AddAuthentication()
                .AddGoogle(options =>
            {
                // register your IdentityServer with Google at https://console.developers.google.com
                // enable the Google+ API
                // set the redirect URI to http://localhost:5000/signin-google
                options.ClientId = "copy client ID from Google here";
                options.ClientSecret = "copy client secret from Google here";
            });;

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

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

            // Block 4:
            //  UseIdentityServer include a call to UseAuthentication
            app.UseIdentityServer();
            app.UseAuthentication();
            app.UseStaticFiles();
            app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
        }
    }
}

Best Regards, Rauno

brockallen commented 4 years ago

Sounds like you're not running out of IIS. In any event, that's not an IdentityServer issue -- it's something about your hosting app's config and thus more of a question for Microsoft support (or stack overflow).

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.