C
C#4mo ago
Palfi

Problem with userclaim

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