asp.net web api2 - Get user data from bearer token web api -


i implementing .net web api 2 in c# using. works fine, send username , password , bearer token returned.

now token used other api calls, in each call know user data. because app call web api such python code etc, in such cases send bearer token not user name.

i tried dig through code not locate it.

any ideas appreciated here.

the answer depends on type of bearer token you're dealing with. example, if jwt token, consists of 3 parts separated period: header, payload, , signature. user id payload, you'll need base-64 decode payload , grab "sub" property. 'sub' known 'claim'. token contains many claims 'sub' typically stores user id. see more details: https://jwt.io/introduction/

in c#, can parse out payload of token using string.split() , base 64 decode payload this:

var payload = encoding.utf8.getstring(textencodings.base64url.decode(b64payload)); 

from there, need deserialize payload object matches format expect:

var tokenpayloadobj = jsonconvert.deserializeobject<yourjwtobjecthere>(payload); 

finally, user id should property of jwt token object:

var userid = tokenpayloadobj.userid; 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -