hanyadikhetvan-dotnethf/HanyadikHetVan/Controllers/V1/TestController.cs

49 lines
1.4 KiB
C#
Raw Permalink Normal View History

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;
2021-05-19 23:14:57 +02:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
2021-05-19 04:33:53 +02:00
using System;
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 23:14:57 +02:00
return this.User.FindFirst("user_role").Value;
2021-05-19 04:33:53 +02:00
}
2021-05-19 05:08:00 +02:00
2021-05-19 23:14:57 +02:00
[Authorize(Policy = "AdminPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
2021-05-19 05:08:00 +02:00
[HttpGet("roleprotected")]
[Produces(MediaTypeNames.Application.Json)]
public string RoleProtected()
{
return this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
}
2021-05-19 04:33:53 +02:00
}
}