Skip to content

Instantly share code, notes, and snippets.

@owlyowl
Created November 26, 2019 21:00
Show Gist options
  • Save owlyowl/24a25ab22909c12652a00feeba8160a2 to your computer and use it in GitHub Desktop.
Save owlyowl/24a25ab22909c12652a00feeba8160a2 to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Http;
using PregnancyStudy.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
namespace Study.Web.Services
{
public class GetCurrentApplicationUserName : IGetCurrentApplicationUserName
{
readonly IHttpContextAccessor contextAccessor;
public GetCurrentApplicationUserName(IHttpContextAccessor contextAccessor)
{
this.contextAccessor = contextAccessor;
}
string IGetCurrentApplicationUserName.GetCurrentApplicationUserName()
{
var email = contextAccessor.HttpContext?.User?.Claims?.FirstOrDefault(c => c.Type == "sub")?.Value;
var userId = contextAccessor.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier).Value;
return contextAccessor.HttpContext?.User?.Identity?.Name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment