残念ながら、これは Windows Media Player for FF の既知のバグです。IEで動作します。
これが機能しない理由は非常に単純です。プラグインはリクエストとともに認証 Cookie を送信しないため、認証されていないかのように見えます。
これを機能させる唯一の方法は、Cookie 値をクエリ文字列パラメーターとして要求に追加し、サーバーでセッションを再同期することです。
それを実行に移しましょう。
残念ながら、@Video.MediaPlayer
ヘルパーを使用することはできません。これは、クエリ文字列パラメーターを指定できないためです。物理ファイルでのみ機能します (これはちょっと面倒です)。そう:
<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" height="400" width="600" >
<param name="URL" value="@Url.Content("~/media/test.wmv?requireAuthSync=true&token=" + Url.Encode(Request.Cookies[FormsAuthentication.FormsCookieName].Value))" />
<param name="autoStart" value="False" />
<param name="uiMode" value="full" />
<param name="stretchToFit" value="True" />
<param name="volume" value="75" />
<embed src="@Url.Content("~/media/test.wmv?requireAuthSync=true&token=" + Url.Encode(Request.Cookies[FormsAuthentication.FormsCookieName].Value))" width="600" height="400" type="application/x-mplayer2" autoStart="False" uiMode="full" stretchToFit="True" volume="75" />
</object>
内部Global.asax
では、メソッドをサブスクライブしApplication_BeginRequest
、リクエストから認証 Cookie を再同期します。
protected void Application_BeginRequest()
{
if (!string.IsNullOrEmpty(Context.Request["RequireAuthSync"]))
{
AuthCookieSync();
}
}
private void AuthCookieSync()
{
try
{
string authParamName = "token";
string authCookieName = FormsAuthentication.FormsCookieName;
if (!string.IsNullOrEmpty(Context.Request[authParamName]))
{
UpdateCookie(authCookieName, Context.Request.QueryString[authParamName]);
}
}
catch { }
}
private void UpdateCookie(string cookieName, string cookieValue)
{
var cookie = Context.Request.Cookies.Get(cookieName);
if (cookie == null)
{
cookie = new HttpCookie(cookieName);
}
cookie.Value = cookieValue;
Context.Request.Cookies.Set(cookie);
}
そして、それはほとんどそれです。.wmv
これが機能するための唯一の要件は、IIS 7 統合パイプライン モードで実行して、すべての要求がASP.NETを通過するBeginRequest
ことです。
レガシー Web サーバー (IIS 6.0 など) を使用しているか、クラシック パイプライン モードで実行していて、ASP.NET ですべての要求のワイルドカード マッピングを実行したくない場合は、すべてのメディア ファイルを安全な場所 (そのような場所) に置くことができます。 as ~/App_Data
) ユーザーが直接アクセスすることはできず、[Authorize]
属性で装飾されたコントローラー アクションを介してそれらを提供します。
[Authorize]
public ActionResult Media(string file)
{
var appData = Server.MapPath("~/App_Data");
var filename = Path.Combine(path, file);
filename = Path.GetFullPath(filename);
if (!filename.StartsWith(appData))
{
// prevent people from reading arbitrary files from your server
throw new HttpException(403, "Forbidden");
}
return File(filename, "application/octet-stream");
}
その後:
<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" height="400" width="600" >
<param name="URL" value="@Url.Action("media", "home", new { requireAuthSync = true, token = Request.Cookies[FormsAuthentication.FormsCookieName].Value })" />
<param name="autoStart" value="False" />
<param name="uiMode" value="full" />
<param name="stretchToFit" value="True" />
<param name="volume" value="75" />
<embed src="@Url.Action("media", "home", new { requireAuthSync = true, token = Request.Cookies[FormsAuthentication.FormsCookieName].Value })" width="600" height="400" type="application/x-mplayer2" autoStart="False" uiMode="full" stretchToFit="True" volume="75" />
</object>