gpt4 book ai didi

azure - 证书未经过身份验证,但 Azure 应用服务上的 asp.net 5 请求仍然成功

转载 作者:行者123 更新时间:2023-12-02 23:59:04 26 4
gpt4 key购买 nike

我正在尝试在此处为我的服务器 api 启用客户端证书身份验证:

https://learn.microsoft.com/en-us/aspnet/core/security/authentication/certauth?view=aspnetcore-5.0

我看到的问题是证书是由客户端发送的(根据 Azure 应用服务设置的要求),但即使我故意调用 context.Fail,请求始终会被处理并返回 200。我猜我可能错过了一些基本的东西 - 我对几乎所有服务器端 .NET 都是全新的。感谢您的浏览!

    public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme).
AddCertificate(options =>
{
options.AllowedCertificateTypes = CertificateTypes.All;
options.Events = new CertificateAuthenticationEvents
{
OnCertificateValidated = context => {
context.Fail("FAIL!!!");
_logger.LogWarning("OnCertificateValidated!!!");
return Task.CompletedTask; },
OnAuthenticationFailed = context => {
context.Fail("BAD cert. BAD!");
_logger.LogWarning("OnAuthenticationFailed!!!");
return Task.CompletedTask; }
};
}).
AddCertificateCache();


services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddConsole();
loggingBuilder.AddDebug();
loggingBuilder.AddAzureWebAppDiagnostics();
});

services.AddControllers();
}

并配置

        private static ILogger<Startup> _logger;

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
_logger = logger;

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

app.UseHttpsRedirection();

app.UseRouting();

app.UseCertificateForwarding();
app.UseAuthentication();

app.UseAuthorization();

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

}

在我的 Azure 应用服务日志流中,我看到

2021-04-21 05:42:07.759 +00:00 [Warning] MyApi.Startup: OnCertificateValidated!!!!!
2021-04-21 05:42:07.759 +00:00 [Information] Microsoft.AspNetCore.Authentication.Certificate.CertificateAuthenticationHandler: Certificate was not authenticated. Failure message: FAIL!!!

如果我将应用服务配置为不允许证书,我会收到不同的日志,但请求仍然存在

2021-04-21 05:27:12.119 +00:00 [Debug] Microsoft.AspNetCore.Authentication.Certificate.CertificateAuthenticationHandler: No client certificate found.
2021-04-21 05:27:12.120 +00:00 [Debug] Microsoft.AspNetCore.Authentication.Certificate.CertificateAuthenticationHandler: AuthenticationScheme: Certificate was not authenticated.

但是,在所有情况下,请求都会成功,而上面的链接文档似乎表明我应该看到 403(禁止)结果 - 当我没有发送证书并且 Azure 配置设置为需要证书时,我就这样做了。这是我唯一能让它失败的时候。

我发现我也许可以使用此处描述的方法 - 检索请求 header 并完全由我自己解析和验证它。但上面的方法不应该起作用吗? https://learn.microsoft.com/en-us/azure/app-service/app-service-web-configure-tls-mutual-auth

最佳答案

我也遇到过这个问题。我的问题是我没有声明任何 Controller 路由需要身份验证。

根据您的用例,有两种可能的修复方法:

为所有路由启用身份验证

// Require auth by default for all routes
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});

使用每个 the docs 的属性打开特定 Controller /操作的身份验证

[Authorize]
public class AccountController : Controller
{
public ActionResult Login()
{
}

public ActionResult Logout()
{
}
}

关于azure - 证书未经过身份验证,但 Azure 应用服务上的 asp.net 5 请求仍然成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67190032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com