* Use suggested Antiforgery AJAX patern.
This commit is contained in:
parent
6d91b8d9fd
commit
a182eca6d4
|
|
@ -1,10 +1,8 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Antiforgery;
|
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using Microsoft.Extensions.Primitives;
|
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
using MusicStore.ViewModels;
|
using MusicStore.ViewModels;
|
||||||
|
|
||||||
|
|
@ -59,28 +57,11 @@ namespace MusicStore.Controllers
|
||||||
//
|
//
|
||||||
// AJAX: /ShoppingCart/RemoveFromCart/5
|
// AJAX: /ShoppingCart/RemoveFromCart/5
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> RemoveFromCart(
|
public async Task<IActionResult> RemoveFromCart(
|
||||||
[FromServices] IAntiforgery antiforgery,
|
|
||||||
int id,
|
int id,
|
||||||
CancellationToken requestAborted)
|
CancellationToken requestAborted)
|
||||||
{
|
{
|
||||||
var cookieToken = string.Empty;
|
|
||||||
var formToken = string.Empty;
|
|
||||||
StringValues tokenHeaders;
|
|
||||||
string[] tokens = null;
|
|
||||||
|
|
||||||
if (HttpContext.Request.Headers.TryGetValue("RequestVerificationToken", out tokenHeaders))
|
|
||||||
{
|
|
||||||
tokens = tokenHeaders.First().Split(':');
|
|
||||||
if (tokens != null && tokens.Length == 2)
|
|
||||||
{
|
|
||||||
cookieToken = tokens[0];
|
|
||||||
formToken = tokens[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
antiforgery.ValidateTokens(HttpContext, new AntiforgeryTokenSet(formToken, cookieToken));
|
|
||||||
|
|
||||||
// Retrieve the current user's shopping cart
|
// Retrieve the current user's shopping cart
|
||||||
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.Hosting;
|
|
||||||
using Microsoft.AspNet.Identity.EntityFramework;
|
using Microsoft.AspNet.Identity.EntityFramework;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,9 @@
|
||||||
|
|
||||||
@functions
|
@functions
|
||||||
{
|
{
|
||||||
public string GetAntiXsrfToken()
|
public string GetAntiXsrfRequestToken()
|
||||||
{
|
{
|
||||||
var tokens = Xsrf.GetTokens(Context);
|
return Xsrf.GetAndStoreTokens(Context).RequestToken;
|
||||||
return tokens.CookieToken + ":" + tokens.RequestToken;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -26,10 +25,12 @@
|
||||||
// Perform the ajax post
|
// Perform the ajax post
|
||||||
$.ajax(PostToUrl, {
|
$.ajax(PostToUrl, {
|
||||||
type: "post",
|
type: "post",
|
||||||
data: { "id": recordToDelete },
|
data: {
|
||||||
|
"id": recordToDelete
|
||||||
|
},
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
headers: {
|
headers: {
|
||||||
"RequestVerificationToken": '@GetAntiXsrfToken()'
|
"RequestVerificationToken": '@GetAntiXsrfRequestToken()'
|
||||||
}
|
}
|
||||||
}).done(function (data) {
|
}).done(function (data) {
|
||||||
// Successful requests get here
|
// Successful requests get here
|
||||||
|
|
|
||||||
|
|
@ -188,7 +188,7 @@ namespace MusicStore.Controllers
|
||||||
controller.ControllerContext.HttpContext = httpContext;
|
controller.ControllerContext.HttpContext = httpContext;
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await controller.RemoveFromCart(antiForgery, cartItemId, CancellationToken.None);
|
var result = await controller.RemoveFromCart(cartItemId, CancellationToken.None);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var jsonResult = Assert.IsType<JsonResult>(result);
|
var jsonResult = Assert.IsType<JsonResult>(result);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue