2014-09-03 63 views
10

基於Katana項目中的其他示例,我正在爲OpenID Connect授權代碼流編寫我自己的OWIN中間件。OwinContext.Request.Path和PathBase如何填充?

作爲這個的一部分,我必須構建一對URI,例如重定向URI和返回URL。

在卡塔納其他的例子從當前請求拼接部分做到這一點,例如在CookieAuthenticationHandler

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

我的問題是什麼規則控制什麼在這兩個路徑屬性結束:

OwinContext.Request.Path 
OwinContext.Request.PathBase 

我已經嘗試檢查這些屬性,因爲請求經過下面的管道中的不同處理程序,請求:

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

其結果是:

  • 在用於/登錄,PathBase = 「/客戶端/登錄」 所映射的處理程序。
  • 當請求到達我的QuillCodeFlowHandler中的ApplyResponseChallengeAsync方法時,返回相同的請求PathBase =「/ Client」和Path =「/ Login」。

因此,如果不知道這些值如何填充的「規則」,然後改變,那麼使用它們很難構建URI。如果有人能解釋,將不勝感激。

我的配置的提取物是:

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 specification給出了一些解釋和Microsoft.Owin.Host.HttpListener.GetPathAndQuery方法似乎是在路徑變量的初始設置。

回答

5

當使用構建物

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

這使用

Owin.MapExtensions.Map 

它構造的

Microsoft.Owin.Mapping.MapMiddleware 

一個實例爲其中需要運行的代碼。

我看到在此中間件的調用方法進行說明的行爲:

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); 
    } 
} 

基本上代碼改變它運行委託之前的路徑和PathBase (等待_options.Branch(環境)) ,然後在執行完成後將這些設置恢復爲原始值

因此我解釋過的行爲。