/// <summary>
        /// 只要有关用户的身份信息单元被请求(例如在令牌创建期间或通过用户信息终点),就会调用此方法
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public virtual Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            context.LogProfileRequest(Logger);

            //判断是否有请求Claim信息
            if (context.RequestedClaimTypes.Any())
            {
                var userClaims = new List <Claim>
                {
                    new Claim("role", "测试1"),
                    new Claim("name", "测试2"),
                };
                List <TestUser> userList = new List <TestUser>()
                {
                    new TestUser()
                    {
                        SubjectId = "cfac01a9-ba15-4678-bccb-cc22d7896362", Password = "******", Username = "******", Claims = userClaims
                    },
                    new TestUser()
                    {
                        SubjectId = "cfac01a9-ba15-4678-bccb-cc22d7855555", Password = "******", Username = "******"
                    },
                };
                TestUserStore userStore = new TestUserStore(userList);
                //根据用户唯一标识查找用户信息
                var user = userStore.FindBySubjectId(context.Subject.GetSubjectId());
                if (user != null)
                {
                    //调用此方法以后内部会进行过滤,只将用户请求的Claim加入到 context.IssuedClaims 集合中 这样我们的请求方便能正常获取到所需Claim
                    context.AddRequestedClaims(user.Claims);
                }
                //context.IssuedClaims=userClaims;
            }
            context.LogIssuedClaims(Logger);

            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        FindUserFromExternalProviderAsync(AuthenticateResult result)
        {
            var externalUser = result.Principal;

            // try to determine the unique id of the external user (issued by the provider)
            // the most common claim type for that are the sub claim and the NameIdentifier
            // depending on the external provider, some other claim type might be used
            var userIdClaim = externalUser.FindFirst(JwtClaimTypes.Subject) ??
                              externalUser.FindFirst(ClaimTypes.NameIdentifier) ??
                              throw new Exception("Unknown userid");

            // remove the user id claim so we don't include it as an extra claim if/when we provision the user
            var claims = externalUser.Claims.ToList();

            claims.Remove(userIdClaim);

            var provider       = result.Properties.Items["scheme"];
            var providerUserId = userIdClaim.Value;

            // find external user
            var user = _users.FindBySubjectId(providerUserId);

            return(user, provider, providerUserId, claims);
        }
Ejemplo n.º 3
0
 public IActionResult GetUser([FromRoute]string subjectId)
 {
     var user = _usersStore.FindBySubjectId(subjectId);
     Logger.LogDebug("User found: {subjectId}", subjectId);
     return Json(user);
 }