IdentityServer OpenIdConnect将api添加为作用域

问题描述 投票:0回答:1

我有一个在localhost:44387上运行的项目,这是IdentityServer的配置。我有一个在localhost:44373上运行的ASP.NET Core应用程序,该应用程序充当供用户进行交互的前端应用程序,在另一个在localhost:44353上运行的ASP.NET Core应用程序,其充当API。

当用户尝试访问前端应用程序中的授权控制器时,他们将被重定向到IdentityServer的登录页面。用户登录后,将被重定向回。

然后在前端应用程序上对它们进行了授权,但是在localhost:44353上对API进行调用时,它将返回未授权的状态。

我已经尝试将范围添加到.OpenIdConnect方法,以将API添加为范围,但是重定向到登录页面时,它会使应用程序崩溃。

我如何添加API作为请求权限,因此一旦前端应用程序获得授权,便可以调用API?

这在IdentityServer的Config.cs文件中

                new Client
                {
                    ClientId = "mvc",
                    ClientName = "MVC Client",
                    AllowedGrantTypes = GrantTypes.Implicit,

                    // where to redirect to after login
                    RedirectUris = { "https://localhost:44373/signin-oidc" },

                    // where to redirect to after logout
                    PostLogoutRedirectUris = { "https://localhost:44373/signout-callback-oidc" },
                    AllowedScopes =
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        "roles",
                        "staff_api" // <---- Add staff api as scope
                    },
                    RequireConsent = false,
                }

在前端应用程序的启动内部

services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = "oidc";
            })
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
                 .AddOpenIdConnect("oidc", options =>
                 {
                     options.Authority = baseAuthAddress;
                     options.RequireHttpsMetadata = false;

                     options.ClientId = "mvc";
                     options.SaveTokens = true;
                     options.GetClaimsFromUserInfoEndpoint = true;

                     //options.Scope.Add("staff_api"); <--- THIS MAKES IT CRASH?
                     options.Scope.Add("roles");

                     // Fix for getting roles claims correctly :
                     options.ClaimActions.MapJsonKey("role", "role", "role");

                     options.TokenValidationParameters.NameClaimType = "name";
                     options.TokenValidationParameters.RoleClaimType = "roles";
                 });

API的内部Startup.cs

services.AddAuthentication("Bearer")
                .AddJwtBearer("Bearer", options =>
                 {
                     options.Audience = "staff_api"; ;
                     options.Authority = Configuration["AuthURL"];

                 });
c# asp.net api scope identityserver4
1个回答
0
投票

您是否已在IdentityServer端添加并植入了ApiResource

quickstarts中所示?由于我们没有看到完整的Config.cs文件,因此这是第一件事。您还应该查看IS4的.well-known/openid-configuration,以查看api的作用域是否已在scopes_supported部分中注册(也请参见指向快速入门的链接)。

IdentityServer的调试输出TokenValidation-和客户端上的AuthenticationMiddleware非常冗长,您应检查调试输出中是否有条目告知您什么不起作用。

如果不是Asp.Net Core应用程序,也不要使用GrantTypes.ImplicitSPA,此类型仅适用于基于JS的前端,并且不太安全。

© www.soinside.com 2019 - 2024. All rights reserved.