Using Microsoft.AspNetCore.Identity, you might miss a method like GetUserId() which was available for previous .NET frameworks by default.
As a workaround, use this extension method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System.Security.Claims; namespace myproject.Common { public static class ExtensionMethods { /// <summary> /// User ID /// </summary> /// <param name="user"></param> /// <returns></returns> public static string getUserId(this ClaimsPrincipal user) { if (!user.Identity.IsAuthenticated) return null; ClaimsPrincipal currentUser = user; return currentUser.FindFirst(ClaimTypes.NameIdentifier).Value; } } } |
Usage:
1 2 3 4 5 |
using myproject.Common; ... var userId = User.getUserId(); |
thank you, this documentation was very helpful.
Danke für das Löscen des Problems.
thx a lot.