首页 > 解决方案 > 可以使用客户端登录,但不能发出 API 请求。401

问题描述

我有引用 IdentityServer3 库的 .Net Framework Web API(旧)

            app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
            {
                Authority = "https://localhost:5001",
                ValidationMode = ValidationMode.Local,
                RequiredScopes = new[] { "api1" }
            });

身份服务器5:

new Client { ClientId = "mvc",
                    ClientSecrets = { new Secret("secret".Sha256()) },
                    AllowedGrantTypes = GrantTypes.Code,
                    RedirectUris = { "https://localhost:5002/signin-oidc" },
                    PostLogoutRedirectUris = { "https://localhost:5002/signout-callback-oidc" },
                    AllowedScopes = new List<string> { OpenId, Profile }
                }, new Client { ClientId = "client",
                    AllowedGrantTypes = GrantTypes.ClientCredentials, 
                    ClientSecrets = { new Secret("secret".Sha256()) },
                    AllowedScopes = { "api1" }}

有效的网络客户端:

services.AddAuthentication(options =>
                {   options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc"; })
                .AddCookie("Cookies")
                .AddOpenIdConnect("oidc", options =>
                {   options.Authority = "https://localhost:5001";
                    options.ClientId = "mvc";
                    options.ClientSecret = "secret";
                    options.ResponseType = "code";
                    options.SaveTokens = true;  });

但是,当我从客户端调用 API 并将其作为授权承载传递时,它不起作用。我只是无缘无故地得到一个普通的401。我错过了什么。

标签: asp.net-web-apiidentityserver5

解决方案


推荐阅读