带有动态TokenEndpointPath的OAuth WebAPI [英] owin oauth webapi with a dynamic TokenEndpointPath

查看:336
本文介绍了带有动态TokenEndpointPath的OAuth WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过以下方式在WebApi 2服务器中使用OWIN成功实现了oAuth:

I've successfully implemented oAuth using OWIN in my WebApi 2 Server with:

app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions {
  TokenEndpointPath = new PathString("/api/TokenByPassword"),
  // ...
});

但是,我希望TokenEndpointPath是动态的,因为我将有多个数据库,每个数据库都有自己的帐户记录.

However, I would like the TokenEndpointPath to be dynamic as I will have multiple databases each with their own account records.

我相信我想要类似的东西:

I believe I want something like:

TokenEndpointPath = new PathString("/api/{databaseid}/TokenByPassword");

我不相信OAuthAuthorizationServerOptions支持此功能,即使它支持,我也将如何获取databaseid?

I don't believe OAuthAuthorizationServerOptions supports this and even if it did - how would I get the databaseid ?

我可以使用AttributeRouting在自己的WebAPI中实现此功能,但是在该WebAPI中进行正确的OWIN调用才能生成正确的BearerToken呢?

I could implement this in my own WebAPI with AttributeRouting, but then what would be the correct OWIN calls to make in that WebAPI to generate the correct BearerToken?

推荐答案

我找到了答案.

即使在OAuthAuthorizationServerOptions中指定了TokenEndpointPath,OAuthAuthorizationServerProvider仍具有一个称为OnMatchEndpoint的委托.在此委托内部,您可以访问调用的Request.Uri.AbsolutePath,如果符合条件,则可以调用MatchesTokenEndpoint(),在这种情况下,将调用OnGrantResourceOwnerCredentials,在该位置您可以再次访问Request.Uri并选择退出{databaseid}并使用正确的数据库授予访问权限.

Even though the TokenEndpointPath is specified in the OAuthAuthorizationServerOptions, the OAuthAuthorizationServerProvider has a delegate called OnMatchEndpoint. Inside this delegate, you can access the Request.Uri.AbsolutePath of the call and if it matches your criteria, you can then call MatchesTokenEndpoint() in which case OnGrantResourceOwnerCredentials will get called where you again can gain access the the Request.Uri and pick out the {databaseid} and use the correct database to Grant access.

OWIN非常灵活,但是在不太简单的情况下,哪个调用可以使您在何时执行所需的操作不是很明显.

OWIN is very flexible, but not immediately obvious which calls to make when to do what you want when it is something not quite straightforward.

这篇关于带有动态TokenEndpointPath的OAuth WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