しばやん雑記

Azure とメイドさんが大好きなフリーランスのプログラマーのブログ

HttpPlatformHandler v1.2 が公開されていました

Web PI を起動した時に HttpPlatformHandler がアップデートされていたことに気が付きました。

f:id:shiba-yan:20151029211708p:plain

インストールしてバージョンを確認したところ 7.1.1959.0 になっていました。前回のアップデートで調べた時にはバージョン 7.1.1954.0 だったので、少しだけビルド番号が上がっていました。

ちなみに Azure Web Apps にインストールされているのは 7.1.1956.0 でした。Web PI で公開されているものよりも、少しだけ古いバージョンのようです。

既に IIS 公式サイトのリファレンスは更新されていたのでまとめます。

HttpPlatformHandler Configuration Reference | Microsoft Docs

HttpPlatformHandler v1.2 で更新された内容は以下の通りです。

  • processPath / stdoutLogFile でサイトルートからの相対パス対応
  • forwardWindowsAuthToken 属性が追加
  • recycleOnFileChange 子要素が追加

それぞれの機能について、個別に紹介しておくことにします。

processPath / stdoutLogFile の相対パス対応

これまでは絶対パスが必須だったので、環境変数などを利用して指定する必要がありましたが、ドット始まりでパスを書くとサイトルートからの相対パス指定として認識されます。

As of v1.2 relative paths are supported, if the path begins with '.' the path is considered to be relative to the site root.

以下のように書くとサイトルート以下にある startup.bat が実行されます。

<httpPlatform processPath=".\startup.bat" stdoutLogEnabled="true" stdoutLogFile=".\log.txt" />

カレントディレクトリがサイトルートになっていれば便利ですが、まだ確認してません。

forwardWindowsAuthToken 属性の追加

これまでは Windows 認証の処理を IIS が行うので、ユーザー情報を使った処理は IIS 側でしか行えなかったのですが、新しく Windows 認証のトークンを HTTP リクエストヘッダーで渡すオプションが追加されました。

X-IIS-WindowsAuthToken という名前で渡されるので、ASP.NET 5 の IISIntegration で実装されている処理を参考にすれば、簡単に Windows 認証を組み込めます。

実際の処理としては C# の場合 WindowsIdentity にトークンの値を渡すだけのようです。

private WindowsPrincipal UpdateUser(HttpContext httpContext)
{
    var xIISWindowsAuthToken = httpContext.Request.Headers[XIISWindowsAuthToken];
    int hexHandle;
    WindowsPrincipal winPrincipal = null;
    if (!StringValues.IsNullOrEmpty(xIISWindowsAuthToken)
        && int.TryParse(xIISWindowsAuthToken, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hexHandle))
    {
        // Always create the identity if the handle exists, we need to dispose it so it does not leak.
        var handle = new IntPtr(hexHandle);
        var winIdentity = new WindowsIdentity(handle);

        // WindowsIdentity just duplicated the handle so we need to close the original.
        NativeMethods.CloseHandle(handle);

        httpContext.Response.RegisterForDispose(winIdentity);
        winPrincipal = new WindowsPrincipal(winIdentity);

        if (_options.AutomaticAuthentication)
        {
            var existingPrincipal = httpContext.User;
            if (existingPrincipal != null)
            {
                httpContext.User = SecurityHelper.MergeUserPrincipal(existingPrincipal, winPrincipal);
            }
            else
            {
                httpContext.User = winPrincipal;
            }
        }
    }

    return winPrincipal;
}

といっても ASP.NET 5 ではこのミドルウェアが面倒を見てくれるので簡単ですね。

recycleOnFileChange 子要素の追加

そのまま名前から分かるように、指定したファイルが変更されたタイミングで IIS ワーカープロセスをリサイクルする設定です。子要素として file 要素を取ります。

<httpPlatform processPath=".\startup.bat" stdoutLogEnabled="true" stdoutLogFile=".\log.txt">
  <recycleOnFileChange>
    <file path=".\recycle.txt"/>
  </recycleOnFileChange>
</httpPlatform>

個人的には IIS 側のリサイクル設定で大体の場合は事足りると思いますが、アプリケーション側が任意のタイミングでプロセス自体を再起動することが可能になりました。

まとめると ASP.NET 5 で使いやすくするための更新ということです。