Skip to content

Instantly share code, notes, and snippets.

@mrnkr
Last active April 9, 2020 00:03
Show Gist options
  • Save mrnkr/4d0e493458a9ed8b86ec1246bbbd053c to your computer and use it in GitHub Desktop.
Save mrnkr/4d0e493458a9ed8b86ec1246bbbd053c to your computer and use it in GitHub Desktop.
This is how I retrieve the user from my request without tears
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace TwoDrive.Api
{
public static class ExtractUserExtension
{
protected static string GetLoggedUserId(this ControllerBase ctrl)
{
return ctrl.User.Claims
.Where(c => c.Type == ClaimTypes.NameIdentifier)
.Select(c => c.Value)
.SingleOrDefault();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment