如何填充OwinContext.Request.Path和PathBase? [英] How are OwinContext.Request.Path and PathBase populated?

查看:203
本文介绍了如何填充OwinContext.Request.Path和PathBase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于Katana Project中的其他示例,我正在为OpenID Connect授权代码流编写自己的OWIN中间件.

I'm writing my own OWIN middleware for OpenID Connect authorization code flow, based on other examples in the Katana Project.

为此,我必须构造几个URI,例如重定向URI和返回URL.

As part of this I have to construct a couple of URIs, eg a Redirect URI and a Return URL.

Katana中的其他示例通过并置当前请求中的部分来做到这一点,例如在CookieAuthenticationHandler中

Other examples in Katana do this by concatenating parts from the current request, for example in CookieAuthenticationHandler

loginUri =
    Request.Scheme +
    Uri.SchemeDelimiter +
    Request.Host +
    Request.PathBase +
    Options.LoginPath +
    new QueryString(Options.ReturnUrlParameter, currentUri);

我的问题是,什么规则控制着两个路径属性的最终结果:

OwinContext.Request.Path
OwinContext.Request.PathBase

当请求通过下面管道中的不同处理程序处理请求时,我尝试检查这些属性:

I've tried inspecting these properties as the request passes through different handlers in the pipeline below, for the request:

"https://localhost/Client/login" // Where Client is a virtual directory in IIS

结果:

  • 在/login的映射处理程序中,PathBase ="/Client/Login".
  • 但是,当该请求在返回同一请求的途中到达我的QuillCodeFlowHandler中的ApplyResponseChallengeAsync方法时,PathBase ="/Client"和Path ="/Login".
  • In the mapped handler for /login, the PathBase = "/Client/Login".
  • But when the request gets to the ApplyResponseChallengeAsync method in my QuillCodeFlowHandler on the way back out for the same request, PathBase = "/Client" and Path = "/Login".

因此,在不知道这些值如何填充然后再进行更改的规则"的情况下,很难使用它们来构造URI.如果有人可以解释,将不胜感激.

So without knowing the "rules" for how these values are populated, then later changed, it is hard to construct URIs using them. If anyone can explain, will be much appreciated.

我的配置摘录为:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
    LoginPath = new PathString("/Login")
});

app.UseQuillCodeFlowAuthentication(new QuillCodeFlowOptions());

app.Map("/login", map =>
{
   map.Run(async ctx =>
   {
     if (ctx.Authentication.User == null ||
     !ctx.Authentication.User.Identity.IsAuthenticated)
     {                        
       var authenticationProperties = new AuthenticationProperties();
       [...]
       ctx.Authentication.Challenge(authenticationProperties,
                                    QuillCodeFlowDefaults.AuthenticationType);  

OWIN规范提供了一些说明,而Microsoft.Owin .Host.HttpListener.GetPathAndQuery方法似乎是最初设置路径变量的地方.

The OWIN specification gives some explanation and Microsoft.Owin.Host.HttpListener.GetPathAndQuery method seems to be where the path variables are set initially.

推荐答案

使用构造时

app.Map("/login", map => [...]

这使用

Owin.MapExtensions.Map

构造

Microsoft.Owin.Mapping.MapMiddleware

用于需要运行的代码.

我看到的行为在此中间件的Invoke方法中进行了解释:

The behaviour I have seen is explained in the Invoke method of this middleware:

public async Task Invoke(IDictionary<string, object> environment)
{
    IOwinContext context = new OwinContext(environment);

    PathString path = context.Request.Path;

    PathString remainingPath;
    if (path.StartsWithSegments(_options.PathMatch, out remainingPath))
    {
        // Update the path
        PathString pathBase = context.Request.PathBase;
        context.Request.PathBase = pathBase + _options.PathMatch;
        context.Request.Path = remainingPath;

        await _options.Branch(environment);

        context.Request.PathBase = pathBase;
        context.Request.Path = path;
    }
    else
    {
        await _next(environment);
    }
}

基本上,代码在运行委托之前先更改Path和PathBase (等待_options.Branch(environment)),然后在执行后将它们重新设置为原始值完成.

Basically the code changes the Path and PathBase before it runs the delegate (await _options.Branch(environment) ), then sets these back to the original values after execution is complete.

因此,解释了我所见到的行为.

Hence the behaviour I had seen is explained.

这篇关于如何填充OwinContext.Request.Path和PathBase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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