提问者:小点点

同一项目中的IdentityServer4和Web API


我有一个IdentityServer和一个MVC-Client,IdentityServer有自己的Web API为其客户端提供用户管理。MVC 客户端使用 HybridAndClientCredentials 授权类型与 IdentityServer 交互。我对客户端的用户进行身份验证没有问题。问题是当我尝试使用经过身份验证的用户从 IdentityServer 调用某些 API 时,服务器返回登录视图而不是 API 的结果。

这是我的客户端配置:

new Client
{
    ClientId = "mvc.identity.management",
    ClientName = "Identity Management",
    AllowedGrantTypes = GrantTypes.Hybrid,

    RequireConsent = false,

    ClientSecrets =
    {
        new Secret("somesecret".Sha256())
    },

    RedirectUris = { "http://localhost:5001/signin-oidc" },
    PostLogoutRedirectUris = { "http://localhost:5001/signout-callback-oidc" },

    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile
    }
}

和客户

    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

    services.AddAuthentication(options =>
    {
        options.DefaultScheme = "Cookies";
        options.DefaultChallengeScheme = "oidc";
    })
    .AddCookie("Cookies")
    .AddOpenIdConnect("oidc", options =>
    {
        options.SignInScheme = "Cookies";

        options.Authority = "http://localhost:5000";
        options.RequireHttpsMetadata = false;

        options.ClientSecret = "somesecret";
        options.ResponseType = "code id_token";
        options.GetClaimsFromUserInfoEndpoint = true;

        options.ClientId = "mvc.identity.management";
        options.SaveTokens = true;
    });

任何建议都会有帮助。


共1个答案

匿名用户

也许现在很晚了,但这个问题可能会出现在其他人身上。解决方案是使用< code>LocalApi,它是< code>IdentityServer4的内置功能。您创建一个< code>LocalApi,并使用自定义范围对其进行授权,您的客户端应该添加该范围,即< code>IdentityServerApi。完整的资源可从以下网址获得:https://docs . identity server . io/en/latest/topics/add _ APIs . html