原文发布时间为: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. jQuery检测判断复选框是否被选中了的几种方法

    方法一:if ($("#checkbox-id")get(0).checked) {    // do something} 方法二:if($('#checkbox-id').is ...

  2. Apache安装之后,在浏览器输入ip无法访问

    博主本来在linux下面配置安装了apache,然后用浏览器输入ip却无法访问 就一直在想是不是dns无法解析的问题,最后才发现原来是防火墙的原因, 在linux下面 service iptables ...

  3. 010---Django与Ajax

    预备知识: 什么是Json? 定义:json是一种轻量级的数据交换格式. 如果我们要在不同的编程语言中传递对象,就必须把对象序列化为标准格式,比如XML,但那是以往的时代,现在大多数使用序列化为jso ...

  4. 理解线程3 c语言示例线程基本操作

    Table of Contents 1. 基本线程的动作 1.1. 设置线程属性 1.1.1. 设置脱离状态 1.1.2. 设置调度属性 1.2. 取消线程 1.3. 主线程创建多个线程示例 2. 了 ...

  5. JS是如何计算 1+1=2 的?

    身为程序员多年,作者今天突然对这件事感到十分好奇了.我问计算机芸芸部件,1+1究竟是如何计算的,他们都茫然的看着我. 打开谷歌浏览器->Console面板,大脑向双手不停发送生物电信号,肌肉细胞 ...

  6. 第四模块:网络编程进阶&数据库开发 口述

    进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 子进程死了之后 ,父进程关闭的时候要清理掉子进程的僵尸进程(收尸),孤儿进程是指父进程先死掉了的,交给init管理. join() 等待子进 ...

  7. CodeForces 771C Bear and Tree Jumps 树形DP

    题意: 给出一棵树,一个人可以在树上跳,每次最多跳\(k(1 \leq k \leq 5)\)个点 定义\(f(s,t)\)为从顶点\(s\)跳到顶点\(t\)最少需要跳多少次 求\(\sum\lim ...

  8. laravel5.5artisan命令

    目录 1. 简介 2. 编写命令 2.1 构建自己的命令 2.2 闭包命令 3. 定义输入期望 4.I/O 命令 5. 注册命令 6. 调用命令 1. 简介 Artisan 是 Laravel 自带的 ...

  9. js对数组去重的完整版

    数组去重是很常见的一个需求,而各种各样的姿势也很多,常见的如indexOf,或者hash,但是他们还是有缺陷,这里我查了一些资料做补充. 一般方式 //一般方法->使用indexOf Array ...

  10. python判断mongodb--find(),find_one()返回是否为空

    conn = MongoClient('127.0.0.1', 27017)db = conn.diffcollection = db['test1']result = collection.find ...