private string GetTestDataMD5(int pid) { string body = string.Format("gettestdatalist=1&pid={0}&time=1", pid); string res = _webClient.UploadString(_config.TaskFetchUrl, body, 3); return(MD5Encrypt.EncryptToHexString(res)); }
/// <summary> /// 生成校验Token /// </summary> public static string Create() { //Token = MD5( MD5( JudgerName + SecretKey ) + UtcDate ) string name = ConfigManager.Config.JudgerName; string secret = ConfigManager.Config.Password; string date = DateTime.UtcNow.ToString("yyyy-MM-dd"); string ns = MD5Encrypt.EncryptToHexString(name + secret); string token = MD5Encrypt.EncryptToHexString(ns + date); return(token); }
/// <summary> /// 保存单个文件到磁盘 /// </summary> /// <param name="file">上传的文件</param> /// <returns>是否保存成功</returns> public static IMethodResult AdminSaveUploadFile(HttpPostedFileBase file) { if (!AdminManager.HasPermission(PermissionType.Administrator)) { throw new NoPermissionException(); } if (file == null) { return(MethodResult.FailedAndLog("No file was uploaded!")); } if (String.IsNullOrEmpty(file.FileName)) { return(MethodResult.FailedAndLog("Filename can not be NULL!")); } FileInfo fi = new FileInfo(file.FileName); if (!UploadsManager.CheckFileExtension(fi.Extension, ALLOW_EXTENSTIONS)) { return(MethodResult.FailedAndLog("Filename is INVALID!")); } if (file.ContentLength <= 0) { return(MethodResult.FailedAndLog("You can not upload empty file!")); } String fileNewName = MD5Encrypt.EncryptToHexString(fi.Name + DateTime.Now.ToString("yyyyMMddHHmmssffff"), true) + fi.Extension; String savePath = Path.Combine(ConfigurationManager.UploadDirectoryPath, fileNewName); if (File.Exists(savePath)) { return(MethodResult.FailedAndLog("Filename exists!")); } file.SaveAs(savePath); return(MethodResult.SuccessAndLog <String>(fileNewName, "Admin upload file, name = {0}, origin = {1}", fileNewName, fi.Name)); }
/// <summary> /// 导入题目(不存在时返回null) /// </summary> /// <param name="request">Http请求</param> /// <param name="fileType">文件类型</param> /// <param name="uploadType">上传方式</param> /// <param name="content">文件内容</param> /// <param name="file">上传文件</param> /// <returns>题目数据是否插入成功集合(全部失败时为null)</returns> public static IMethodResult AdminImportProblem(HttpRequestBase request, String fileType, String uploadType, String content, HttpPostedFileBase file) { if (!AdminManager.HasPermission(PermissionType.SuperAdministrator)) { throw new NoPermissionException(); } if (!String.Equals("1", fileType)) { return(MethodResult.FailedAndLog("File type is INVALID!")); } if (String.Equals("1", uploadType))//从文件上传 { if (file == null) { return(MethodResult.FailedAndLog("No file was uploaded!")); } StreamReader sr = new StreamReader(file.InputStream); content = sr.ReadToEnd(); } //转换题库模型 List <ProblemEntity> problems = null; List <Byte[]> datas = null; List <Dictionary <String, Byte[]> > images = null; Dictionary <String, Byte[]> imagefiles = new Dictionary <String, Byte[]>(); if (!ProblemImport.TryImportFreeProblemSet(content, out problems, out datas, out images)) { return(MethodResult.FailedAndLog("File content is INVALID!")); } if (problems == null || problems.Count == 0) { return(MethodResult.FailedAndLog("No problem was imported!")); } //处理题目及图片路径 for (Int32 i = 0; i < problems.Count; i++) { problems[i].IsHide = true; problems[i].LastDate = DateTime.Now; if (images[i] == null) { continue; } String uploadRoot = ConfigurationManager.UploadDirectoryUrl; foreach (KeyValuePair <String, Byte[]> pair in images[i]) { if (pair.Value == null || !pair.Key.Contains(".")) { continue; } String oldUrl = pair.Key; String fileNewName = MD5Encrypt.EncryptToHexString(oldUrl + DateTime.Now.ToString("yyyyMMddHHmmssffff"), true) + pair.Key.Substring(pair.Key.LastIndexOf('.')); String newUrl = uploadRoot + fileNewName; problems[i].Description = problems[i].Description.Replace(oldUrl, newUrl); problems[i].Input = problems[i].Input.Replace(oldUrl, newUrl); problems[i].Output = problems[i].Output.Replace(oldUrl, newUrl); problems[i].Hint = problems[i].Hint.Replace(oldUrl, newUrl); imagefiles[fileNewName] = pair.Value; } } //将题目插入到数据库 List <Int32> pids = ProblemRepository.Instance.InsertEntities(problems); if (pids == null || pids.Count == 0) { return(MethodResult.FailedAndLog("Failed to import problem!")); } //保存题目数据 Dictionary <Int32, Boolean> dataadded = new Dictionary <Int32, Boolean>(); for (Int32 i = 0; i < pids.Count; i++) { if (pids[i] < 0) { continue; } try { if (datas[i] != null) { IMethodResult ret = ProblemDataManager.InternalAdminSaveProblemData(pids[i], datas[i]); if (!ret.IsSuccess) { return(ret); } dataadded[pids[i]] = true; } } catch { dataadded[pids[i]] = false; } ProblemCache.IncreaseProblemSetCountCache(); //更新缓存 ProblemCache.IncreaseProblemIDMaxCache(); //更新缓存 ProblemCache.RemoveProblemSetCache(GetProblemPageIndex(pids[i])); //删除缓存 } //保存题目图片 foreach (KeyValuePair <String, Byte[]> pair in imagefiles) { try { UploadsManager.InternalAdminSaveUploadFile(pair.Value, pair.Key); } catch { } } return(MethodResult.SuccessAndLog <Dictionary <Int32, Boolean> >(dataadded, "Admin import problem, id = {0}", String.Join(",", pids))); }
/// <summary> /// 申请找回密码 /// </summary> /// <param name="userName">用户名</param> /// <param name="email">电子邮箱</param> /// <param name="userip">用户IP</param> /// <param name="checkCode">验证码</param> /// <param name="link">找回密码链接</param> /// <returns>是否可以申请</returns> public static async Task <IMethodResult> RequestResetUserPassword(String userName, String email, String userip, String checkCode, String link) { if (!CheckCodeStatus.VerifyCheckCode(checkCode)) { return(MethodResult.Failed("The verification code you input didn't match the picture, Please try again!")); } if (!RegexVerify.IsUserName(userName)) { return(MethodResult.InvalidRequest(RequestType.User)); } if (!RegexVerify.IsEmail(email)) { return(MethodResult.Failed("Email address is INVALID!")); } UserEntity user = UserManager.InternalGetUserByNameAndEmail(userName, email); if (user == null) { return(MethodResult.Failed("The username \"{0}\" doesn't exist or the email is wrong!", userName)); } if (user.IsLocked) { return(MethodResult.Failed("The user is locked, please contact the administrator!")); } if (String.IsNullOrEmpty(user.Email) || "NULL".Equals(user.Email, StringComparison.OrdinalIgnoreCase)) { return(MethodResult.Failed("The user has no email, please contact the administrator!")); } Random rand = new Random(DateTime.Now.Millisecond); UserForgetPasswordEntity ufp = new UserForgetPasswordEntity() { UserName = userName, SubmitDate = DateTime.Now, SubmitIP = userip, HashKey = MD5Encrypt.EncryptToHexString(String.Format("{0}-{1}-{2}", userName, DateTime.Now.Ticks.ToString(), rand.Next(DateTime.Now.Millisecond)), true) }; Boolean success = UserForgetPasswordRepository.Instance.InsertEntity(ufp) > 0; if (!success) { return(MethodResult.Failed("Failed to process your request!")); } String url = ConfigurationManager.DomainUrl + ((link[0] == '/') ? link.Substring(1) : link); String mailSubject = ConfigurationManager.OnlineJudgeName + " Password Recovery"; String mailContent = UserForgetPasswordManager.GetMailContent(userName, url + ufp.HashKey.ToLowerInvariant()); try { await MailClient.SendMailAsync(ConfigurationManager.EmailSMTPServer, ConfigurationManager.EmailAddresser, email, mailSubject, mailContent, true, true, ConfigurationManager.EmailUsername, ConfigurationManager.EmailPassword); } catch { return(MethodResult.Failed("Failed to send a password reset link to your email address.")); } return(MethodResult.SuccessAndLog("User forget password, name = {0}", userName)); }