惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
爱范儿
爱范儿
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
IT之家
IT之家
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
C
Check Point Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - Franky
G
Google Developers Blog
V
V2EX
雷峰网
雷峰网
美团技术团队
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
V
Visual Studio Blog
J
Java Code Geeks

博客园 - Xia.CJ

C# 委托 node.js中连接mongodb(Please ensure that you set the default write...)解决办法 node.js api解读之file system模块 Node.js小技巧 在windows上安装新版node.js-node.js学习笔记 Asp.Net MVC部暑托管服务器iis7提示403错误解决方法 mvc3中使用unobtrusive时,ajax更新加载页面后验证失效解决方法 ashx中使用HttpContext.Current.Session ,出现未将对象引用设置到实例上 搜索引擎(google/百度)高级指令 MVC中用Html.RenderPartial还是Html.RenderAction或者Html.Partial-Xia.CJ 在_Layout使用Html.RenderAction的问题-MVC3(Razor)问题 无法删除此对象,因为未在 ObjectStateManager 中找到它-entity framework删除时 Javascript数组(array)操作 序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用 entity framework(EF)_code first复杂类型(Complex Types)问题 entity framework多对多关系查询 JQuery获取checkbox值,checkbox全选,操作checkbox JQuery操作Select JQuery动态创建DOM、表单
在mvc3中使用uploadify上传组件User.isAuthenticated等于fals...
Xia.CJ · 2012-04-27 · via 博客园 - Xia.CJ

我们前台html这样定义

  @{
 var auth = Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
}
<script  src="@Url.Content("~/Uploadify/swfobject.js")" type="text/javascript"></script>
<script  src="@Url.Content("~/Uploadify/jquery.uploadify.v2.1.4.min.js")" type="text/javascript"></script>
<script type="text/javascript">
// <![CDATA[
    $(document).ready(function () {
        $('#file_upload').uploadify({
            'uploader': '@Url.Content("~/Uploadify/uploadify.swf")',
            'script': '@Url.Content("~/Admin/Product/Process")',
            'cancelImg': '@Url.Content("~/uploadify/cancel.png")',
            'folder': '@Url.Content("~/Content/goods")',
            // 'scriptData': { 'ASPSESSID':@sessionId' },
            'auto': true,
            'multi': true,
            'queueID': 'custom-queue',
            'fileExt': '*.jpg;*.gif;*.png',
            'fileDesc': 'Image Files (.JPG, .GIF, .PNG)',
            'queueSizeLimit': 10,
            'onSelectOnce': function (event, data) {
                $('#file_upload').uploadifySettings('scriptData', {'token': '@auth' });
                $('#status-message').text(data.filesSelected + ' files have been added to the queue.');

            },
            'onAllComplete': function (event, data) {
                $('#status-message').text(data.filesUploaded + ' files uploaded, ' + data.errors + ' errors.');
            }
        });
    });
// ]]>

注意这句

$('#file_upload').uploadifySettings('scriptData', {'token': '@auth' });
选择文件的时候,我们把auth也返回到服务器上.注意这里参数名"token"
然后我们后台:
public class UploadAuthenticationAttribute : AuthorizeAttribute
    {
        private const string TOKEN_KEY = "token";
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {

           
                string token = httpContext.Request.Params[TOKEN_KEY];

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

                    if (ticket != null)
                    {
                        FormsIdentity identity = new FormsIdentity(ticket);
                        string[] roles = {"Administrators" }; //System.Web.Security.Roles.GetRolesForUser(identity.Name); 注意这里,如果你没有启用asp.net权限系统,会报错。
                        GenericPrincipal principal = new GenericPrincipal(identity, roles);
                        httpContext.User = principal;
                    }
                }
           
            return base.AuthorizeCore(httpContext);
        }

在你要上传的action上面加个[UploadAuthentication]就行了。

例如:

[UploadAuthentication]
        public void Process(HttpPostedFileBase fileData, string folder)
{
 //dosomething...
}