Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting the current user in a minimal api

I am trying to refactor my api into a minimal api. Previously I’ve been using ControllerBase.HttpContext to get the user like this:

var emial = HttpContext.User.FindFirstValue(ClaimTypes.Email);

The method that I want to use for my endpoint mapping should be something like this:

public static void MapSurveyEndpoints(this WebApplication app) {
    app.MapPost("/api/Surveys", AddSurveysAsync);
}

public static async Task<Survey> AddSurveysAsync(ISurveyRepository repo, Survey survey) {
    var email = ...; //get current user email
    survey.UserEmail = email;
    return await repo.AddSurveysAsync(survey);
}

What would be another approach for getting the user without using controller?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can take the HttpContext as a parameter of your endpoint. ASP.NET Core will then provide that to you.

public static async Task<Survey> AddSurveysAsync(
    HttpContext context,
    ISurveyRepository repo,
    Survey survey)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading