Проверка подлинности ASP.NET Core прервана после обновления до 3.0 - PullRequest
1 голос
/ 25 сентября 2019

У меня есть следующий код, который отлично работал в ASP.NET Core 2.2 (отредактирован для удаления имен для конкретного продукта)

public class Type1AuthenticationOptions : AuthenticationSchemeOptions {
   public string Secret { get; set; }
}

public class Type1AuthenticationHandler : AuthenticationHandler<Type1AuthenticationOptions> {

  public Type1AuthenticationHandler (IOptionsMonitor<Type1AuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
        : base(options, logger, encoder, clock)
    { }

    protected override Task<AuthenticateResult> HandleAuthenticateAsync() {
        ..
    }
}
// Repeat for Type2AuthenticationHandler

Мои контроллеры были аннотированы таким образом:

[Route("api/thing")]
[Authorize(AuthenticationSchemes = "type1")]
public class ThingController : Controller {
   ...
}

[Route("api/thing2")]
[Authorize(AuthenticationSchemes = "type2")] // different way of authorizing for thing2
public class Thing2Controller : Controller {
   ...
}

Как и выше, все это прекрасно работало в Asp.NET Core 2.2, а также в предыдущей версии 2.1, и я думаю, что 2.0.Ключевые части (Configure и ConfigureServices приведены ниже)

При обновлении до Asp.NET Core 3.0 код все еще компилируется - все классы, атрибуты и структура все еще существуют, однако мой обработчик аутентификации никогда не вызывается.Если я поставлю точку останова в конструкторе Type1AuthenticationHandler, он даже не попадет.

Весь мой метод метода Configure () выглядит следующим образом:

public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseAuthentication();
    app.UseAuthorization();

    app.UseRouting();

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

    if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
    }

    // Middleware to set the no-cache header value on ALL requests
    app.Use((context, next) => {
        context.Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue { NoCache = true };
        return next();
    });

И мой ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

    services.AddControllers() // no views to be had here. Turn it off for security
        .AddNewtonsoftJson(opts => {
            opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            opts.SerializerSettings.Converters.Add(new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() });
        });

    services
        .AddAuthentication() // no default scheme, controllers must opt-in to authentication
        .AddScheme<Type1AuthenticationOptions, Type1AuthenticationHandler>("type1", o =>
        {
            o.Secret = Configuration.GetThing1Secret();
        })
        .AddScheme<Type2AuthenticationOptions, Type2AuthenticationHandler>("type2", o =>
        {
            o.Secret = Configuration.GetThing2Secret();
        });

    services.AddAuthorization();

    services.AddDbContext<DatabaseContext>(
        options => options.UseNpgsql(Configuration.GetDatabaseConnectionString()),
        ServiceLifetime.Transient);

    // Used for fire and forget jobs that need access to the context outside of the
    // the HTTP request that initiated the job
    services.AddTransient(provider =>
        new Func<DatabaseContext>(() => {
            var options = new DbContextOptionsBuilder<DatabaseContext>()
                .UseNpgsql(Configuration.GetDatabaseConnectionString())
                .UseLoggerFactory(provider.GetRequiredService<ILoggerFactory>())
                .Options;

            return new DatabaseContext(options);
        })
    );

    // make the configuration available to bits and pieces that may need it at runtime.
    // Note: Generally it's bad practice to use this, you should configure your thing here, and inject the configured-thing instead
    services.AddSingleton(Configuration);

    services.AddMemoryCache();

    // some more app-specific singleton services added but not relevant here
}

Любая помощь очень ценится

PS на дальнем плане, вот мой csproj

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
        <UserSecretsId>99f957da-757d-4749-bd65-2b86753821a6</UserSecretsId>

        <!-- SonarQube needs this but it's otherwise irrelevant for dotnet core -->
        <ProjectGuid>{39054410-1375-4163-95e9-00f08b2efe2e}</ProjectGuid>
    </PropertyGroup>

    <PropertyGroup>
        <LangVersion>latest</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        <Folder Include="wwwroot\" />
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="AWSSDK.S3" Version="3.3.104.31" />
        <PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.3.101.68" />
        <PackageReference Include="AWSSDK.SimpleSystemsManagement" Version="3.3.106.12" />
        <PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="3.0.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0" PrivateAssets="all">
        <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
        <PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.100.1" />
        <PackageReference Include="AWSSDK.SimpleEmail" Version="3.3.101.50" />
        <PackageReference Include="NLog" Version="4.6.7" />
        <PackageReference Include="NLog.Web.AspNetCore" Version="4.8.5" />
        <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.0-preview9" />
        <PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
        <PackageReference Include="System.IO.FileSystem.DriveInfo" Version="4.3.1" />
        <PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
        <PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.2" />
    </ItemGroup>
</Project>

1 Ответ

3 голосов
/ 25 сентября 2019

После еще одного более подробного изучения руководства по миграции я заметил следующее:

Если приложение использует функции аутентификации / авторизации, такие как AuthorizePage или [Authorize], вызовите UseAuthentication и UseAuthorization.после UseRouting (и после UseCors, если используется CORS Middleware).

Уловка (которая не упоминается в руководстве по миграции) заключается в том, что UseAuthentication / UseAuthorization должно быть AFTER UseRouting, но ДО UseEndpoints.

Теперь это работает:

app.UseRouting();

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

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

Если вы выполняете вызовы Auth после UseEndpoints, вы получаете сообщение об отсутствии промежуточного программного обеспечения

...