AntiForgeryToken validation failing while deploy on server - 400 Bad Request
See original GitHub issueI am working on an Angular app with .NET Core and Web API implementation. I am trying to implement Anti forgery Token for APIs. This is working all the time on my local machine but when I deploy this on the Dev environment (cloud) , APIs are sometime working and sometime not working. And When its not working I am getting 400- Bad Request error.
Here is my configuration, Please let me know where did I implement wrong ?
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
this.ConfigureServicesForAuth(services);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
services.AddMvc(options =>
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()));
}
Interceptor
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const headername = 'X-XSRF-TOKEN';
const requestToken = this.getCookieValue(environment.TokenName); // declared in environment file with name XSRF-REQUEST-TOKEN-R
req = req.clone({
headers: req.headers.append(headername, requestToken)
.append('Cache-Control', 'no-cache')
.append('Pragma', 'no-cache')
.append('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT')
});
Anti Forgery Controller
public class AntiForgeryController : Controller
{
private IAntiforgery _antiForgery;
public AntiForgeryController(IAntiforgery antiForgery)
{
_antiForgery = antiForgery;
}
[Route("antiforgerytoken")]
[IgnoreAntiforgeryToken]
public IActionResult GenerateAntiForgeryTokens()
{
var tokens = _antiForgery.GetAndStoreTokens(HttpContext);
Response.Cookies.Append("XSRF-REQUEST-TOKEN-R", tokens.RequestToken, new Microsoft.AspNetCore.Http.CookieOptions
{
Path="/",
HttpOnly = false
});
return NoContent();
}
[Route("Startupcall")]
[IgnoreAntiforgeryToken]
[HttpPost]
public IActionResult Startupcall()
{
return NoContent();
}
}
Service
public GetandSetToken() {
return this.http.post('api/AntiForgery/Startupcall', {
}).pipe(
switchMap(_ => this.http.get('api/AntiForgery/antiforgerytoken'))
);
}
Page-base-component.ts
ngOnInit() {
this.sharedService.GetandSetToken().subscribe(result => {
});}
Finally in a Controller Method
[Authorize]
[Route("api/[controller]")]
[AutoValidateAntiforgeryToken]
public partial class EmployeeController : Controller
{
private NewDomain _newDomain;
public MyController(NewDomain newDomain)
{
this._newDomain = newDomain;
}
[Authorize]
[HttpPost]
[Route("DeleteEmployees")]
public async Task<IActionResult> DeleteEmployees(int departmentId , [FromBody]IEnumerable<EmployeeDetails> objListEmployeeDetails)
{
await _newDomain.DeleteEmployees(objListEmployeeDetails);
return Ok();
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Net Core ValidateAntiForgeryToken throwing web api 400 ...
Visual Studio 2017 with Web Api using .net Core 1.1 I'm using, but I am getting a 400 Bad Request Error. Error Occurs...
Read more >Anti-Forgery Validation in ASP.NET Core - Simple Talk
Hackers use the cross-site request forgery technique to grab the ... The exception being thrown results in a HTTP 400 Bad Request code....
Read more >Anti-Forgery Token Issue in ASP.NET Core 2.1+ Applications
NET Core 2.1+ application with anti-forgery token. ... Failed to load resource: the server responded with a status of 400 (Bad Request) Uncaught...
Read more >ValidateAntiforgeryToken: Debug Bad Request Error - YouTube
Debugging BadRequest error (Status code 400 ) can be very frustrating. In this tutorial I will show how to debug this error and...
Read more >Request Verification in Razor Pages
NET Core processes a POST request. If verification fails, the framework returns an HTTP status code of 400, signifying a Bad Request.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Does your deployment run more than one instance?
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.
See our Issue Management Policies for more information.