//connect/userinfo
        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            try
            {
                if (!string.IsNullOrEmpty(context.Subject.Identity.Name))
                {
                    var user = await userService.GetOneAsync(context.Subject.Identity.Name);

                    if (user != null)
                    {
                        var claims = ResourceOwnerPasswordValidator.GetUserClaims(user);

                        context.IssuedClaims = claims.Where(x => context.RequestedClaimTypes.Contains(x.Type)).ToList();
                    }
                }
                else
                {
                    var userId = context.Subject.Claims.FirstOrDefault(x => x.Type == "sub");

                    if (!string.IsNullOrEmpty(userId?.Value) && long.Parse(userId.Value) > 0)
                    {
                        var user = await userService.GetOneAsync(int.Parse(userId.Value));

                        if (user != null)
                        {
                            context.IssuedClaims = ResourceOwnerPasswordValidator.GetUserClaims(user).ToList();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("User profile not found");
            }
        }
 public async Task GetProfileDataAsync(ProfileDataRequestContext context)
 {
     await Task.Run(() =>
     {
         var id               = context.Subject.Claims.FirstOrDefault().Value;
         int userId           = Int32.Parse(id);
         User user            = Config.Users.FirstOrDefault(x => x.UserId == userId);
         context.IssuedClaims = ResourceOwnerPasswordValidator.GetUserClaims(user);
     });
 }
示例#3
0
        //Get user profile date in terms of claims when calling
        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var validator = new ResourceOwnerPasswordValidator(_userRepository);

            try
            {
                //depending on the scope accessing the user data.
                if (!string.IsNullOrEmpty(context.Subject.Identity.Name))
                {
                    //get user from db
                    var user = await _userRepository.GetUserByUsername(context.Subject.Identity.Name);

                    if (user != null)
                    {
                        var claims = ResourceOwnerPasswordValidator.GetUserClaims(user);

                        //set issued claims to return
                        context.IssuedClaims = claims.Where(x => context.RequestedClaimTypes.Contains(x.Type)).ToList();
                    }
                }
                else
                {
                    //get subject from context (this was set ResourceOwnerPasswordValidator.ValidateAsync),
                    //where and subject was set to my user id.
                    var userId = context.Subject.Claims.FirstOrDefault(x => x.Type == "sub");

                    if (!string.IsNullOrEmpty(userId?.Value) && long.Parse(userId.Value) > 0)
                    {
                        //get user from db (find user by user id)
                        var user = await _userRepository.GetUserByID(int.Parse(userId.Value));

                        // issue the claims for the user
                        if (user != null)
                        {
                            var claims = ResourceOwnerPasswordValidator.GetUserClaims(user);

                            context.IssuedClaims = claims.Where(x => context.RequestedClaimTypes.Contains(x.Type)).ToList();
                            context.IssuedClaims.AddRange(claims);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
示例#4
0
        public async Task GetProfileDataAsync(ProfileDataRequestContext profileContext)
        {
            try
            {
                if (!string.IsNullOrEmpty(profileContext.Subject.Identity.Name))
                {
                    var customer = await _context.Customers
                                   .SingleOrDefaultAsync(m => m.Email == profileContext.Subject.Identity.Name);

                    if (customer != null)
                    {
                        var claims = ResourceOwnerPasswordValidator.GetUserClaims(customer);

                        profileContext.IssuedClaims = claims.Where(
                            x => profileContext.RequestedClaimTypes.Contains(x.Type)).ToList();
                    }
                }
                else
                {
                    var customerId = profileContext.Subject.Claims.FirstOrDefault(x => x.Type == "sub");

                    if (!string.IsNullOrEmpty(customerId.Value))
                    {
                        var customer = await _context.Customers
                                       .SingleOrDefaultAsync(u => u.Id == Guid.Parse(customerId.Value));

                        if (customer != null)
                        {
                            var claims = ResourceOwnerPasswordValidator.GetUserClaims(customer);

                            profileContext.IssuedClaims = claims.Where(x => profileContext.RequestedClaimTypes.Contains(x.Type)).ToList();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }