原文发布时间为:2011-10-18 —— 来源于本人的百度文章 [由搬家工具导入]

为什么jquery.uploadify不支持MVC的Authorize呢,因为flash的cookie跟服务端的不一样。

http://stackoverflow.com/questions/1729179/uploadify-session-and-authentication-with-asp-net-mvc

There was not "properly" an issue with my code. The usage of the plugin was generally correct but there was an issue with the authentication mechanism.

As everybody can find on the internet the flash plugin does not share the authentication cookie with the server-side code and this was the reason behind the usage of the "scriptData" section inside my code that contained the Authentication Cookie.

The problem was related to the fact that the controller was decorated with the [Authorize] attribute and this was never letting the request reach its destination.

The solution, found with the help of another user on the uploadify forum, is to write a customized version of the AuthorizeAttribute like you can see in the following code.

/// <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)]
publicclassTokenizedAuthorizeAttribute:AuthorizeAttribute
{
    /// <summary>
    /// The key to the authentication token that should be submitted somewhere in the request.
    /// </summary>
    privateconststring TOKEN_KEY ="AuthenticationToken";

    /// <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>
    protectedoverrideboolAuthorizeCore(System.Web.HttpContextBase httpContext ){
        string token = httpContext.Request.Params[TOKEN_KEY];

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

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

        returnbase.AuthorizeCore( httpContext );
    }
}

http://www.uploadify.com/download/

jquery.uploadify不支持MVC的Authorize的更多相关文章

  1. jquery.uploadify上传文件配置详解(asp.net mvc)

    页面源码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...

  2. asp.net mvc 2.o 中使用JQuery.uploadify

    From:http://www.cnblogs.com/strugglesMen/archive/2011/07/01/2095916.html 官方网站http://www.uploadify.co ...

  3. MVC 4 结合jquery.uploadify 上传实例

    前言:由于今天公司源代码服务瘫痪,没法编写代码,利用这个有限的时间,做了一个小小的 基于MVC的图片上传demo,貌似近些年来MVC十分火爆,相关的demo也数不胜数,小弟就在这里打着MVC的旗子,狐 ...

  4. jquery.uploadify+spring mvc实现上传图片

    一.前端页面 1.下载jquery.uploadify 去uploadify官网(http://www.uploadify.com/download/)下载压缩包,解压后放在如下路径: 2.html结 ...

  5. MVC中使用jquery uploadify上传图片报302错误

    使用jquery uploadify上传图片报302错误研究了半天,发现我上传的action中有根据session判断用户是否登录,如果没有登录就跳到登陆页,所以就出现了302跳转错误.原来更新了fl ...

  6. jquery.uploadify文件上传组件

    1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...

  7. jQuery.uploadify文件上传组件实例讲解

    1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...

  8. 解决jQuery uploadify在非IE核心浏览器下无法上传

    之前上传了一个通过Flash实现多文件上传,但是在IE正常运行,FireFox 不能正常上传.经过反复研究学习,之所以firefox和360浏览器无法正常运行,是因为FireFox.chrome.36 ...

  9. 使用jQuery Uploadify在ASP.NET 上传附件

    Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.Uploadify官方网址:http://www.uploadify.com/,在MVC中使用的方法可以参考 jQuer ...

随机推荐

  1. PHP 多参数方法的重构

    假设我们要完成一个保存文章的功能,如果采用函数编程的方式,大概会是下面这个样子: <?php function saveArticle($title, $content, $categoryId ...

  2. SpringCloud框架搭建+实际例子+讲解+系列五

    (4)服务消费者,面向前端或者用户的服务 本模块涉及到很多知识点:比如Swagger的应用,SpringCloud断路器的使用,服务API的检查.token的校验,feign消费者的使用.大致代码框架 ...

  3. python3 练习题100例 (十六)鸡尾酒疗法

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Fan Lijun' n = input('请输入一个大于1,小于等于20的整 ...

  4. Codeforces Round #458C DP

    C. Travelling Salesman and Special Numbers time limit per test 1 second memory limit per test 256 me ...

  5. spring源码学习中的知识点

    一.循环依赖 循环依赖就是循环引用,就是两个或多个bean之间互相持有对方. 1.构造器循环依赖 表示通过构造器注入造成的循环依赖,此依赖是无法解决的,只能抛出BeanCurrentlyInCreat ...

  6. 17,saltstack高效运维

      salt介绍 saltstack是由thomas Hatch于2011年创建的一个开源项目,设计初衷是为了实现一个快速的远程执行系统. salt强大吗 系统管理员日常会进行大量的重复性操作,例如安 ...

  7. javaScript对SEO的影响

    在两大搜索引擎阵营中,大量实践证明百度对JAVASCRIP的处理很不理想而GOOGLE的处理要好一些.   网页中出现大量的JavaScript会给搜索引擎爬行增加难度.其主要影响有以下几点: 1.干 ...

  8. BP神经网络的手写数字识别

    BP神经网络的手写数字识别 ANN 人工神经网络算法在实践中往往给人难以琢磨的印象,有句老话叫“出来混总是要还的”,大概是由于具有很强的非线性模拟和处理能力,因此作为代价上帝让它“黑盒”化了.作为一种 ...

  9. [转]grep 在文本中查找内容

    转自: http://www.lampweb.org/linux/3/27.html 功能:grep系列是Linux中使用频率最高的文本查找命令.主要功能在一个或者多个文件中查找特定模式的字符串.如果 ...

  10. javascript检测数组

    在ECMAScript5中的数组已经引入了isArray方法,该方法的目的就是检测变量是否为数组. 但是对于ie6.7等古老的浏览器是没有这样的方法的,在Zakas写的一本书上摘到一个函数,基本能优雅 ...