【问题标题】:Setting up a web project (ASP.NET Core 3) to be both client and ApiResource for IdentityServer将 Web 项目 (ASP.NET Core 3) 设置为 IdentityServer 的客户端和 ApiResource
【发布时间】:2020-02-07 03:52:20
【问题描述】:

我知道这样做很荒谬,而且非常愚蠢,但是如果您想同时成为 IdentityServer 的 ClientApiResource,使用引用标记?

我的想法如下:要求用户使用 OpenID Connect 进行身份验证。将凭据存储在 cookie 中,但之后不再依赖它们,而是继续执行普通 ApiResource 对引用令牌所做的事情 -> 命中内省端点,检查令牌(从 cookie 中获取)是否有效,如果是 -> 允许访问,如果不是 -> 恢复身份验证。

遗憾的是,我根本无法使上述行为起作用。我不确定哪个模式在哪里,尤其是 cookie 模式。如果我将其设置为默认模式,则授权通过,但如果令牌被拒绝,我仍然可以访问资源,因为 API 仍然将 cookie 视为对令牌的引用,而实际上令牌已经被撤销。 (内省将返回 false)

对于托管身份服务器的项目,我不需要任何配置指针。

【问题讨论】:

    标签: c# authentication asp.net-core identityserver4 openid-connect


    【解决方案1】:

    我认为这样做的方法是使用隐式授权类型,如下所示:

        new Client
                        {
                            ClientId = "Client_implicit",
                            ClientSecrets = new [] { new Secret("secret".Sha256()) },
                            AllowedGrantTypes = GrantTypes.Implicit,
                            AllowedScopes = new [] {
                                IdentityServerConstants.StandardScopes.OpenId,
                                IdentityServerConstants.StandardScopes.Profile,
                            },
                            AllowAccessTokensViaBrowser = true,
                            RedirectUris = new [] { "...Api Adress .../signin-oidc" },
                            PostLogoutRedirectUris = { "...Api Adress ../signout-callback-oidc" },
                        }
    

    关于 Api 启动类:

      services.AddAuthentication(options =>
                {
                    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                })
       .AddCookie()
       .AddOpenIdConnect(options =>
       {
           options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
           options.Authority = "...IdentityServer Adress";
           options.RequireHttpsMetadata = false;
           options.ClientId = "Client_Implicit";
           options.ClientSecret = "secret";
           options.ResponseType = "id_token token";
           options.Scope.Add("profile");
           options.Scope.Add("openid");
           options.GetClaimsFromUserInfoEndpoint = true;
           options.SaveTokens = true;
       });
    

    在您的情况下,您不需要 Api 资源,因为您的客户端也是 Api 资源

    为注销在您的 api 上创建一个用于注销的操作,包含以下内容:

     await HttpContext.SignoutAsync("Cookie");
     await HttpContext.SignoutAsync("OpenIdConnect");
    

    所以登录的场景会是这样的:

    • 用户将尝试登录
    • api 会将使用重定向到 IdentityServer
    • IdentityServer 将检查用户凭据,如果成功将 使用 (access_token) 重定向到 api

    • 用户现在登录

    注销的场景:

    • 用户将调用注销;
    • access_token 将从 cookie 和 IdentitySever 中删除

    【讨论】:

    • 我的想法正是如此——让应用程序在身份服务器项目中注册为 ApiResource以及客户端。此外,为了让事情变得更加糟糕,它必须使用 reference tokens(不是自包含的 JWT)。你所展示的只是一个普通的 Client 设置。
    猜你喜欢
    • 2017-02-28
    • 2017-07-13
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多