Uploadify - MVC3: ข้อผิดพลาดในการเปลี่ยนเส้นทาง HTTP 302 (เนื่องจากการรับรองความถูกต้องของ asp.net)

ฉันใช้ uploadify เพื่ออัปโหลดไฟล์จำนวนมาก และได้รับข้อผิดพลาดการเปลี่ยนเส้นทาง 302 ฉันสมมติว่านี่เป็นเพราะโทเค็นคุกกี้การรับรองความถูกต้องของ asp.net บางตัวไม่ได้ถูกส่งกลับในการเรียกสคริปต์

ฉันได้อัปโหลดการทำงานในแอปพลิเคชัน Bare Bones mvc3 ทั่วไป แต่เมื่อฉันพยายามรวมเข้ากับแอปพลิเคชัน asp.net mvc3 ที่ปลอดภัย มันจะพยายามเปลี่ยนเส้นทางไปยังบัญชี/การลงชื่อเข้าใช้

ฉันมีโทเค็นการรับรองความถูกต้อง (@auth) ที่กลับมาเป็นสตริงแบบยาวภายในมุมมอง แต่ฉันยังคงได้รับข้อผิดพลาดการเปลี่ยนเส้นทาง 302

มีแนวคิดใดบ้างที่จะส่งข้อมูลการรับรองความถูกต้องของคุกกี้ได้อย่างไร

ดู:

 @{
     string auth = @Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
     }


<script type="text/javascript">
    jQuery.noConflict();
    jQuery(document).ready(function () {
        jQuery("#bulkupload").uploadify({
            'uploader': '@Url.Content("~/Scripts/uploadify.swf")',
            'cancelImg': '/Content/themes/base/images/cancel.png',
            'buttonText': 'Browse Files',
            'script': '/Creative/Upload/',
            scriptData: { token: "@auth" }, 
            'folder': '/uploads',
            'fileDesc': 'Image Files',
            'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
            'sizeLimit': '38000',
            'multi': true,
            'auto': true,
              'onError'     : function (event,ID,fileObj,errorObj) {
      alert(errorObj.type + ' Error: ' + errorObj.info);
    }
        });
    }); 
    </script> 

ตัวควบคุม:

public class CreativeController : Controller
{
    public string Upload(HttpPostedFileBase fileData, string token)
    {
      ...
    }
}

อัปเดต: ตกลง ใช้งานได้ดีกับ IE9 แต่ไม่ใช่ใน Chrome (Chrome พ่น 302) FF ไม่แสดงการควบคุมด้วยซ้ำ

ไม่มีใครรู้วิธีทำให้สิ่งนี้ทำงานในการปรับปรุงล่าสุดสำหรับ Chrome และ FF หรือไม่


person genxgeek    schedule 15.04.2012    source แหล่งที่มา


คำตอบ (1)


ฉันมีปัญหาที่คล้ายกันเมื่อฉันพยายามใช้ uploadify กับ asp.net mvc3 หลังจาก googling มากนี่คือสิ่งที่ฉันพบว่าดูเหมือนว่าจะได้ผล โดยพื้นฐานแล้วเกี่ยวข้องกับการประกาศแอตทริบิวต์ที่กำหนดเอง จากนั้นส่งผ่านคุกกี้การตรวจสอบสิทธิ์ไปยังโพสต์ HTTP ของ Uploadify อย่างชัดเจน และประมวลผลคุกกี้ด้วยตนเองผ่านแอตทริบิวต์ที่กำหนดเอง

ก่อนอื่นให้ประกาศแอตทริบิวต์ที่กำหนดเองนี้:

/// <summary>
/// A custom version of the <see cref="AuthorizeAttribute"/> that supports working
/// around a cookie/session bug in Flash.  
/// </summary>
/// <remarks>
/// Details of the bug and workaround can be found on this blog:
/// http://geekswithblogs.net/apopovsky/archive/2009/05/06/working-around-flash-cookie-bug-in-asp.net-mvc.aspx
/// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class TokenizedAuthorizeAttribute : AuthorizeAttribute
{
    /// <summary>
    /// The key to the authentication token that should be submitted somewhere in the request.
    /// </summary>
    private const string TOKEN_KEY = "authCookie";

    /// <summary>
    /// This changes the behavior of AuthorizeCore so that it will only authorize
    /// users if a valid token is submitted with the request.
    /// </summary>
    /// <param name="httpContext"></param>
    /// <returns></returns>
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        string token = httpContext.Request.Params[TOKEN_KEY];

        if (token != null)
        {
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);

            if (ticket != null)
            {
                var identity = new FormsIdentity(ticket);
                string[] roles = System.Web.Security.Roles.GetRolesForUser(identity.Name);
                var principal = new GenericPrincipal(identity, roles);
                httpContext.User = principal;
            }
        }

        return base.AuthorizeCore(httpContext);
    }
}

