Search code examples
oauth-2.0azure-ad-b2caccess-token

How can i get username in oauth 2 token?


Im trying to get user properties from oauth2 token, using login microsoft request, but i just get the email in token claims. Is there a way to get the username in token claims?

This is the complete request: Postman request


Solution

  • In my case, I created one custom attribute named username in my B2C tenant like this:

    enter image description here

    In your Sign Up and Sign In flow, make sure to include username attribute before running it:

    enter image description here

    When new user is signing up, it will ask the user to enter the username again like this:

    enter image description here

    Before generating token using ROPC flow, make sure to include username in Application claims like this:

    enter image description here

    Now, I generated access token via Postman with below parameters:

    POST https://<b2ctenant_name>.b2clogin.com/b2ctenant.onmicrosoft.com/B2C_1_ROPC_Auth/oauth2/v2.0/token
    grant_type:password
    client_id: appId
    scope:openid <appId> offline_access
    username: <username>
    password:xxxxxxx
    

    Response:

    enter image description here

    When I decoded this token in jwt.ms website, I got username in token claims successfully like this:

    enter image description here

    Reference:

    How to get the signin-name(username) as a claims after login - Microsoft Q&A by AmanpreetSingh-MSFT