我看到了这个页面,几乎放弃了,但后来我在 PluralSight 看到了 Craig 的这篇文章。这给了我从 ASP.Net 而不是 IIS 返回 401 的想法,这就是在 IIS 中启用匿名身份验证的原因。
以下是解决此问题的步骤。
第 1 步:在 IIS 中启用匿名身份验证和 Windows 身份验证。
第 2 步:将此代码添加到您的 Global.asax.cs
信用/感谢: Uploadify (Session and authentication) 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>
...