services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>,
AdditionalUserClaimsPrincipalFactory>();
public class AdditionalUserClaimsPrincipalFactory
: UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
{
public AdditionalUserClaimsPrincipalFactory(
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, roleManager, optionsAccessor)
{ }
public override async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var principal = await base.CreateAsync(user);
var identity = (ClaimsIdentity)principal.Identity;
var claims = new List<Claim>();
claims.Add(user.IsAdmin ? new Claim(JwtClaimTypes.Role, "admin") : new Claim(JwtClaimTypes.Role, "user"));
identity.AddClaims(claims);
return principal;
}
}
public class ApplicationUser : IdentityUser
{
[PersonalData]
public string Name { get; set; }
[PersonalData]
public DateTime BirthDate { get; set; }
public bool IsAdmin { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{
var con = _configuration.GetConnectionString("telemetry");
services.AddDbContext<TelemetryContext>(options => options.UseSqlServer(con));
}
[ApiController]
[Route("api/[controller]")]
public class TelemetryController : ControllerBase
{
private const int Interval = 15;
private readonly TelemetryContext _ctx;
public TelemetryController(TelemetryContext context)
{
_ctx = context;
}
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Post(TelemetryModel model)
{
if (ModelState.IsValid)
{