C
C#•2w ago
Yarden

Can someone explain me Claims? I don't manage to understand it well. Especially for my scenario.

Is Claim a data structure made for integrating with Databases? In my scenario I'm creating a cookie to send to the frontend. Right now, I send the user ID and UserName from the database? Those lines: new(ClaimTypes.NameIdentifier, validUser.Id.ToString()), new(ClaimTypes.Name, validUser.UserName), Now, if I want also to send the user Role from my database, I'm trying to do: new(ClaimTypes.Role, validUser.Role) But here I get an error: "Compiler Error CS1503 Argument 'argument' cannot convert from TypeA to TypeB" But for some reason the line " new(ClaimTypes.Role, "Business")," complies. So I don't understand what exactly the form of Claim and what exactly the purpose of it. Should I change those line: new(ClaimTypes.NameIdentifier, validUser.Id.ToString()), new(ClaimTypes.Name, validUser.UserName), To anything else, like the role? something like this: new(ClaimTypes.NameIdentifier, "ID"), new(ClaimTypes..Name, "UserName"), I'm really confused about this subject. And the code inside the file someone from discord gave me, so this is the only part inside my project I'm yet to understand Thank you!
7 Replies
Angius
Angius•2w ago
Claims are strings Is validUser.Role a string? FWIW, I handle custom claims with a custom ClaimsPrincipalFactory: https://github.com/Genfic/Ogma/blob/master/Ogma3/Data/OgmaClaimsPrincipalFactory.cs
Yarden
YardenOP•2w ago
The Role inside my database is a string, yes
Angius
Angius•2w ago
Argument 'argument' cannot convert from TypeA to TypeB" What exactly are the types here?
Yarden
YardenOP•2w ago
aa there is obviously something I don't understand. Enums.Role is an enum so yeah it is not a string. The Role inside my database is one of the values of the enum which is a string. I'll try to ask it differently. Here for example: new(ClaimTypes.Name, validUser.UserName) What this line does? It intiailizses "ClaimTypes.Name" with the value of "validUser.UserName"?
No description
Angius
Angius•2w ago
What it is in the database doesn't matter Yes, enums are sometimes serialized as strings for storage in the db So you will need to .ToString() it here So it's a value of type string
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Yarden
YardenOP•2w ago
I think I understand. It's a lot clearer now!! Thank you very much much much, I love you 😂 I will try what I understood and update if anything goes wrong, but I think I got it

Did you find this page helpful?