csharppublicclassUserController:ApiController{// GET api/user/5[HttpGet]publicIHttpActionResultGetUser(intid){Useruser=Database.GetUserById(id);if(user!=null){returnOk(user);}returnNotFound();}}
1. Add the `[Authorize]` attribute on the UserController class to enable the authorization check for all the actions within the controller. 2. Check if the User.Identity.Name (the authenticated user's username) is the same as the username of the requested user (user.Username). This ensures that only the authenticated user can access their own information.
csharp[Authorize]// Add this attribute to enable authorizationpublicclassUserController:ApiController{// GET api/user/5[HttpGet]publicIHttpActionResultGetUser(intid){Useruser=Database.GetUserById(id);// Ensure that the user requesting the data is the same as the data requestedif(user!=null&&User.Identity.Name==user.Username){returnOk(user);}returnNotFound();}}