gpt4 book ai didi

asp.net - Owin.Security.OAuth : returns invalid_grant 上的 Authorization_code 授予流程

转载 作者:行者123 更新时间:2023-12-02 10:26:01 27 4
gpt4 key购买 nike

我正在尝试使用 authorization_code 授权流程设置身份验证。我之前曾使用过 grant_type=password ,所以我知道这些东西应该如何工作。但是当使用 grant_type=authorization_code 时,我无法让它返回除 invalid_grant

之外的任何内容

这是我的设置:

app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/auth/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(5),
Provider = new SampleAuthProvider()
});

app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
AuthenticationType = "Bearer"
});

SampleAuthProvider 是以下类:https://gist.github.com/anonymous/8a0079b705423b406c00

基本上,它只是记录每一步并验证它。我尝试了请求:

POST http://localhost:12345/auth/token
grant_type=authorization_code&code=xxxxxx&client_id=xxxxx&redirect_uri=https://xxxx.com/
Content-Type: application/x-www-form-urlencoded

它正在经历:

  • OnMatchEndpoint
  • OnValidateClientAuthentication

仅此而已。我预计它接下来会调用 OnValidateTokenRequestOnGrantAuthorizationCode,但事实并非如此。我不知道为什么。

请求中的xxxx不是占位符,我这样尝试过。也许中间件会自行进行一些检查并因此拒绝请求?我尝试了带有 httpredirect_uri 变体,没有任何协议(protocol),没有尾部斜杠...

它也可以与自定义grant_type一起正常工作。因此,如果我太绝望了,我想我可以用它来模拟授权代码,但我宁愿不必这样做。

TL;DR

使用 grant_type=authorization_code 时,我的 OAuthAuthorizationServerProviderOnValidateClientAuthentication 之后返回 {"error":"invalid_grant"} .

  • 为什么停在那里?
  • 我怎样才能让这一切该死的事情发挥作用?

感谢您的帮助!

<小时/>

编辑

正如 RajeshKannan 所指出的,我的配置犯了一个错误。我没有提供 AuthorizationCodeProvider 实例。但是,这并没有完全解决问题,因为在我的例子中,代码不是由 AuthorizationCodeProvider 发出的,我不能只是反序列化它。我回答了我正在工作的解决方法。

最佳答案

这就是我的工作内容。我对该解决方案并不完全满意,但它有效并且应该可以帮助其他人解决他们的问题。

<小时/>

所以,问题是我没有设置 AuthorizationCodeProvider 属性。当收到带有 grant_type=authorization_code 的请求时,该代码必须由该代码提供者进行验证。该框架假设代码是由该代码提供商发布的,但我的情况并非如此。我从另一台服务器获取它,并且必须将代码发送回它进行验证。

在标准情况下,您也是发布代码的人,RajeshKannan 提供的链接描述了您必须执行的所有操作。

您必须在此处设置属性:

app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString(Paths.TokenPath),
Provider = new SampleAuthProvider(),
AuthorizationCodeProvider = new MyAuthorizationCodeProvider ()
}

以及 MyAuthorizationCodeProvider 类的声明:

internal class MyAuthorizationCodeProvider : AuthenticationTokenProvider
{
public override async Task ReceiveAsync(
AuthenticationTokenReceiveContext context)
{
object form;
// Definitely doesn't feel right
context.OwinContext.Environment.TryGetValue(
"Microsoft.Owin.Form#collection", out form);
var redirectUris = (form as FormCollection).GetValues("redirect_uri");
var clientIds = (form as FormCollection).GetValues("client_id");
if (redirectUris != null && clientIds != null)
{
// Queries the external server to validate the token
string username = await MySsoService.GetUserName(context.Token,
redirectUris[0]);
if (!string.IsNullOrEmpty(username))
{
var identity = new ClaimsIdentity(new List<Claim>()
{
// I need the username in GrantAuthorizationCode
new Claim(ClaimTypes.NameIdentifier, username)
}, DefaultAuthenticationTypes.ExternalBearer);

var authProps = new AuthenticationProperties();

// Required. The request is rejected if it's not provided
authProps.Dictionary.Add("client_id", clientIds[0]);

// Required, must be in the future
authProps.ExpiresUtc = DateTimeOffset.Now.AddMinutes(1);

var ticket = new AuthenticationTicket(identity, authProps);
context.SetTicket(ticket);
}
}
}
}

关于asp.net - Owin.Security.OAuth : returns invalid_grant 上的 Authorization_code 授予流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24515211/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com