public IActionResult Index([FromServices] IGamesService games) { GameNewVm model = new GameNewVm() { Hand = GetRandomHand(), GoogleRecaptchaApiKey = _config.GoogleRecaptcha.ApiKey, }; if (!string.IsNullOrEmpty(this.Request.Query["prev"]) && Guid.TryParse(this.Request.Query["prev"], out Guid id) && games.Exist(id)) { bool a = this.Request.Query["a"] == "1"; Game game = games.Get(id); model.NameAuthor = a ? game.NameAuthor : game.NameOpponent; model.NameOpponent = a ? game.NameOpponent : game.NameAuthor; model.Memo = game.Memo; model.Trophy = game.Trophy; } return(View(model)); }
public async Task <IActionResult> Index(GameNewVm model, [FromServices] GoogleRecaptchaService recaptcha, [FromServices] IGamesService games) { model.GoogleRecaptchaApiKey = this._config.GoogleRecaptcha.ApiKey; if (ModelState.IsValid) { //Captcha string recaptchaResponse = this.Request.Form["g-recaptcha-response"]; if (!await recaptcha.ValidateRecaptcha(this._config.GoogleRecaptcha.ApiSecret, recaptchaResponse)) { return(ViewAlertMessageDanger(model, "Access denied", "Invalid captcha token.")); } //Create form Guid session = Guid.NewGuid(); Game game = new Game() { CreatedAt = DateTime.UtcNow, HandAuthor = model.Hand, ID_Session = session, NameAuthor = model.NameAuthor, NameOpponent = model.NameOpponent, Memo = model.Memo, Trophy = model.Trophy, }; Game result = games.Create(game); //Create authors cookie this.Response.Cookies.Append("GameRPS_" + result.ID.ToString(), session.ToString(), new Microsoft.AspNetCore.Http.CookieOptions() { Expires = DateTime.Now.AddMonths(1), }); return(RedirectAlertInfo("/" + result.ID.ToString(), "Success", "The game has been successfully created.")); } return(ViewAlertMessageDanger(model, "No valid data", "Please enter valid data: login, password, captcha.")); }