Seeking Confirmation that IdentityServer 3 does not support Custom Claims without 2nd call to server

时间秒杀一切 提交于 2019-12-03 07:32:17

Sorry to be the one to answer my own question, but I found out how to make this work.

  1. Create a custom scope. Make sure that the Type = ScopeType.Resource, and that IncludeAllClaimsForUser = false, and add a collection of Claims to the scope, and set the second parameter of the ScopeClaim to true.
  2. Add the custom scope to your client.
  3. In your IUserService override, in AuthenticateLocalAsync, make sure to pass the user.Claims as the 3rd parameter in the call to AuthenticateResult.
  4. In your IUserService override, in GetProfileDataAsync add the following code:

    if (context.RequestedClaimTypes != null)
    {
        List<Claim> newclaims = new List<Claim>();
        foreach (Claim claim in context.Subject.Claims)
        {
            if (context.RequestedClaimTypes.Contains(claim.Type))
            {
                newclaims.Add(claim);
            }
        }
        context.IssuedClaims = newclaims;
    }
    return Task.FromResult(context.IssuedClaims);
    
  5. Finally, to make sure that GetProfileDataAsync fires every time the user logs in, not just the first time, make sure that you do not have caching turned on. This probably means removing a line of code in your startup that looks like this: factory.ConfigureUserServiceCache().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!