Owin OAuth 2.0 密码授予流程

Owin OAuth2.0 PasswordGrant flow

问题:我是不是遗漏了什么或者误解了实际应该调用的函数?

因此,我通过创建一个测试 WebApi 项目来使用 Owin.OAuth 实现 OAuth2,开始了简单的工作。点击 Route 并进入 provider 是没有问题的,但这里是代码: 启动 Class:

    public void Configuration(IAppBuilder app)
    {
        var config = GlobalConfiguration.Configuration;

        app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new OAuthProvider(),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
            AllowInsecureHttp = true
        });

        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());           

        WebApiConfig.Register(config);            
    }

现在是准系统提供程序 class:

    public class OAuthProvider : OAuthAuthorizationServerProvider
    {

    public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        return base.ValidateClientAuthentication(context);
    }

    public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        return base.GrantResourceOwnerCredentials(context);
    }
}

我想使用密码授予 https://www.rfc-editor.org/rfc/rfc6749#section-4.3.2。现在根据 OAuthAuthorizationServerProvider 文档,GrantResourceOwnerCredentials 函数在以下时间被调用:

Called when a request to the Token endpoint arrives with a "grant_type" of "password". This occurs when the user has provided name and password credentials directly into the client application's user interface, and the client application is using those to acquire an "access_token" and optional "refresh_token".

但是当我点击路由时,它总是进入 ValidateClientAuthentication 函数。

邮递员负载:

POST /Token HTTP/1.1
Host: localhost:57507
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
grant_type=password&username=test&password=test123

还尝试通过 Postman 使用 BasicAuth:

POST /Token HTTP/1.1
Host: localhost:57507
Content-Type: application/x-www-form-urlencoded
Authorization: Basic dGVzdDp0ZXN0MTIz
grant_type=password

我是不是遗漏了什么或者误解了它的工作原理?

您需要 "describe" 在 GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext) 方法体中验证凭据的方法,而不是调用基本方法。 当您调用 context.Validated(ClaimsIdentity) 方法时 - 您将获得不记名令牌作为响应。

有一个很好的例子的问题 - see first code block in question

或者你可以看例子here

ValidateClientAuthentication 只是验证您的凭证 "grant_type=password&username=test&password=test123"

否则,您的代码看起来没问题。

好的,所以当我问这个问题时,我对调用流程的解释不正确。

我在想,当使用密码授权时,第一个被调用的函数是 GrantResourceOwnerCredentialsOAuth Spec Doc Password Grant. 实际上它总是会调用 ValidateClientAuthentication 然后 GrantResourceOwnerCredentials.

所以这只是我的误会。此示例代码有效。