コード例 #1
0
        public IdentityContext HydrateIdentity(string userId)
        {
            if (userId.IsNullOrEmpty())
            {
                return(new IdentityContext());
            }

            // If the User's instance is not enabled, vacate
            //
            var instances = _masterRepository.RetrievesInstanceByUserId(userId);

            if (instances.Count > 1)
            {
                // *** Pending multi-domain feature release
                //
                throw new NotImplementedException("Multiple Instances feature not available yet");
            }

            var instance = instances.First();

            if (!instance.IsEnabled)
            {
                SignOut();
                return(new IdentityContext());
            }

            // Successfully located User
            //
            var user        = _dbContext.Users.FirstOrDefault(x => x.Id == userId);
            var userRoleIds = user.Roles.Select(x => x.RoleId);
            var userRoles   =
                _roleManager.Roles
                .Where(x => userRoleIds.Contains(x.Id))
                .Select(x => x.Name)
                .ToList();

            var context = new IdentityContext();

            context.AspNetUserId = user.Id;
            context.Email        = user.Email;
            context.AspNetRoles  = userRoles;

            // Located valid Instance - load data into properties
            //
            context.InstanceId       = instance.Id;
            context.InstanceNickName = instance.OwnerNickname;
            context.InstanceDomain   = instance.OwnerDomain;
            context.InstanceHref     = $"https://{instance.OwnerDomain}/admin";

            // Auto-initialize persistence layer
            //
            _instanceContext.InitializePersistOnly(instance.Id);

            var state = _stateRepository.RetrieveSystemStateNoTracking();

            context.SystemState = state;
            return(context);
        }