How to add claims to access token get from IdentityServer3 using resource owner flow with javascript client

后端 未结 3 1798
Happy的楠姐
Happy的楠姐 2021-02-04 14:52

I use the resource owner flow with IdentityServer3 and send get token request to identity server token endpoint with username and password in javascript as below:



        
3条回答
  •  心在旅途
    2021-02-04 15:56

    Not only that I tried other methods, I tried all possible combinations of scopes etc. All I could read in the access token was "scope", "scope name", for Resource Flow there were no claims I have added period.

    I had to do all this

    1. Add custom UserServiceBase and override AuthenticateLocalAsync since I have username/password there and I need both to fetch things from the database
    2. Add claims that I need in the same function (this on itself will not add claim to Access Token, however you will able to read them in various ClaimsPrincipal parameters around)
    3. Add custom DefaultClaimsProvider and override GetAccessTokenClaimsAsync where ClaimsPrincipal subject contains the claims I previously set, I just take them out and put again into ølist of claims for the result.

    I guess this last step might be done overriding GetProfileDataAsync in the custom UserServiceBase, but the above just worked so I did not want to bother.

    The general problem is not how to set claims, it is where you populate them. You have to override something somewhere.

    This here worked for me since I needed data from a database, someone else should populate claims elsewhere. But they are not going to magically appear just because you nicely set Scopes and Claims Identity Server configurations.

    Most of the answers say not a word about where to set the claim values properly. In each particular override you have done, the passed parameters, when they have claims, in the function are attached to identity or access token.

    Just take care of that and all will be fine.

提交回复
热议问题