I am creating an Asp.net Core WebAPI project. I want to check Logined user's username in some endpoints.
[HttpGet]
public IActionResult EndPoint1() // using HttpContext
{
var userName = HttpContext.User.Identity.Name;
// diong some action with userName
// doing some action in endpoint1
}
[HttpGet]
public IActionResult EndPoint2() // using HttpContext
{
var userName = HttpContext.User.Identity.Name; // Same Endpoint1
// diong some action with userName // Same Endpoint1
// doing some action in endpoint2
}
[HttpGet]
public IActionResult EndPoint3() // Without using HttpContext
{
// doing some action in endpoint3
}
In Endpoint1 and Endpoint2, I have same code :
var userName = HttpContext.User.Identity.Name;
// diong some action with userName
Can I create a custom attribute (For example [CheckUser]) and use it on endpoints?
Like this:
[HttpGet]
[CheckUser] // optional attribute.
public IActionResult EndPoint1(){
// doing some action in endpoint1
}
[HttpGet]
[CheckUser] // optional attribute.
public IActionResult EndPoint2(){
// doing some action in endpoint2
}