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

标签 c# web-services http asp.net-web-api2 owin

我是第一次使用 Web Api 2.0 和 OWIN 中间件构建 RESTful Web 服务,它使用带有刷新 token 的 token 身份验证。 这个博客在这方面帮了我很大的忙: 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"); 来完成,它从请求正文中获取值。

现在我最初的计划是像在 Controller 中那样指定通过属性路由:

[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 并自行获取它。

关于c# - 在 OWIN 身份验证期间获取路由属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49818101/

相关文章:

C#字节数组问题

java - 发布 WebService 供公众访问

PHP SoapClient SSL 连接超时错误

c# - 如何使用 https 创建 HttpListener

c# - Npoco 新数据库失败并显示 "Value cannot be null"

c# - 类上的扩展方法 - 好主意还是坏主意?

c# - 更改 Google 身份验证请求的语言 - Oauth 2.0

asp.net - 您可以使用 jQuery 连接到 .NET Web 服务吗?

node.js - 如何在 Node.js 中使用 HTTPS

http - 如何在 v0.8 中的 influxdb 中获取数据库列表