IdentityServer4 invalid_token Azure上的“发行者无效”,处理localhost

时间:2017-03-24 14:30:13

标签: azure asp.net-core asp.net-identity identityserver4

请帮助,我正在构建一个带有离子前端的.NET Core API。我想使用ASPNET核心身份,所以我或多或少地遵循这个例子 https://identityserver4.readthedocs.io/en/release/quickstarts/6_aspnet_identity.html

这是我在Startup.cs中的内容

// Adds IdentityServer
services.AddIdentityServer()                
    .AddTemporarySigningCredential()
    .AddInMemoryIdentityResources(Config.GetIdentityResources())
    .AddInMemoryApiResources(Config.GetApiResources())
    .AddInMemoryClients(Config.GetClients(Configuration))
    .AddAspNetIdentity<ApplicationUser>();

app.UseIdentity();
app.UseIdentityServer();
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = API_address,
    RequireHttpsMetadata = false,

    ApiName = "myAPIs"
});

和我的Config.cs文件中的内存配置

public class Config
{
    // scopes define the resources in your system
    public static IEnumerable<IdentityResource> GetIdentityResources()
    {
        return new List<IdentityResource>
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
        };
    }

    // scopes define the API resources in your system
    public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>
        {
            new ApiResource(
                "myAPIs",                                       // Api resource name
                "My API Set #1",                                // Display name
                new[] { JwtClaimTypes.Name, JwtClaimTypes.Role }) // Claims to be included in access token
        };
    }

    // client want to access resources (aka scopes)
    public static IEnumerable<Client> GetClients(IConfigurationRoot configuration)
    {
        return new List<Client>
        {
            new Client
            {
                ClientId = "myClient",
                ClientName = "My Custom Client",
                AllowedCorsOrigins = new List<string>
                {
                    "whateverINeedHere"
                },
                AccessTokenLifetime = 60 * 60 * 24,
                AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                RequireClientSecret = false,
                AccessTokenType = AccessTokenType.Jwt,
                AllowedScopes =
                {
                    "myAPIs"
                }
            }
        };
    }
}

现在的问题是,当我在本地测试时,一切正常。 我点击/ connect / token端点,我得到一个令牌响应,点击需要令牌授权的控制器,我的声明就在那里。但是当我将它部署到Azure时,当我想使用令牌(从该环境发出)时,我获得401 Unauthorized with response header invalid_token“发行者无效”。我用谷歌搜索,但人们得到带有签名问题的无效令牌,而不是发行人。我以前从来没有使用过身份服务器这对我来说这看起来像是一些配置问题。我比较了我从jwt.io上的身份服务器获得的令牌,它们看起来完全一样,唯一不同的是发行者localhost - &gt; myAPIAddress。

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

您可能在客户端和IdentityServer之间遇到SSL / TLS问题,您是否能够从IdentityServer本身查看记录的异常?你可能会看到类似的东西:

"... Could not establish trust relationship for the SSL/TLS..."

如果您在HTTPS上运行IdentityServer,则需要确保您的证书中已包含其域/子域。

无论哪种方式,IdentityServer都会记录大量有用的信息,因此请打开日志并查看它的内容,这应该指向正确的方向。