public static string GetScript(ScriptIds ids, string token) { var script = @"(function (v, t) { if (!t) t = 'hidden'; var chk = function () { var elem = document.getElementById('" + ids.ScriptId + @"'); document.getElementById('" + ids.HoneyPotContainerId + @"').style.display = 'none'; if (elem) { var newElem = document.createElement('div'); newElem.innerHTML = '<input type=""' + t + '"" name=""" + ids.TokenId + @""" value=""' + v + '"" />'; elem.parentNode.appendChild(newElem); elem.parentNode.removeChild(elem); } else { window.setTimeout(chk, 500); } }; window.setTimeout(chk, 500); document.getElementById('" + ids.HoneyPotContainerId + @"').style.display = 'none'; }('" + token + @"'));"; var compressor = new JavaScriptCompressor { Encoding = Encoding.UTF8, DisableOptimizations = false, ObfuscateJavascript = true, PreserveAllSemicolons = true, IgnoreEval = true, ThreadCulture = System.Globalization.CultureInfo.InvariantCulture }; var example = compressor.Compress(script); return(example); }
/// <summary> /// Writes a CAPTTIA element to the form. /// </summary> /// <param name="html"></param> /// <param name="request"></param> /// <returns></returns> public static MvcHtmlString Capttia(this HtmlHelper html, HttpRequestBase request) { var config = ConfigurationManager.GetSection("capttia") as CapttiaSection; var encryption = new Encryption(); var ids = new ScriptIds(config.ModuleName); string contextId = GetContextId(request, config, encryption); // Place it in a cookie var cookieContextId = encryption.Encrypt(contextId, config.PassPhraseB); request.RequestContext.HttpContext.Response.SetCookie(new HttpCookie(config.CookieName, cookieContextId) { HttpOnly = true, SameSite = SameSiteMode.Lax }); // Place it on the form var formId = encryption.Encrypt(contextId, config.PassPhrase); var token = JavaScript.EncodeForSingleQuotes(formId); return(MvcHtmlString.Create(GetHoneyPot(ids) + GetScriptElement(ids, token))); }
public async Task <IActionResult> OnPostAsync(int id) { ErrorMessage = ""; if (!ModelState.IsValid) { return(Page()); } try { var npc = await _db.Npcs.FindAsync(id); if (npc == null) { ErrorMessage = $"Npc {id} 不存在!"; return(Page()); } var content = DifferenceComparison(npc, Npc); _mapper.Map(Npc, npc); var npcScripts = _db.NpcScripts.Where(x => x.NpcId == id); foreach (var npcScript in npcScripts) { if (!ScriptIds.Contains(npcScript.ScriptId)) { _db.NpcScripts.Remove(npcScript); } else { ScriptIds.Remove(npcScript.ScriptId); } } foreach (var scriptId in ScriptIds) { _db.NpcScripts.Add(new NpcScriptEntity { NpcId = id, ScriptId = scriptId }); } await _db.SaveChangesAsync(); var result = await _mudClient.EditNpc(id); await AddSuccess(new OperatorLog { Type = OperatorLogType.修改Npc, Content = $"Id = {id},Data = {content}" }); } catch (Exception ex) { ErrorMessage = ex.Message; await AddError(new OperatorLog { Type = OperatorLogType.修改Npc, Content = $"Id = {id},Data={JsonConvert.SerializeObject(Npc)},ErrorMessage={ErrorMessage}" }); return(Page()); } return(Redirect(UrlReferer)); }
public static string GetScriptElement(ScriptIds ids, string token) { return(@"<script id=""" + ids.ScriptId + @""">" + GetScript(ids, token) + "</script>"); }
public static string GetHoneyPot(ScriptIds ids) { return(@"<div id=""" + ids.HoneyPotContainerId + @"""><input type=""text"" name=""" + ids.HoneyPotInputId + @""" value=""""></div>"); }