Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
AngularASPNETCore2WebApiAuth/src/Controllers/DashboardController.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
51 lines (45 sloc)
1.54 KB
This file contains 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 System.Linq; | |
using System.Security.Claims; | |
using System.Threading.Tasks; | |
using AngularASPNETCore2WebApiAuth.Data; | |
using AngularASPNETCore2WebApiAuth.Models.Entities; | |
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Identity; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.EntityFrameworkCore; | |
namespace AngularASPNETCore2WebApiAuth.Controllers | |
{ | |
[Authorize(Policy = "ApiUser")] | |
[Route("api/[controller]/[action]")] | |
public class DashboardController : Controller | |
{ | |
private readonly ClaimsPrincipal _caller; | |
private readonly ApplicationDbContext _appDbContext; | |
public DashboardController(UserManager<AppUser> userManager, ApplicationDbContext appDbContext, IHttpContextAccessor httpContextAccessor) | |
{ | |
_caller = httpContextAccessor.HttpContext.User; | |
_appDbContext = appDbContext; | |
} | |
// GET api/dashboard/home | |
[HttpGet] | |
public async Task<IActionResult> Home() | |
{ | |
// retrieve the user info | |
//HttpContext.User | |
var userId = _caller.Claims.Single(c => c.Type == "id"); | |
var customer = await _appDbContext.Customers.Include(c => c.Identity).SingleAsync(c => c.Identity.Id == userId.Value); | |
return new OkObjectResult(new | |
{ | |
Message = "This is secure API and user data!", | |
customer.Identity.FirstName, | |
customer.Identity.LastName, | |
customer.Identity.PictureUrl, | |
customer.Identity.FacebookId, | |
customer.Location, | |
customer.Locale, | |
customer.Gender | |
}); | |
} | |
} | |
} |