在 OWIN 身份验证期间获取路由属性?

Get Route Attribute during OWIN Authentication?

我是第一次使用 Web Api 2.0 和 OWIN 中间件构建 RESTful Web 服务,它使用带有刷新令牌的令牌身份验证。 这个博客在这方面帮了我很大的忙: http://bitoftech.net/2014/07/16/enable-oauth-refresh-tokens-angularjs-app-using-asp-net-web-api-2-owin/

在身份验证期间,我需要一个值来指定它应该连接到哪个数据库。 我可以使用 string dbid = context.Parameters.Get("dbid"); 来做到这一点,它从请求正文中获取值。

现在我最初的计划是像在控制器中那样指定属性路由:

[RoutePrefix("api/{dbid:length(2)}")]
public class CustomersController : ApiController
{
    // GET: api/dbid/Customers/id
    // GET: api/XX/Customers/5
    [Authorize]
    [Route("Customer/{id}")]
    [ResponseType(typeof(Customers))]
    public Customers GetCustomer(string dbid, string id)
    {
        Customers c = null;
        Connection cn = new Connection(dbid);

是否也可以使用 TokenEndpointPath 来做到这一点? 就像设置 TokenEndpointPath = new PathString("/{dbid:length(2)}/token"), 而不是以某种方式访问​​路由属性,提取 id?

您不能像在身份验证中那样访问路由参数,因为身份验证中间件在 WebApi 中间件之前,而且路由参数是 WebApi 中的一个术语,因此您不能在身份验证中使用它。

但是,如果 dbid 始终在 url 的同一段中,或者如果它在查询字符串中,您可以自己解析请求 uri 并获取它。