Wenn Ihr mit RC2 den gesamten Code-Filter ignorieren alles, was SieWie Fälschungs Token in Ajax-Header mit IAuthorizationFilter in MVC6 RC1 RC2 validieren
StartUp.cs
services.AddAntiforgery(options => options.HeaderName = "YOUR_HEADER_NAME");
ist tun müssen, um für RC1
Früher habe ich einen IAuthorization-Filter verwendet, um AntiForgery-Tokens zu validieren, aber es sieht so aus, als hätten sich die Dinge in MVC6 drastisch geändert. Ich kann keine Dokumentation finden, die die Filteränderungen beschreibt.
Das ist mein alter Code
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class CustomValidateAntiForgeryTokenAttribute : FilterAttribute, IAuthorizationFilter
{
// why is this necessary..
// take a look here http://www.asp.net/web-api/overview/security/preventing-cross-site-request-forgery-%28csrf%29-attacks
// the CSRF token is inserted into the AJAX header
//
public Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
if (actionContext == null)
{
throw new ArgumentNullException("actionContext");
}
if (continuation == null)
{
throw new ArgumentNullException("continuation");
}
try
{
// insanely easy -- just extart the cookie token and the headertoken - it they are null who cares the validation will fail
var headerToken = actionContext.Request.Headers.GetValues("__RequestVerificationToken").FirstOrDefault(); // ajax headers
var cookieToken = actionContext.Request.Headers.GetCookies().Select(s => s[AntiForgeryConfig.CookieName]).FirstOrDefault();
AntiForgery.Validate(cookieToken.Value, headerToken);
}
catch (HttpAntiForgeryException)
{
actionContext.Response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.Forbidden,
RequestMessage = actionContext.ControllerContext.Request
};
return FromResult(actionContext.Response);
}
return continuation();
}
private Task<HttpResponseMessage> FromResult(HttpResponseMessage result)
{
var source = new TaskCompletionSource<HttpResponseMessage>();
source.SetResult(result);
return source.Task;
}
}
[Authorize]
public class SubmissionController : ApiController
{
[CustomValidateAntiForgeryToken] // note this is a custom filter, named to clarify its not the built in one as thats for views!
public HttpResponseMessage Delete(int? ID) {.. blah ..}
- Was ist der empfohlene Weg, die gleiche Sache in MVC6 zu erreichen?
- Kann mir jemand auf die Dokumentation verweisen?
Dank
[MVC Kernfilter Dokumentation] (https://docs.asp.net/en/latest/mvc/controllers/filters.html). Nicht sicher, was für eine Antwort Sie brauchen, scheint es ziemlich einfach (nicht zu erwähnen, nicht anders als MVC 5). – NightOwl888
Danke..Ich habe diese Dokumentation total vermisst – PingCrosby