จากนั้นประกาศการดำเนินการนี้ให้ส่งคุกกี้การรับรองความถูกต้องไปยัง ViewData ของคุณ เพื่อให้คุณสามารถส่งผ่านไปยังวิดเจ็ต Uploadify นี่จะเป็นการดำเนินการที่คุณเรียกใช้เพื่อสร้างอินสแตนซ์วิดเจ็ต Uploadify ในภายหลัง

    [Authorize]
    public ActionResult UploadifyUploadPartial()
    {
        ViewBag.AuthCookie = Request.Cookies[FormsAuthentication.FormsCookieName] == null
                                 ? string.Empty
                                 : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
        return PartialView("UploadifyUpload");
    }

นี่คือโค้ดสำหรับมุมมอง UploadifyUpload โดยพื้นฐานแล้วมันเป็น wrapper สำหรับ JavaScript ซึ่งตั้งค่า Uploadify ฉันคัดลอกของฉันโดยไม่มีการเปลี่ยนแปลง ดังนั้นคุณจะต้องปรับให้เข้ากับใบสมัครของคุณ สิ่งสำคัญที่ควรทราบคือคุณกำลังส่ง authCookie ไปยังคุณสมบัติ scriptData ผ่านทาง ViewData จากการดำเนินการ UploadifyUploadPartial

@if (false)
{
    <script src="../../Scripts/jquery-1.5.1-vsdoc.js" type="text/javascript"></script>
}
@{
    ViewBag.Title = "Uploadify";
}
<script src="@Url.Content("~/Scripts/plugins/uploadify/swfobject.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/plugins/uploadify/jquery.uploadify.v2.1.4.min.js")" type="text/javascript"></script>
<link href="/[email protected]("~/Scripts/plugins/uploadify/uploadify.css")" rel="stylesheet" type="text/css" />
<script>
    $(document).ready(function () {
        CreateUploadifyInstance("dir-0");
    });
    function CreateUploadifyInstance(destDirId) {
        var uploader = "@Url.Content("~/Scripts/plugins/uploadify/uploadify.swf")";
        var cancelImg = "@Url.Content("~/Scripts/plugins/uploadify/cancel.png")";
        var uploadScript = "@Url.Content("~/Upload/UploadifyUpload")";
        var authCookie = "@ViewBag.AuthCookie";
        $('#uploadifyHiddenDummy').after('<div id="uploadifyFileUpload"></div>');
        $("#uploadifyFileUpload").uploadify({
            'uploader': uploader,
            'cancelImg': cancelImg,
            'displayData': 'percentage',
            'buttonText': 'Select Session...',
            'script': uploadScript,
            'folder': '/uploads',
            'fileDesc': 'SunEye Session Files',
            'fileExt': '*.son2',
            'scriptData'  : {'destDirId':destDirId, 'authCookie': authCookie},
            'multi': false,
            'auto': true,
            'onCancel': function(event, ID, fileObj, data) {
                //alert('The upload of ' + ID + ' has been canceled!');
            },
            'onError': function(event, ID, fileObj, errorObj) {
                alert(errorObj.type + ' Error: ' + errorObj.info);
            },
            'onAllComplete': function(event, data) {
                $("#treeHost").jstree("refresh");
                //alert(data.filesUploaded + ' ' + data.errors);
            },
            'onComplete': function(event, ID, fileObj, response, data) {
                alert(ID + " " + response);
            }
        });
    }
    function DestroyUploadifyInstance() {
        $("#uploadifyFileUpload").unbind("uploadifySelect");
        swfobject.removeSWF('uploadifyFileUploadUploader');
        $('#uploadifyFileUploadQueue').remove();
        $('#uploadifyFileUploadUploader').remove();
        $('#uploadifyFileUpload').remove();
    }
</script>
<div id="uploadifyHiddenDummy" style="visibility:hidden"></div>
<div id="uploadifyFileUpload">
</div>

สำหรับการดำเนินการของคุณที่อัปโหลดโพสต์ ให้ใช้แอตทริบิวต์ TokenizedAuthorize ใหม่แทนแอตทริบิวต์ Authorize:

    [HttpPost]
    [TokenizedAuthorize]
    public string UploadifyUpload(HttpPostedFileBase fileData)
    {
        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Form["authCookie"]);
        if (ticket != null)
        {
            var identity = new FormsIdentity(ticket);
            if (!identity.IsAuthenticated)
            {
                return "Not Authenticated";
            }
        }
        // Now parse fileData...
    }

สุดท้าย หากต้องการใช้โค้ดที่เราเขียนไว้ ให้เรียกใช้ UploadifyUploadPartial Action ผ่านทาง Html.Action Helper บนมุมมองใดๆ ที่คุณต้องการโฮสต์วิดเจ็ต Uploadify:

@Html.Action("UploadifyUploadPartial", "YourUploadControllerName")

คุณควรจะไปที่จุดนี้ดี รหัสควรใช้งานได้ใน FF, Chrome และ IE 9 โปรดแจ้งให้เราทราบหากคุณมีปัญหา

person Bojin Li    schedule 15.04.2012
comment
สวัสดี ฉันได้ลองกลับมาดูสิ่งนี้อีกครั้ง และพบสิ่งที่คล้ายกับแอตทริบิวต์ tokenizeauthorize อย่างไรก็ตาม คุกกี้ยังคงไม่ได้รับการตั้งค่าในส่วนหัวของคำขอ: (นี่คือสิ่งที่ได้รับจากคำขอส่วนหัวสำหรับ IE9) แน่นอนว่า Chrome และ FF ที่ขาดหายไปนั้นจะไม่สามารถใช้งานกับการอัพโหลดได้ คุกกี้: __RequestVerificationToken_Lw__=8+WZPGAaKtgkIPfbBovP1ZRP2qQKE3u67ueltnzcoCPH0nN1tUHdtgUorjlweUvn+zTJhkFeRuMShCOrbyHR5Xi3DOL4HCspXuVEOsWIr4Ape+l5MYPiFsQ6Lnw8LstqNjceWW9E AV24eA0mVxq2xTG18h/INNKLB8cRUiEn9DI=; .ASPXAUTH=C64A69436A8F... - person genxgeek; 21.04.2012
comment
ฉันได้เห็นวิธีแก้ปัญหานี้แล้วเช่นกัน แต่ไม่ได้ใส่ Cookie: __RequestVerificationToken... ไว้ในส่วนหัว ดังนั้นการกระทำการอัปโหลดจะไม่ถูกเรียก นี่เป็นเพียงปัญหาของ Chrome (FF ไม่โหลดส่วนควบคุมด้วยซ้ำ IE9 ทำงานได้ดี) - person genxgeek; 24.04.2012
comment
ป้องกันแทนที่บูล AuthorizeCore (HttpContextBase httpContext) { โทเค็นสตริง = httpContext.Request.Params [TOKEN_KEY]; โทเค็น if (token != null) นั้นเป็นโมฆะเสมอในโค้ดนี้ - person genxgeek; 24.04.2012
comment
@JaJ เฮ้ ดูเหมือนว่าฉันไม่ได้โพสต์ทุกอย่างในโซลูชันของฉัน เนื่องจากฉันไม่ได้ใช้ Uploadify (แม้ว่าฉันจะใช้งานได้ก็ตาม) ฉันก็มีโค้ดขึ้นสนิมเล็กน้อย ให้ฉันอัปเดตคำตอบด้วยสิ่งที่ขาดหายไป - person Bojin Li; 24.04.2012
comment
รหัสเด็ด! แต่ FF ไม่แสดงการควบคุม ตำแหน่งที่จะแสดงว่างเปล่าโดยสิ้นเชิง IE9 และ Chrome แสดงผลได้ดี - person genxgeek; 25.04.2012
comment
นอกจากนี้ Chrome ยังไม่ส่งคุกกี้เหมือนกับ IE [คุกกี้: __RequestVerificationToken_Lw__=8+WZPGAaKtgkIPfbBovP1ZRP2qQKE3u67ueltnzcoCPH0nN1tUHdtgUorjlweUvn+zTJhkFeRuMShCOrbyHR5Xi3DOL4HCspXuVEOsWIr4Ape+l5MYPiFsQ6Lnw8LstqNjceWW 9EaV24eA0mVxq2xTG18h/INNKLB8cRUiEn9DI=; .ASPXAUTH=924A93387DBF416CAA58744C821850012D46928F064CB10A9AE6F52AF77BC58D3EE5F1D66898ED3236177984F1E9FE9E2E7701CFAD2DF356AEA246DD27DAABEFCB96 213175A63C1E55ADC59C50AFF945329024416F2A62388AF952AD8E2E2356ED140A104003A6E4F9E45F487E6BCA6F512382753FF263275349BC9BF5A95CE2] - person genxgeek; 25.04.2012
comment
พารามิเตอร์ตัวเลือกสำหรับการ uploadify เวอร์ชันปัจจุบัน (3.0) ล้าสมัยแล้ว... แต่ขอบคุณสำหรับความช่วยเหลือ :) - person Chris Barry; 09.05.2012
comment
นี่มันสมบูรณ์แบบ ขอบคุณที่ให้วิธีแก้ปัญหา! - person Bryan Denny; 25.07.2012
comment
ฉันหวังว่าฉันจะลงคะแนนได้มากกว่าหนึ่งครั้ง อย่างไรก็ตาม ควรเปลี่ยนพารามิเตอร์ Uploadify 'scriptData' เป็น 'formData' - person JSK NS; 20.03.2013