c# - how to add claims in jwt using jose-jwt -
i using jose jwt library creating jwt token, not sure how can use claims tag in payload. want store user name , other data related it. below code using generate code
byte[] secretkey = base64urldecode("-----begin private key-----"); datetime issued = datetime.now; datetime expire = datetime.now.addhours(10); var payload = new dictionary<string, object>() { {"iss", "service email"}, {"aud", "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.identitytoolkit"}, {"sub", "service email"}, {"iat", tounixtime(issued).tostring()}, {"exp", tounixtime(expire).tostring()} }; string token = jwt.encode(payload, secretkey, jwsalgorithm.hs256); return token;
the jwt specification talks 3 types of claims: registered, public , private.
registered
the usual ones such iss
, sub
, exp
, etc.
public claims
the iana jwt claims registry used specify claims should used publicly standardize them between services. these contains lots of useful ones such name
, email
, address
, etc.
private claims
if using token within own application or between known applications add whatever claims want.
it might idea avoid using claims iana jwt claims registry other purposes though (ie don't use name
store users username).
so in case code add username (with claim iana registry)
byte[] secretkey = base64urldecode("-----begin private key-----"); datetime issued = datetime.now; datetime expire = datetime.now.addhours(10); var payload = new dictionary<string, object>() { {"iss", "service email"}, {"aud", "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.identitytoolkit"}, {"sub", "service email"}, {"iat", tounixtime(issued).tostring()}, {"exp", tounixtime(expire).tostring()}, {"preferred_username", "myawesomeusername"} }; string token = jwt.encode(payload, secretkey, jwsalgorithm.hs256); return token;
though if it's internal use go username
or usr
myself.
another thing remember (and many wrong) jwt isn't encrypting anything. content base64 encoded hold of token can read in it. make sure not put sensitive in them if there slight chance user can see them.
Comments
Post a Comment