0

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
}
QasemBSK
  • 190
  • 8
  • Why do you need to save it as a file before returning it in the response? Perhaps [this answer](https://stackoverflow.com/a/2433883/3034273) or [this answer](https://stackoverflow.com/a/1902482/3034273) can help? – Xerillio Jul 01 '23 at 19:22
  • Why are you using `UploadString` if you want to work with binary content? (And you should not be using `WebClient`: it's an effectively-obsoleted and horribly outdated part of .NET - you should be using `HttpClient` instead. – Dai Jul 01 '23 at 20:05

0 Answers0