我看到了這個頁面,我幾乎放棄了,但後來我在PluralSight的Craig處碰到這個article。這給了我從ASP.Net返回401而不是IIS的想法,這就是爲什麼在IIS中啓用匿名身份驗證的原因。
以下是解決該問題的步驟。
步驟1:在IIS中啓用匿名身份驗證和Windows身份驗證。
第2步:此代碼添加到您的Global.asax.cs
信用卡/感謝:Uploadify (Session and authentication) with ASP.NET MVC
注:在我的版本,因爲我只希望這個代碼僅POST請求使用特殊的邏輯爲uploadify工作。換句話說,我刪除了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步:更新的JavaScript調用uploadify包括表單的身份驗證密鑰,會話密鑰。
<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>
...
我有喜歡的OP完全相同的問題,我想你的解決方案,但我不能得到它的工作。出於某種原因,Request.ServerVariables [「LOGON_USER」]始終爲空。有什麼建議麼? – Simon 2011-01-28 12:16:11
是否在IIS中啓用了匿名身份驗證和Windows身份驗證? – 2011-02-10 18:00:24
我知道評論/提出問題的時間已晚,但這意味着使用uploadify和Windows身份驗證的解決方案不需要使用Windows身份驗證? – bzarah 2011-05-05 15:51:59