Benutzer:MovGP0/ASP.NET Core/Anti-Forgery
aus Wikipedia, der freien Enzyklopädie
MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
Cross-Site Request Forgery (CSRF)Synchronizer Token Pattern (STP)<input type="hidden" name="csrftoken" value="KbyUmhTLMpYj7CD2di7JKP1P3qmLlkPt" />
services.AddAntiforgery(options => {
options.FormFieldName = "csrftoken";
options.RequireSsl = true;
});
CookieWird im HTTP-Header deklariert: Set-Cookie: Csrf-token=i8XNjC4b8KVok4uw5RftR38Wgp2BFwql; expires=Thu, 23-Jul-2015 10:25:33 GMT; Max-Age=31449600; Path=/
services.AddAntiforgery(options => {
options.CookieName = "CsrfCookie";
options.CookiePath = "/";
options.CookieDomain = "example.com";
options.RequireSsl = true;
});
HTTP-Header / REST
services.AddAntiforgery(options => {
options.HeaderName = "X-Csrf-Token";
options.RequireSsl = true;
});
There are multiple headers used:
Manuelle Validierung: csrf_token = HMAC(session_token, application_secret)
XMLHttpRequestsFor old Browsers that allow Cross-Site // pass if Origin header is ok
var expected = new Regex("^https?://myserver.com$"); // compare with URI for production code
var origin = request.Headers["Origin"].SingleOrDefault();
if(expected.Matches(origin)) return Next(request);
// pass if the request was not done with XmlHttpRequest
var requestedWith = request.Headers["X-Requested-With"];
if(!requestedWith.Any(rw => rw.Equals("XmlHttpRequest", StringComparison.InvariantCultureIgnoreCase))) return Next(request);
// deny otherwise
var response = context.Response;
response.StatusCode = 401;
return response.WriteAsync("Access denied.");
Verteilte .NET Core AnwendungBei einer verteilten .NET Core Anwendung muss das Application Secret ( Siehe auch: Microsoft.AspNetCore.Antiforgery Quellen
|