【问题标题】:Token from identity server connection\token is not valid for my API来自身份服务器连接\令牌的令牌对我的 API 无效
【发布时间】:2017-10-25 23:29:55
【问题描述】:

我通过带有 POST 请求的 url 连接/令牌从身份服务器 4 获取令牌:

然后我将 access_token 键的值作为标头复制/粘贴到我的 API GET 请求中:

我的令牌字符串

eyJhbGciOiJSUzI1NiIsImtpZCI6IkYxMDhCODA2NUNFMTRBOEEwOTZBODUyMkIxQUNBMkFDMTdEQjQwNEEiLCJ0eXAiOiJKV1QiLCJ4NXQiOiI4UWk0Qmx6aFNvb0phb1Vpc2F5aXJCZmJRRW8ifQ.eyJuYmYiOjE1MDg1OTU5MzIsImV4cCI6MTUwODU5OTUzMiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo2NTUzNSIsImF1ZCI6WyJodHRwOi8vbG9jYWxob3N0OjY1NTM1L3Jlc291cmNlcyIsInRlYWNoZXJzX3Rlc3RfcGxhbm5lciJdLCJjbGllbnRfaWQiOiJ0ZWFjaGVyc190ZXN0X3BsYW5uZXIiLCJzY29wZSI6WyJ0ZWFjaGVyc190ZXN0X3BsYW5uZXIiXX0.g2x31JcYrXyIavfxCu7UKY3kndznI_gYHJYCxl0dQn3u7l7vWo6qKr13XYMo6P1Lqtu68T2FEXL-5kyS0XwFClpdJE6m13-hfKZsd2QHBmOlgZ2ANwghXW4hfU5nWiwkUACwkP9wfDCULV3oQm5i49L5TQmUiiqcy0TTS2FDBdS5ymFBi1bCKnPh5ErsD8V_4eTqLzxv8CyVkPx2gPd6aBIf_2JNrjrMrrm69kghOHnktVG17KPQhppbIeJO8RP-URiJUJGXIY09yRGVF7YXtkFj-I5QOMvNIAWgUeqNYqH0cuQol9nglA4mtU1MfXtnRoEpRRzGViw7gxJ_-MFadA

授权:承载mytokenstring

什么会导致来自身份服务器的令牌对我的 API 无效?

POSTMAN

出现 401 错误

查看红隼服务器的输出我得到这个:

Api> fail: Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware[0]
Api>       'MS-ASPNETCORE-TOKEN' does not match the expected pairing token '52da49ee-6599-483a-b97a-15ced1603005', request rejected.

我做错了什么,那是什么配对令牌 Guid?

API HttpGet:

标题:

授权承载 eyJh...UntilTheEndOfTheString

IdentityServer 设置:

public void ConfigureServices(IServiceCollection services)
{
    string certificateFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "certifiateselfsigned.pfx");

    var certificate = new X509Certificate2(certificateFilePath, "test");

    services.AddIdentityServer()
    .AddSigningCredential(certificate)
        .AddInMemoryApiResources(InMemoryConfiguration.GetApiResources())
        .AddInMemoryClients(InMemoryConfiguration.GetClients())
        .AddTestUsers(InMemoryConfiguration.GetUsers());

    services.AddMvc();
}

更新

API

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(options =>
    {
        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;

        })
            .AddIdentityServerAuthentication(opt =>
           {
               opt.RequireHttpsMetadata = false;
               opt.Authority = "http://localhost:65535"; // IdentityProvider => port running IDP on
               opt.ApiName = "teachers_test_planner"; // IdentityProvider => api resource name
           });

    services.AddMvc();
}

身份提供者

public static class InMemoryConfiguration
{
    public static List<TestUser> GetUsers()
    {
        return new List<TestUser>
    {
        new TestUser{ SubjectId = "6ed26693-b0a1-497e-aa14-7b880536920f", Username = "orders.tatum@gmail.com", Password = "mypassword",
            Claims = new List<Claim>
            {
                new Claim("family_name", "tatum")
            }
        }
    };
    }

    public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>
    {
        new ApiResource("teachers_test_planner", "Mein Testplaner")
    };
    }

    public static IEnumerable<IdentityResource> GetIdentyResources()
    {
        return new List<IdentityResource>
    {
        new IdentityResources.OpenId(),
        new IdentityResources.Profile(),
    };
    }

    public static IEnumerable<Client> GetClients()
    {
        return new List<Client>
    {
        new Client
        {
            ClientId = "teachers_test_planner",
            ClientSecrets = new[]{ new Secret("secret".Sha256()) },
            AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
            AllowedScopes = new []{ "teachers_test_planner" }
        }
    };
    }
}

更新 2

你可以在这里找到测试项目:

https://github.com/LisaTatum/IdentityServer4Test

更新 3

没有人问我如何将 http_post 发送到连接/令牌端点:

【问题讨论】:

  • 你的 POST 返回的 json 是什么?然后你在 POST 中使用什么来获取 GET?
  • 更新了更多信息。在此处检查令牌:jwt.io/#debugger ;-)
  • 除了该 access_token 的引号之外,您是否传递了所有内容?
  • 不带引号是的!问题也不是到期时间......我的疯狂猜测是这样的:github.com/IdentityServer/IdentityServer3/issues/703 引用:“好的,您的错误表明身份服务器使用的 SSL 证书不被信任用于来自 Web API 项目的令牌验证IdentityServer。IOW,您需要建立对您的 SSL 证书的机器信任(不仅仅是在浏览器中单击“忽略 SSL 错误”)。”
  • 抱歉这些愚蠢的问题,但需要确定。邮递员会发生这种情况吗?以及您创建的应用?

标签: c# oauth-2.0 openid identityserver4


【解决方案1】:

您错过的只是app.UseAuthentication()。您必须将此添加到 Api startup.cs 上的 Configure 方法中

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(); // Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseAuthentication();// The missing line

    app.UseStaticFiles();

    app.UseMvc();
}

我编写了以下控制台应用程序来测试调用您的 api

class Program
{
    public static void Main(string[] args) => MainAsync().GetAwaiter().GetResult();

    private static async Task MainAsync()
    {
        // discover endpoints from metadata
        var disco = await DiscoveryClient.GetAsync("http://localhost:65535");
            
        // request token
        var tokenClient = new TokenClient(disco.TokenEndpoint, "teachers_test_client", "secret");
        var tokenResponse = await tokenClient.RequestClientCredentialsAsync("teachers_test_planner");

        if (tokenResponse.IsError)
        {
            Console.WriteLine(tokenResponse.Error);
            return;
        }

        Console.WriteLine(tokenResponse.Json);
        Console.WriteLine("\n\n");

        // call api
        var client = new HttpClient();
        client.SetBearerToken(tokenResponse.AccessToken);

        var response = await client.GetAsync("http://localhost:52129/api/values/1");
        if (!response.IsSuccessStatusCode)
        {
            Console.WriteLine(response.StatusCode);
        }
        else
        {
            var content = await response.Content.ReadAsStringAsync();
            Console.WriteLine(content);
        }
        Console.ReadKey();
    }
}

你可以试试邮递员,我没试过

但是,我不得不对您的项目 (github) 进行一些更改,以使其在我的机器上运行。他们是

项目:IdentityProvider

  • mytestplanner.pfx 未设置为始终复制

项目:Api2

  • 卸载 IdentityServer4 包。为什么客户端需要 IdentityServer4?
  • 将目标框架从&lt;TargetFramework&gt;net462&lt;/TargetFramework&gt;改为&lt;TargetFramework&gt;netcoreapp2.0&lt;/TargetFramework&gt;,(原因是我的机器上没有安装4.6.2)

不管怎样,让我知道这是否适合你

更新

我已经添加了工作项目here

【讨论】:

  • 我在我的生产解决方案上试用了它,它在那里工作,谢谢。用邮递员测试。我只是我添加了 AddIdentityServerAuthentication 而我没有 app.UseAuthentication... 其他更正是由于从生产到 github 测试解决方案的复制/粘贴错误:-)
  • 很高兴它对您有用。 @Elisabeth 感谢赏金,这是我获得的第一个赏金 :)
【解决方案2】:

如果您请求“错误”令牌,就会发生这种情况。

假设您有客户端 C 调用 API A,“POST GetToken”将如下所示:

  • ClientId:C.ClientId
  • 资源:A.Resource(“观众”)

您的令牌表明您正在执行以下请求

http://localhost:65535/resources”是您的目标 API 的受众吗?

【讨论】:

  • 抱歉,Alex 我以为我发布了所有代码...我已经用更多信息更新了我的问题。
  • 看看这是否可以解决您日志中的红隼错误:stackoverflow.com/questions/38839034/… 您也可以再次添加您的示例令牌吗?还可以尝试重命名您的客户端 Teachers_test_client,同时保持范围和 api 相同。最后,尝试从您的设置中删除证书以减少可能的错误来源:.AddSigningCredential
  • 再次添加令牌。将 clientId 重命名为“teachers_test_client”。使用 AddSigningCredential 删除证书并收到此错误:

    InvalidOperationException:未配置签名凭据。无法创建 JWT 令牌

  • 今晚我会把测试项目上传到github :-)
猜你喜欢
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2016-10-19
  • 2015-10-19
  • 1970-01-01
  • 2020-11-22
  • 2014-12-01
相关资源
最近更新 更多