首页 > 解决方案 > 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;
    });

任何建议都会有所帮助。

标签: asp.net-mvcasp.net-coreasp.net-web-api2identityserver4

解决方案


也许为时已晚,但其他人可能会提出这个问题。解决方案是LocalApi使用IdentityServer4. 您创建一个LocalApi并使用自定义范围授权它,您的客户应添加该范围,即IdentityServerApi. 完整资源可在此处获得: https ://docs.identityserver.io/en/latest/topics/add_apis.html


推荐阅读