/// <summary> /// 设置登录时设置的票据并返回 /// </summary> /// <param name="uid">用户标识</param> /// <param name="roles">用户拥有的角色</param> /// <param name="days">有效时间</param> /// <param name="userData">用户附加数据(不需要可以设置成null)</param> /// <returns>认证票据</returns> public Dictionary <string, string> SignIn(string uid, int days, string[] roles, object userData) { //在这里, 将用户标识(Uid), 用户角色(Roles)和盐值拼接成字符串 //解密的时候也要按照这个格式来拆分获取 var expire = DateTime.Now.AddDays(days); var plainText = AuthTicket <object> .Create(uid, roles, userData, expire).ToJson(); var cipherBytes = SecurityUtils.AesEncrypt(plainText, AesKey); var cipherText = Convert.ToBase64String(cipherBytes); var cookieValue = AuthUtils.CreateCookieValue(this.CookieName, cipherText, expire, "/"); var headers = new Dictionary <string, string> { { "Set-Cookie", cookieValue } }; return(headers); }
private void Encrypt(string key, string filename, string outfile) { new Thread(() => { IsRunning = true; int tmp = 0, pro = 0; long total = 0; long finished = 0; try { SaveThumbnail(filename); FileStream fsr = new FileStream(filename, FileMode.Open); FileStream fout = new FileStream(outfile, FileMode.OpenOrCreate); total = fsr.Length; if (OnStart != null) { OnStart(total); } byte[] readBytes = new byte[HttpServer.BufferSize]; int len = 0; while ((len = fsr.Read(readBytes, 0, readBytes.Length)) != 0) { finished += len; if (IsStopped) { Logger.Warn("用户取消加密进程"); break; } if (len < HttpServer.BufferSize) { byte[] rest = new byte[len]; Array.Copy(readBytes, rest, len); byte[] en = SecurityUtils.AesEncrypt(key, rest); fout.Write(en, 0, en.Length); } else { byte[] en = SecurityUtils.AesEncrypt(key, readBytes); fout.Write(en, 0, en.Length); } // tmp = (int)(finished * 100 / total); if (pro < tmp) { pro = tmp; if (OnProgress != null) { OnProgress(pro); } } } fsr.Close(); fout.Close(); } catch (Exception ex) { if (OnError != null) { OnError(ex.Message); } } IsRunning = false; if (OnFinished != null) { OnFinished(); } }).Start(); }