C
C#6mo ago
Gipper

ASP.NET Identity need help getting to respective logged in user's row in AspNetUsers table

Hello all, What the title says, I need to know how to get to that row. Here's what I've tried already from the internet:
string aspNetUserId = System.Security.Principal.WindowsIdentity.GetCurrent().GetUserId(); //returns nothing (null)
string aspNetUserId = System.Security.Principal.WindowsIdentity.GetCurrent().GetUserId(); //returns nothing (null)
var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); //doesn't know what OwinContext is and idk how to get to it + it doesn't know how to get to ApplicationUserManager and I also don't know where (if it even exists) that class is
var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); //doesn't know what OwinContext is and idk how to get to it + it doesn't know how to get to ApplicationUserManager and I also don't know where (if it even exists) that class is
Help?
4 Replies
Angius
Angius6mo ago
Try going from ClaimsPrincipal? It will be injected by default into controllers and pages as User property So User.FindFirstValue(ClaimTypes.NameIdentifier) would give you the user ID In general, that .FindFirstValue() is how you would get any claim I have a number of useful extension methods to simplify it, for example
public static long? GetNumericId(this ClaimsPrincipal principal)
{
var user = principal.FindFirstValue(ClaimTypes.NameIdentifier);
var castResult = long.TryParse(user, out var userId);
return castResult ? userId : null;
}

public static string? GetUsername(this ClaimsPrincipal principal)
=> principal.FindFirstValue(ClaimTypes.Name);
public static long? GetNumericId(this ClaimsPrincipal principal)
{
var user = principal.FindFirstValue(ClaimTypes.NameIdentifier);
var castResult = long.TryParse(user, out var userId);
return castResult ? userId : null;
}

public static string? GetUsername(this ClaimsPrincipal principal)
=> principal.FindFirstValue(ClaimTypes.Name);
Gipper
Gipper6mo ago
GetNumericId is the same it just casts into a long value, right?
Angius
Angius6mo ago
I mean, you can see what it does Identity stores the IDs as strings, by default And since I set it to use longs for sequential IDs, I need to parse that If your IDs are strings, keep it as-is If they're GUIDs, you might need to parse the strings to a GUID instead of a long Etc
Gipper
Gipper6mo ago
thanks, it works ❤️ yeah, I just don't understand very well what claims are or how they work
Want results from more Discord servers?
Add your server
More Posts
STJ: Prevent or reverse escaping of base64 string during deserializationI am using STJ to decode a structure with a string member; the string contains an image in base64 foI would cherish if someone would share an idea on how to solve the Simple Algebra Problem.You are given a mathematical function f(x) and a value of a. Your task is to determine if the equatiproblem with entityframework migrationerror: An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing withoC# RoadmapI’ve been struggling for a while now on how to progress with learning C#. I’ve been given a lot of gDoes .NETZ MAUI rightly have the reputation of being half-baked?I'm a Swift guy, but I want to re-check on the Microsoft approach after my break up with Xamarin lik✅ Help me with TestI want to test RequestHandler (im using Mediator), but inside of that handler method I need to get uNeed help with a telephone directory Windows Forms applicationI am getting an exception when I try to click on a row to change the selected row in a DataGridView.How many unity exclusive functions and methods are there?Im talking about stuff like Navmesh agent, game.object and etcHow do I redirect a user back to my app after authentication? (Xamarin)Hi, I'm very new to C# and trying to write an app using Xamarin for my coursework. I'm using the SpoCan someone please help with C# and Dot Net program?Basically, I've been trying to synchronize nzxt rgb in my PC with Razer, and have modded a nuget lib