我是第一次使用 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/