首页 > 解决方案 > 身份服务器承载未经过身份验证

问题描述

我有一个运行 Identity Server 4 的 dockerised .NET Core 项目,该项目可以正确地创建帐户、发出令牌并在其自己的容器中验证请求。我将其称为我的 AuthAPI

我的问题是当我尝试使用我的 AuthAPI 验证其他 API 上的令牌时

我有一个 API,我会调用我的 CustomerAPI。我正在尝试使用 AuthAPI 上的身份服务器来授权我的端点,但到目前为止我还没有运气

我测试此功能的方式是通过以下步骤:

  1. 在 AuthAPI 上创建一个帐户
  2. 确认 AuthAPI 帐户电子邮件
  3. 在 AuthAPI 上进行身份验证以检索我的承载令牌
  4. 向 AuthAPI 发出请求以获取要在我的 CustomerAPI 端点路由中使用的帐户 ID
  5. 将此 Bearer 令牌放入 Authorization Header 并使用 Authorize 属性向 CustomerAPI 端点发出请求

我使用我的 Bearer Token 对 AuthAPI 的授权调用验证并成功返回。我可以在我的 Identity Server 调试日志中看到以下短语:

AuthenticationScheme: Bearer was successfully authenticated.

当我尝试为我的 CustomerAPI 端点使用相同的承载令牌时,我得到以下调试输出:

AuthenticationScheme: Bearer was not authenticated.

我认为这是由于我的 CustomerAPI 配置失败,但尚未设法找到问题所在,这是我的配置:

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;                
            })
            .AddJwtBearer(o =>
            {
                o.Authority = "http://authapi/";
                o.Audience = "http://authapi/";
                o.RequireHttpsMetadata = false;
            });

任何帮助或指导将不胜感激,谢谢!

--- 编辑 --- 请求的配置文件:

    public class Config
    {
        public static IEnumerable<IdentityResource> GetIdentityResource()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Email(),
                new IdentityResources.Profile()
            };
        }

        public static IEnumerable<ApiResource> GetApiResources()
        {
            return new List<ApiResource>
            {
                new ApiResource("customerapi", "Customer API")
                {
                    Scopes = {new Scope("api.read")}
                }
            };
        }

        public static IEnumerable<Client> GetClients()
        {
            return new []
            {
                new Client
                {
                    RequireConsent = false,
                    ClientId = "demo_site",
                    ClientName = "Demo Site",
                    AllowedGrantTypes = GrantTypes.Implicit,
                    AllowedScopes = {"openid", "email", "profile", "api.read"},
                    RedirectUris = {"http://localhost:4200/auth-callback"},
                    PostLogoutRedirectUris = {"http://localhost:4200/"},
                    AllowedCorsOrigins = {"http://localhost:4200"},
                    AllowAccessTokensViaBrowser = true,
                    AccessTokenLifetime = 3600
                }
            };
        }
    }

标签: c#authenticationjwtidentityserver4

解决方案


推荐阅读