小编Man*_*ngh的帖子

.Net Core 自定义身份验证使用 API 密钥和 Identity Server 4

我有一个使用 JWT 令牌进行身份验证的 .NET Core 2.2 Web API。令牌由 Identity Server 4 在单独的 API 上生成。

所有身份验证和授权都可以使用 JWT 令牌按预期工作。但我需要扩展它以允许使用 API 密钥。如果提供了 API 密钥,我想加载该特定用户的声明,将其添加到请求中并让 Authorize 属性处理设置的策略。

以下是我根据此处的建议到目前为止所做的工作。我的错误与链接的帖子完全相同,它也适用于我,并使用 GenericPrincipal 和一组角色,但我使用的是 AuthorisationPolicies 并且我当前的实现总是出现 401 错误,给我类似于上面链接的错误。

启动文件

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvcCore(options =>
        {
            options.Filters.Add(new RequireHttpsAttribute());
            options.Filters.Add(new AuthorizeFilter());
            options.Filters.Add(typeof(ValidateModelStateAttribute));
            options.AllowEmptyInputInBodyModelBinding = true;
        })
        .AddAuthorization(options =>
        {
            options.AddPolicies();
        })
        .AddJsonFormatters();

        services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = Configuration["Authentication:Authority"];
                options.RequireHttpsMetadata = true;
                options.ApiName = Configuration["Authentication:ApiName"];
            });
        services.AddCors();
    }

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if …
Run Code Online (Sandbox Code Playgroud)

c# jwt asp.net-identity asp.net-core identityserver4

8
推荐指数
1
解决办法
2662
查看次数

ILogger.LogError 未记录异常详细信息

我有一个函数应用程序(2.0 版)并试图记录异常的堆栈跟踪。

     try
        {
            processedOrder = await orderProcessingService.ProcessOrder(order);
        }
        catch (Exception ex)
        {
            log.LogError(ex, $"Error processing order: {order.Id}");
        }
Run Code Online (Sandbox Code Playgroud)

这仅记录 App Insights 中的消息,但不记录传递的异常对象。

我这样做对吗?

如果我这样做, log.LogError(ex, $"Error processing order: {order.Id}", ex)那么我确实会看到异常消息,但不会看到堆栈跟踪。

azure azure-application-insights azure-functions

6
推荐指数
1
解决办法
2437
查看次数