.net core identity get user id
C#
public static class ClaimsPrincipalExtensions
{
public static T GetLoggedInUserId<T>(this ClaimsPrincipal principal)
{
if (principal == null)
throw new ArgumentNullException(nameof(principal));
var loggedInUserId = principal.FindFirstValue(ClaimTypes.NameIdentifier);
if (typeof(T) == typeof(string))
{
return (T)Convert.ChangeType(loggedInUserId, typeof(T));
}
else if (typeof(T) == typeof(int) || typeof(T) == typeof(long))
{
return loggedInUserId != null ? (T)Convert.ChangeType(loggedInUserId, typeof(T)) : (T)Convert.ChangeType(0, typeof(T));
}
else
{
throw new Exception("Invalid type provided");
}
}
public static string GetLoggedInUserName(this ClaimsPrincipal principal)
{
if (principal == null)
throw new ArgumentNullException(nameof(principal));
return principal.FindFirstValue(ClaimTypes.Name);
}
public static string GetLoggedInUserEmail(this ClaimsPrincipal principal)
{
if (principal == null)
throw new ArgumentNullException(nameof(principal));
return principal.FindFirstValue(ClaimTypes.Email);
}
}
Also in C#:
- Title
- c sharp substring
- Category
- C#
- Title
- parsing object from text file c#
- Category
- C#
- Title
- c# double question mark
- Category
- C#
- Title
- solid principles c#
- Category
- C#
- Title
- how to add to a list c#
- Category
- C#
- Title
- c# ref
- Category
- C#
- Title
- C# string is all zeros
- Category
- C#
- Title
- c# round number down
- Category
- C#
- Title
- c# read all lines from filestream
- Category
- C#
- Title
- C# foreach loop async but wait at end
- Category
- C#
- Title
- snake game
- Category
- C#
- Title
- unity deactivate scripts in list
- Category
- C#
- Title
- c# compile into an exe
- Category
- C#
- Title
- C# how to get public key for InternalsVisibleTo
- Category
- C#
- Title
- unity get all children
- Category
- C#
- Title
- MVC creating a counter for products
- Category
- C#
- Title
- winforms messagebox with button
- Category
- C#
- Title
- unity face direction of movement
- Category
- C#
- Title
- simple reset transform.rotation c#
- Category
- C#
- Title
- unity c# write line
- Category
- C#
- Title
- make an object disappear from a c# script unity
- Category
- C#
- Title
- change canvas color uwp c#
- Category
- C#
- Title
- get waht is differnt between two arrays c#
- Category
- C#
- Title
- add row count devepxress report
- Category
- C#
- Title
- double tryparse dot comma
- Category
- C#
- Title
- replace double backslash with single backslash c#
- Category
- C#
- Title
- how to change player cursor c# script unity
- Category
- C#
- Title
- unity set list of strings
- Category
- C#
- Title
- c# datafield change cell background color
- Category
- C#
- Title
- rotatearound unity
- Category
- C#
- Title
- c# empty IEnumerable
- Category
- C#
- Title
- c# shorten an method
- Category
- C#
- Title
- c# exit for
- Category
- C#
- Title
- c# array inst working
- Category
- C#
- Title
- c# how to print
- Category
- C#
- Title
- c# enum.getvalues
- Category
- C#
- Title
- dictionary update c#
- Category
- C#
- Title
- vector between two points unity
- Category
- C#
- Title
- send type as argument c#
- Category
- C#
- Title
- unity how to get data of play session time in a text file?
- Category
- C#
- Title
- how get data from json in c#
- Category
- C#
- Title
- unity change tag in script
- Category
- C#
- Title
- mysql executeScalar only if successful
- Category
- C#
- Title
- set int to null c#
- Category
- C#
- Title
- c# return two variables of different types
- Category
- C#
- Title
- access dic by key c#
- Category
- C#
- Title
- Celsius to Fahrenheit c#
- Category
- C#
- Title
- F# convert generic.List to list
- Category
- C#
- Title
- c# string code ascii
- Category
- C#
- Title
- check version of asp.net core
- Category
- C#
- Title
- get day month year from date c#
- Category
- C#
- Title
- convert comma separated string to array c#
- Category
- C#
- Title
- c# query string builder
- Category
- C#
- Title
- c# expandoobject indexer
- Category
- C#
- Title
- unity how to make a ui disappear
- Category
- C#
- Title
- initialize enum with another enum c#
- Category
- C#
- Title
- no overload for 'useItemOnSceneLoad' matches delegate 'UnityAction<Scene, LoadSceneMode>'
- Category
- C#
- Title
- snake
- Category
- C#
- Title
- c# how to sort a list
- Category
- C#
- Title
- gfortran: declare an array
- Category
- C#
- Title
- write string multiple times c#
- Category
- C#
- Title
- unity rb.addexplosionforce 2d
- Category
- C#
- Title
- c# substring reverse
- Category
- C#
- Title
- value is null to insert in c#
- Category
- C#
- Title
- attribute usage c#
- Category
- C#
- Title
- c# convert to nullable datetime
- Category
- C#
- Title
- How to get number of months between 2 dates c#
- Category
- C#
- Title
- check for collision unity c#
- Category
- C#
- Title
- exit game unity
- Category
- C#
- Title
- javascript append
- Category
- C#
- Title
- c# reverse array
- Category
- C#
- Title
- godot c# move towards
- Category
- C#
- Title
- c# how to exit program
- Category
- C#
- Title
- .net core check if linux
- Category
- C#
- Title
- unity list
- Category
- C#
- Title
- Instantiate c#
- Category
- C#
- Title
- c# creating and throwing exceptions
- Category
- C#
- Title
- make a list c#
- Category
- C#
- Title
- c# remove time in datetime
- Category
- C#
- Title
- initialise icollection c#
- Category
- C#
- Title
- Could not load file or assembly 'Ubiety.Dns.Core, Version=2.2.1.0
- Category
- C#
- Title
- custom player spawner mirror
- Category
- C#
- Title
- unity enable gameobject
- Category
- C#
- Title
- find negative version of integer in c#
- Category
- C#
- Title
- south african id number validation c#
- Category
- C#
- Title
- unity find object by name
- Category
- C#
- Title
- how to find the type of a object c#
- Category
- C#
- Title
- disable mouse unity
- Category
- C#
- Title
- unity randomly generated numbers c#
- Category
- C#
- Title
- ubuntu: how to open the terminal from c#
- Category
- C#
- Title
- unity cancel invokerepeating
- Category
- C#
- Title
- declare string array c# without size
- Category
- C#
- Title
- convert string to double c#
- Category
- C#
- Title
- how to goto a website using linklabel c#
- Category
- C#
- Title
- c# catch two exceptions in one block
- Category
- C#
- Title
- c# double value with 2 decimal places
- Category
- C#
- Title
- c# select first value from list
- Category
- C#
- Title
- asp.net core mvc not triggering client side validation
- Category
- C#
- Title
- c# bitmap to picturebox
- Category
- C#
- Title
- hash sign c sharp
- Category
- C#