Using the authenticated user when posting questions and answers
Now that our REST API knows about the user interacting with it, we can use this to post the correct user against questions and answers. Let's carry out the following steps:
- We'll start by adding the following
using
statements inQuestionsController.cs
:using System.Security.Claims; using Microsoft.Extensions.Configuration; using System.Net.Http; using System.Text.Json;
- Let's focus on posting a question first by posting it with the authenticated user's ID:
public async ...   PostQuestion(QuestionPostRequest    questionPostRequest) {   var savedQuestion =     await _dataRepository.PostQuestion(new       QuestionPostFullRequest     {       Title = questionPostRequest.Title,       Content = questionPostRequest.Content,  &...