Beispiel #1
0
        private void OutputFile(HttpListenerContext ctx, string filename)
        {
            ThreadPool.QueueUserWorkItem((obj) =>
            {
                ctx.Response.StatusCode = 200;
                ctx.Response.KeepAlive  = true;
                ctx.Response.AddHeader("Content-Type", GetContentType(filename));
                ctx.Response.AddHeader("content-disposition", "attachment;filename=" +
                                       HttpUtility.UrlEncode(Path.GetFileName(filename)));

                Stream writer  = ctx.Response.OutputStream;
                FileStream fsr = new FileStream(filename, FileMode.Open, FileAccess.Read);

                byte[] readBytes = new byte[BufferSize + 16];
                int len          = 0;
                while ((len = fsr.Read(readBytes, 0, readBytes.Length)) != 0)
                {
                    if (IsStopped)
                    {
                        break;
                    }
                    try
                    {
                        if (len < (BufferSize + 16))
                        {
                            byte[] rest = new byte[len];
                            Array.Copy(readBytes, rest, len);
                            byte[] en = SecurityUtils.AesDecrypt(Key, rest);
                            writer.Write(en, 0, en.Length);
                        }
                        else
                        {
                            byte[] en = SecurityUtils.AesDecrypt(Key, readBytes);
                            writer.Write(en, 0, en.Length);
                        }
                        writer.Flush();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("OutputFile error={0}", ex.Message);
                        break;
                    }
                }

                fsr.Close();
                writer.Close();
                ctx.Response.Close();
            });
        }
        internal bool IsPasswordValid(string FileName)
        {
            try
            {
                FileStream fsr       = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                byte[]     readBytes = new byte[HttpServer.BufferSize + 16];
                int        len       = fsr.Read(readBytes, 0, readBytes.Length);
                byte[]     en        = SecurityUtils.AesDecrypt(Password, readBytes);
                fsr.Close();

                return(en == null ? false : true);
            }
            catch { }
            return(false);
        }
Beispiel #3
0
        /*
         * 以上的2个配置 CookieName, AesKey 可以自己从别的地方读取配置.
         */


        /// <summary>
        /// 获取票据
        /// </summary>
        /// <typeparam name="T">用户附加数据类型</typeparam>
        /// <param name="getHeader">获取头里面的值</param>
        /// <returns>登陆时设置的票据</returns>
        public AuthTicket <T> GetTicket <T>(Func <string, string> getHeader)
        {
            var cipherText = getHeader(this.CookieName);

            if (String.IsNullOrEmpty(cipherText))
            {
                return(null);
            }
            var    cipherBytes = Convert.FromBase64String(cipherText);
            string plainText;

            try
            {
                plainText = SecurityUtils.AesDecrypt(cipherBytes, this.AesKey);
            }
            catch { return(null); }
            if (String.IsNullOrWhiteSpace(plainText))
            {
                return(null);
            }
            return(plainText.ParseJson <AuthTicket <T> >());
        }