c# - IdentityServer 4,试图用 fiddler 捕获流量?

标签 c# asp.net-web-api fiddler identityserver4 openid-connect

试图获得发现的控制台应用程序

var disco = await DiscoveryClient.GetAsync("http://localhost:5000");

工作正常,但我试图弄清楚它是如何工作的,但我似乎无法捕获 http 流量。

如果我使用 http://localhost.fiddler重定向到本地代理错误:

连接到 localhost.fiddler:5000/.well-known/openid-configuration 时出错:需要 HTTPS(它不是使用 HTTPS 设置的,错误消息具有误导性!)

当我们尝试使用

对 web-api 进行身份验证时,奇怪的是在代码的后面
var response = await client.GetAsync("http://localhost.fiddler:5001/identity");

localhost.fiddler 工作正常,现在它在同一个 console.app 和 program.cs 中运行,所以是同一个文件。这让我很烦,为什么我不能捕获到 5000 的流量,它是 HTTP!那么是什么原因造成的呢?是否有另一种方法来查看进出 Identity Server 的神奇 http 流量?

添加启动类

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // configure identity server with in-memory stores, keys, clients and scopes
        services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddTestUsers(Config.GetUsers());
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseIdentityServer();
    }
}

已添加 Blog ,如果我们能解决这个问题,将更新它并致谢。

最佳答案

正如您正确理解的那样,您需要使用,例如,http://localhost.fiddler , 通过 fiddler 路由本地主机流量。但是,使用 DiscoveryClient.GetAsync 使用具有默认策略的 DiscoveryClient。该默认策略具有以下对这种情况很重要的设置:

  • RequireHttps = true
  • AllowHttpOnLoopback = true

因此,除非您查询环回地址,否则它需要 https。它怎么知道环回地址是什么?有 DiscoveryPolicy.LoopbackAddresses 属性。默认情况下它包含:

  • “本地主机”
  • “127.0.0.1”

因此,您遇到“需要 HTTPS”错误 - “localhost.fiddler”不被视为环回地址,默认策略要求 https 用于非环回地址。

因此要修复,您需要将 RequireHttps 设置为 false,或者将“localhost.fiddler` 添加到环回地址列表:

var discoClient = new DiscoveryClient("http://localhost.fiddler:5000");
discoClient.Policy.LoopbackAddresses.Add("localhost.fiddler");
//discoClient.Policy.RequireHttps = false;                        
var disco = await discoClient.GetAsync();

如果你这样做 - 你会在 fiddler 中看到发现请求,但是它会失败(响应将包含错误),因为服务器将报告权限为“http://localhost:5000”并且你查询“http://localhost.fiddler:5000”。因此,您还需要覆盖策略中的权限:

var discoClient = new DiscoveryClient("http://localhost.fiddler:5000");
discoClient.Policy.LoopbackAddresses.Add("localhost.fiddler");
discoClient.Policy.Authority = "http://localhost:5000";
var disco = await discoClient.GetAsync();

现在它将按预期工作。

关于c# - IdentityServer 4,试图用 fiddler 捕获流量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49040040/

相关文章:

C# 使用 fiddlercore 创建对 url 的自动响应

C#:DataGridView 仅显示一行

c# - 有没有.Net4 BackgroundCopyManager.dll?

c# - 如何使用 ConfigurationManager 打开配置文件应用程序设置?

c# - Web Api $extend IQueryable 带过滤器

asp.net - 使用 Web API 身份授权属性角色

python - Fiddler 没有捕获我脚本的请求

C# 字符串置换

asp.net-web-api - 在 ASP.NET Web API 中反序列化为派生类

http-post - 如何在 Fiddler 中创建 Post 请求