このページを見て諦めかけたところ、Craig at PluralSight の記事に出くわしました。これにより、IIS ではなく ASP.Net から 401 を返すというアイデアが得られました。これが、IIS で匿名認証が有効になっている理由です。
この問題を回避する手順は次のとおりです。
手順 1: IIS で匿名認証と Windows 認証を有効にします。
ステップ 2: このコードを Global.asax.cs に追加してください
クレジット/感謝: Uploadify (セッションと認証) with ASP.NET MVC
注:私のバージョンでは、このコードを機能させたいだけなので、POST リクエストのみが特別なロジックを使用します。アップロードします。つまり、GET リクエストのコードを削除します。GET をサポートする場合は、上記のリンクを参照してください。
protected void Application_BeginRequest(object sender, EventArgs e)
{
/* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */
try
{
string session_param_name = "ASPSESSID";
string session_cookie_name = "ASP.NET_SessionId";
if (HttpContext.Current.Request.Form[session_param_name] != null)
{
UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
}
}
catch
{
}
try
{
string auth_param_name = "AUTHID";
string auth_cookie_name = FormsAuthentication.FormsCookieName;
if (HttpContext.Current.Request.Form[auth_param_name] != null)
{
UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
return; // this is an uploadify request....get out of here.
}
}
catch
{
}
// handle the windows authentication while keeping anonymous turned on in IIS.
// see: https://stackoverflow.com/questions/2549914/uploadify-flash-file-upload-integrated-windows-authentication
if (Request.ServerVariables["LOGON_USER"].Length == 0) // They haven't provided credentials yet
{
Response.StatusCode = 401;
Response.StatusDescription = "Unauthorized";
Response.End();
return;
}
FormsAuthentication.SetAuthCookie(Request.ServerVariables["LOGON_USER"], true);
}
private void UpdateCookie(string cookie_name, string cookie_value)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
if (null == cookie)
{
cookie = new HttpCookie(cookie_name);
}
cookie.Value = cookie_value;
HttpContext.Current.Request.Cookies.Set(cookie);
}
ステップ 3: Uploadify を呼び出す JavaScript を更新して、フォームの認証キーとセッション キーを含めます。
<script>
var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
var ASPSESSID = "<%= Session.SessionID %>";
$("#uploadifyLogo").uploadify({
...
scriptData: { ASPSESSID: ASPSESSID, AUTHID: auth }
});
ステップ 4: web.config を更新する
<system.web>
...
<authentication mode="Forms">
<forms defaultUrl="/" />
</authentication>
...