C
C#9mo ago
Palfi

Problem with userclaim

i dont know how to check my userclaim and i need help
35 Replies
Palfi
PalfiOP9mo 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
PalfiOP9mo ago
This is the error
No description
Palfi
PalfiOP9mo 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
Pobiega9mo 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
PalfiOP9mo ago
how can i find the claim? @Pobiega
Pobiega
Pobiega9mo ago
set a breakpoint on the first line, and inspect the User.Claims verify what claims the user has track your problem down
Palfi
PalfiOP9mo ago
here?
No description
Palfi
PalfiOP9mo ago
or on the create
Pobiega
Pobiega9mo ago
var userId = User.Claims.First(c =>
$debug
MODiX
MODiX9mo 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
PalfiOP9mo ago
this what im looking for?
No description
Palfi
PalfiOP9mo ago
@Pobiega
Pobiega
Pobiega9mo ago
almost. you have the claims just above Identity what particular data you are after is up to you
Palfi
PalfiOP9mo ago
No description
Pobiega
Pobiega9mo ago
but use the debugger to see what is available, then write your code to find the correct values
Palfi
PalfiOP9mo ago
im after the users id
Pobiega
Pobiega9mo ago
check the identity claims
Pobiega
Pobiega9mo ago
No description
Palfi
PalfiOP9mo ago
something like this? @Pobiega
Palfi
PalfiOP9mo ago
No description
Palfi
PalfiOP9mo ago
ah no i see what you are asking to see here
Palfi
PalfiOP9mo ago
No description
Palfi
PalfiOP9mo ago
should it be actor by any chance or something like that
Pobiega
Pobiega9mo ago
No description
Pobiega
Pobiega9mo ago
this one. nameidentifier thats your "user id"
Palfi
PalfiOP9mo ago
so i change UserId for nameidentifier
Pobiega
Pobiega9mo ago
wellllll if you just type "nameidentifier" it wont work you need to use the constant I think its something like ClaimNames
Palfi
PalfiOP9mo ago
isnt it just NameIdentifier?
Pobiega
Pobiega9mo ago
no
Palfi
PalfiOP9mo ago
i found a stackoverflow with something like that
Pobiega
Pobiega9mo ago
its
No description
Pobiega
Pobiega9mo ago
thats the claim name that entire thing
Palfi
PalfiOP9mo 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
Pobiega9mo ago
You probably picked the wrong project type at creation There are modern .net test templates
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server