Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

identityserver4 - Flurl调用IdentityServer4(Flurl calling IdentityServer4)

I'm attempting to call Identity Server 4 with Flurl

(我正在尝试使用Flurl调用Identity Server 4)

Im getting a 400 back from the ID server.

(我从ID服务器取回了400。)

The credentials etc work when I make the call with PostMan or with the methods available in IdentityModel.Client.

(当我使用PostMan或IdentityModel.Client中可用的方法进行呼叫时,凭据等起作用。)

I am unsure about the Post ie PostUrlEncodedAsync As you can see I have tried various combinations.

(我不确定Post即PostUrlEncodedAsync如您所见,我尝试了各种组合。)

This.. does not work

(这..行不通)

var address = "http://localhost:8027/connect/token";
var x = await address
.WithBasicAuth("clientName", "secretValue")
.SetQueryParams(
    new
    {
        GrantType = "client_credentials",
        Scope = "requiredScope"
    }
)
.PostUrlEncodedAsync(new { });
//.SendAsync(HttpMethod.Post, null, CancellationToken.None);
//.SendAsync(HttpMethod.Post, y);

This.. does.

(这..确实。)

var apiClientCredentials = new ClientCredentialsTokenRequest()
{
    Address = "http://localhost:8027/connect/token",

    ClientId = "clientName",
    ClientSecret = "secretValue",
    Scope = "requiredScope"
};

var client = new HttpClient();

var tokenResponse = await client.RequestClientCredentialsTokenAsync(apiClientCredentials);
if (tokenResponse.IsError)
{
}

I can see that deep doen inside 'RequestClientCredentialsTokenAsync' it does parameters.Add to add grant type and scope hence I have added those as params.

(我可以看到在'RequestClientCredentialsTokenAsync'中的深层区域确实有参数。添加以添加授权类型和范围,因此我将它们添加为参数。)

Have also tried adding the client id and secret to the query params.

(还尝试将客户端ID和密码添加到查询参数中。)

Same 400 message.

(相同的400条消息。)

Any help greatly appreciated.

(任何帮助,不胜感激。)

  ask by S Rosam translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe the difference is that IdentityModel knows to serialize those ClientCredentialsTokenRequest property names like ClientId and GrantType to snake-case, ie client_id and grant_type , as required by OAuth2.

(我相信不同之处在于,IdentityModel知道可以按照OAuth2的要求将那些ClientCredentialsTokenRequest属性名称(如ClientIdGrantType序列化为蛇形,即client_idgrant_type 。)

When you hand Flurl an anonymous object, it just serializes the property names exactly as-is.

(当您将Flurl交给匿名对象时,它只是按原样序列化属性名称。)

To make it work with Flurl you can either create a class and decorate it with Json.NET serialization attributes , or (what I typically do) keep it simple take some liberties with C# property name conventions:

(要使其与Flurl一起使用,您可以创建一个类并用Json.NET序列化属性装饰它,或者(我通常做的)保持简单,以C#属性名称约定为准:)

.SetQueryParams(
    new
    {
        grant_type = "client_credentials",
        scope = "requiredScope"
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...