2021-05-19 17:56:56 +02:00
|
|
|
|
using HanyadikHetVan.Data.Entities;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-05-19 04:33:53 +02:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System;
|
2021-05-19 17:56:56 +02:00
|
|
|
|
using System.Data;
|
2021-05-19 05:08:00 +02:00
|
|
|
|
using System.Net.Mime;
|
2021-05-19 04:33:53 +02:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
|
|
|
|
namespace HanyadikHetVan.Controllers.V1
|
|
|
|
|
{
|
|
|
|
|
[ApiVersion("1.0")]
|
|
|
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class TestController : Controller
|
|
|
|
|
{
|
2021-05-19 17:56:56 +02:00
|
|
|
|
private UserManager<User> _userManager;
|
2021-05-19 04:33:53 +02:00
|
|
|
|
|
2021-05-19 17:56:56 +02:00
|
|
|
|
public TestController(UserManager<User> userManager)
|
2021-05-19 04:33:53 +02:00
|
|
|
|
{
|
|
|
|
|
_userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("unprotected")]
|
2021-05-19 05:08:00 +02:00
|
|
|
|
[Produces(MediaTypeNames.Application.Json)]
|
2021-05-19 04:33:53 +02:00
|
|
|
|
public string Unprotected()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return "Unprotected";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpGet("protected")]
|
2021-05-19 05:08:00 +02:00
|
|
|
|
[Produces(MediaTypeNames.Application.Json)]
|
2021-05-19 04:33:53 +02:00
|
|
|
|
public string Protected()
|
|
|
|
|
{
|
2021-05-19 17:56:56 +02:00
|
|
|
|
return this.User.FindFirst(ClaimTypes.Role).Value;
|
2021-05-19 04:33:53 +02:00
|
|
|
|
}
|
2021-05-19 05:08:00 +02:00
|
|
|
|
|
|
|
|
|
[Authorize(Roles = "admin")]
|
|
|
|
|
[HttpGet("roleprotected")]
|
|
|
|
|
[Produces(MediaTypeNames.Application.Json)]
|
|
|
|
|
public string RoleProtected()
|
|
|
|
|
{
|
|
|
|
|
return this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
|
|
|
|
|
}
|
2021-05-19 04:33:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|