C
C#10mo ago
Palfi

Problem with userclaim

i dont know how to check my userclaim and i need help
35 Replies
Palfi
PalfiOP10mo ago
public async Task<IActionResult> Create([Bind("PetId,PetName,PetAge")] Pet pet)
{
var userId = User.Claims.First(c => c.Type == "UserID").Value;
if (ModelState.IsValid)
{
var owner = await _context.Owner.FirstOrDefaultAsync(o => o.IdentityUserId == userId);
if (owner == null)
{
return NotFound("Owner not found");
}
pet.OwnerId = owner.OwnerId;
_context.Add(pet);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(pet);
}
public async Task<IActionResult> Create([Bind("PetId,PetName,PetAge")] Pet pet)
{
var userId = User.Claims.First(c => c.Type == "UserID").Value;
if (ModelState.IsValid)
{
var owner = await _context.Owner.FirstOrDefaultAsync(o => o.IdentityUserId == userId);
if (owner == null)
{
return NotFound("Owner not found");
}
pet.OwnerId = owner.OwnerId;
_context.Add(pet);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(pet);
}
i get an error in the userId line
Palfi
PalfiOP10mo ago
This is the error
No description
Palfi
PalfiOP10mo ago
Im using asp.net identity authentication
public async Task<IActionResult> Index()
{
var userId = User.Claims.First(c => c.Type == "UserID").Value; // Assuming "UserID" claim holds the identity user ID
return View(await _context.Pet.Where(p => p.Owner.IdentityUserId == userId).ToListAsync());
}
public async Task<IActionResult> Index()
{
var userId = User.Claims.First(c => c.Type == "UserID").Value; // Assuming "UserID" claim holds the identity user ID
return View(await _context.Pet.Where(p => p.Owner.IdentityUserId == userId).ToListAsync());
}
also have this if needed
Pobiega
Pobiega10mo ago
it jsut means there were no claims that matched. I suggest you place some breakpoints and debug, check if the claim has a different name or that its present at all
Palfi
PalfiOP10mo ago
how can i find the claim? @Pobiega
Pobiega
Pobiega10mo ago
set a breakpoint on the first line, and inspect the User.Claims verify what claims the user has track your problem down
Palfi
PalfiOP10mo ago
here?
No description
Palfi
PalfiOP10mo ago
or on the create
Pobiega
Pobiega10mo ago
var userId = User.Claims.First(c =>
$debug
MODiX
MODiX10mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Palfi
PalfiOP10mo ago
this what im looking for?
No description
Palfi
PalfiOP10mo ago
@Pobiega
Pobiega
Pobiega10mo ago
almost. you have the claims just above Identity what particular data you are after is up to you
Palfi
PalfiOP10mo ago
No description
Pobiega
Pobiega10mo ago
but use the debugger to see what is available, then write your code to find the correct values
Palfi
PalfiOP10mo ago
im after the users id
Pobiega
Pobiega10mo ago
check the identity claims
Pobiega
Pobiega10mo ago
No description
Palfi
PalfiOP10mo ago
something like this? @Pobiega
Palfi
PalfiOP10mo ago
No description
Palfi
PalfiOP10mo ago
ah no i see what you are asking to see here
Palfi
PalfiOP10mo ago
No description
Palfi
PalfiOP10mo ago
should it be actor by any chance or something like that
Pobiega
Pobiega10mo ago
No description
Pobiega
Pobiega10mo ago
this one. nameidentifier thats your "user id"
Palfi
PalfiOP10mo ago
so i change UserId for nameidentifier
Pobiega
Pobiega10mo ago
wellllll if you just type "nameidentifier" it wont work you need to use the constant I think its something like ClaimNames
Palfi
PalfiOP10mo ago
isnt it just NameIdentifier?
Pobiega
Pobiega10mo ago
no
Palfi
PalfiOP10mo ago
i found a stackoverflow with something like that
Pobiega
Pobiega10mo ago
its
No description
Pobiega
Pobiega10mo ago
thats the claim name that entire thing
Palfi
PalfiOP10mo ago
ok well il just copy that Also while we are at it Im having problem with my unit tests It only accepted me to make them in version 4.8 and my solution is in 8.0 and now its giving me errors
Pobiega
Pobiega10mo ago
You probably picked the wrong project type at creation There are modern .net test templates
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server