频道栏目
首页 > 资讯 > 网站安全 > 正文

ASP.NET上传判断类型,防止黑客上传非法文件

09-07-29        来源:[db:作者]  
收藏   我要投稿
检测文件类型函数类:   
using System;   
using System.Collections.Generic;   
using System.Web.UI.WebControls;   
using System.IO;   
namespace 0x001.Pack   
{   
        public enum FileExtension   
        {   
                JPG = 255216,   
                GIF = 7173,   
                BMP = 6677,   
                PNG = 13780   
                // 255216 jpg;   
                // 7173 gif;   
                // 6677 bmp,   
                // 13780 png;   
                // 7790 exe dll,   
                // 8297 rar   
                // 6063 xml   
                // 6033 html   
                // 239187 aspx   
                // 117115 cs   
                // 119105 js   
                // 210187 txt   
                //255254 sql   
        }   
        public class FileValidation   
        {   
static bool IsAllowedExtension() static bool IsAllowedExtension(FileUpload fu, FileExtension[] fileEx)   
                {   
                        int fileLen = fu.PostedFile.ContentLength;   
                        byte[] imgArray = new byte[fileLen];   
                        fu.PostedFile.InputStream.Read(imgArray, 0, fileLen);   
                        MemoryStream ms = new MemoryStream(imgArray);   
                        System.IO.BinaryReader br = new System.IO.BinaryReader(ms);   
                        string fileclass = "";   
                        byte buffer;   
                        try   
                        {   
                                buffer = br.ReadByte();   
                                fileclass = buffer.ToString();   
                                buffer = br.ReadByte();   
                                fileclass += buffer.ToString();   
                        }   
                        catch   
                        {   
                        }   
                        br.Close();   
                        ms.Close();   
                        foreach (FileExtension fe in fileEx)   
                        {   
                                if (Int32.Parse(fileclass) == (int)fe)   
                                        return true;   
                        }   
                        return false;   
     &nbs
相关TAG标签
上一篇:轻松破解Win2008口令-ERD6.0
下一篇:科普:浅谈计算机语言层级
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站