コード例 #1
0
        public override async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var list = context.RequestedClaimTypes.ToList();

            list.Add(TokenIdKey);
            list.Add(NormalizedName);
            list.Add(ProfilePhoto);
            context.RequestedClaimTypes = list;

            if (context.Caller == IdentityServerConstants.ProfileDataCallers.UserInfoEndpoint)
            {
                context.AddRequestedClaims(context.Subject.Claims.Where(c => c.Type == TokenIdKey));

                var userId = context.Subject?.GetSubjectId();
                var user   = await base.UserManager.FindByIdAsync(userId);

                var currentName   = user.NormalizedName;
                var normNameClaim = new Claim(NormalizedName, currentName);

                var photoUrl   = _profilePhotoUrl.ConstructOrDefaultForUser(user);
                var pictureUrl = new Claim(ProfilePhoto, photoUrl);
                context.AddRequestedClaims(new [] { normNameClaim, pictureUrl });

                var roles = await _userManager.GetRolesAsync(user);

                foreach (var role in roles)
                {
                    context.IssuedClaims.Add(new Claim(JwtClaimTypes.Role, role));
                }

                await base.GetProfileDataAsync(context);
            }
            else
            {
                await base.GetProfileDataAsync(context);

                var userId  = context.Subject?.GetSubjectId();
                var tokenId = _loginManager.GetUserLoginId(userId);

                var user = await base.UserManager.FindByIdAsync(userId);

                var roles = await _userManager.GetRolesAsync(user);

                foreach (var role in roles)
                {
                    context.IssuedClaims.Add(new Claim(JwtClaimTypes.Role, role));
                }

                context.IssuedClaims.Add(new Claim(TokenIdKey, tokenId.ToString(), "tokenId"));
            }
        }