Last active
May 16, 2023 16:55
-
-
Save conwid/8a8730193f81f58070d970349b5f846b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Extending anti-forgery tokens in ASP.NET Core |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Item | |
{ | |
public int Data { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using AspNetSecurityDemos.Demos; | |
@model Item | |
@{ | |
ViewData["Title"] = "CreateItem"; | |
} | |
<h1>CreateItem</h1> | |
<h4>Item</h4> | |
<hr /> | |
<div class="row"> | |
<div class="col-md-4"> | |
<form asp-action="CreateItem"> | |
<div asp-validation-summary="ModelOnly" class="text-danger"></div> | |
<div class="form-group"> | |
<label asp-for="Data" class="control-label"></label> | |
<input asp-for="Data" class="form-control" /> | |
<span asp-validation-for="Data" class="text-danger"></span> | |
</div> | |
<div class="form-group"> | |
<input type="submit" value="Create" class="btn btn-primary" /> | |
</div> | |
</form> | |
</div> | |
</div> | |
<div> | |
<a asp-action="Index">Back to List</a> | |
</div> | |
@section Scripts { | |
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExpiringAntiforgeryAddtionalDataProvider : IAntiforgeryAdditionalDataProvider | |
{ | |
public string GetAdditionalData(HttpContext context) | |
{ | |
return DateTime.UtcNow.AddMinutes(5).ToString(); | |
} | |
public bool ValidateAdditionalData(HttpContext context, string additionalData) | |
{ | |
var isDate = DateTime.TryParse(additionalData, out var expirationDate); | |
return isDate && DateTime.UtcNow < expirationDate; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
builder.Services.AddSingleton<IAntiforgeryAdditionalDataProvider, ExpiringAntiforgeryAddtionalDataProvider>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment