iForm auto (#234)
parent
d7445f075d
commit
c43ce9945b
|
@ -1,3 +1,6 @@
|
|||
#define FIRST // FIRST SECOND
|
||||
#if NEVER
|
||||
#elif FIRST
|
||||
// <snippet_1>
|
||||
using Microsoft.AspNetCore.Antiforgery;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
|
@ -18,6 +21,42 @@ app.MapGet("/", (HttpContext context, IAntiforgery antiforgery) =>
|
|||
return Results.Content(html, "text/html");
|
||||
});
|
||||
|
||||
app.MapPost("/upload", async Task<Results<Ok<string>, BadRequest<string>>>
|
||||
([FromForm] FileUploadForm fileUploadForm, HttpContext context,
|
||||
IAntiforgery antiforgery) =>
|
||||
{
|
||||
await MyUtils.SaveFileWithName(fileUploadForm.FileDocument!,
|
||||
fileUploadForm.Name!, app.Environment.ContentRootPath);
|
||||
return TypedResults.Ok($"Your file with the description:" +
|
||||
$" {fileUploadForm.Description} has been uploaded successfully");
|
||||
});
|
||||
|
||||
app.Run();
|
||||
// </snippet_1>
|
||||
|
||||
// The following code shows how to validate the anti-forgery token manually when
|
||||
// not using model binding but using Request.ReadFormAsync() to manually read the form.
|
||||
#elif SECOND
|
||||
// <snippet_2>
|
||||
using Microsoft.AspNetCore.Antiforgery;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
var builder = WebApplication.CreateBuilder();
|
||||
|
||||
builder.Services.AddAntiforgery();
|
||||
|
||||
var app = builder.Build();
|
||||
app.UseAntiforgery();
|
||||
|
||||
// Generate a form with an anti-forgery token and an /upload endpoint.
|
||||
app.MapGet("/", (HttpContext context, IAntiforgery antiforgery) =>
|
||||
{
|
||||
var token = antiforgery.GetAndStoreTokens(context);
|
||||
var html = MyUtils.GenerateHtmlForm(token.FormFieldName, token.RequestToken!);
|
||||
return Results.Content(html, "text/html");
|
||||
});
|
||||
|
||||
app.MapPost("/upload", async Task<Results<Ok<string>,BadRequest<string>>>
|
||||
([FromForm] FileUploadForm fileUploadForm, HttpContext context,
|
||||
IAntiforgery antiforgery) =>
|
||||
|
@ -37,7 +76,8 @@ app.MapPost("/upload", async Task<Results<Ok<string>,BadRequest<string>>>
|
|||
});
|
||||
|
||||
app.Run();
|
||||
// </snippet_1>
|
||||
// </snippet_2>
|
||||
#endif
|
||||
|
||||
public class FileUploadForm
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue