Exemplo n.º 1
0
        public virtual async Task <LoginOutput> LoginAsync(LoginInput input)
        {
            var loginResult = await GetLoginResultAsync(input);

            using var tenantChange = CurrentTenant.Change(loginResult.MiniProgram.TenantId);

            await _identityOptions.SetAsync();

            using (var uow = UnitOfWorkManager.Begin(new AbpUnitOfWorkOptions(true), true))
            {
                var identityUser =
                    await _identityUserManager.FindByLoginAsync(loginResult.LoginProvider, loginResult.ProviderKey) ??
                    await _miniProgramLoginNewUserCreator.CreateAsync(input.UserInfo, loginResult.LoginProvider,
                                                                      loginResult.ProviderKey);

                await UpdateMiniProgramUserAsync(identityUser, loginResult.MiniProgram, loginResult.UnionId,
                                                 loginResult.Code2SessionResponse.OpenId, loginResult.Code2SessionResponse.SessionKey);

                await UpdateUserInfoAsync(identityUser, input.UserInfo);

                await uow.CompleteAsync();
            }

            return(new LoginOutput
            {
                TenantId = loginResult.MiniProgram.TenantId,
                RawData = (await RequestIds4LoginAsync(input.AppId, loginResult.UnionId,
                                                       loginResult.Code2SessionResponse.OpenId))?.Raw
            });
        }
Exemplo n.º 2
0
        public virtual async Task <string> LoginAsync(LoginInput input)
        {
            var miniProgram = await _miniProgramRepository.GetAsync(x => x.AppId == input.AppId);

            var loginResult = await GetLoginResultAsync(miniProgram, input);

            await _identityOptions.SetAsync();

            var identityUser =
                await _identityUserManager.FindByLoginAsync(loginResult.LoginProvider, loginResult.ProviderKey) ??
                await _miniProgramLoginNewUserCreator.CreateAsync(input.UserInfo, loginResult.LoginProvider,
                                                                  loginResult.ProviderKey);

            await UpdateMiniProgramUserAsync(identityUser, miniProgram, loginResult.Code2SessionResponse.UnionId,
                                             loginResult.Code2SessionResponse.OpenId, loginResult.Code2SessionResponse.SessionKey);

            await UpdateUserInfoAsync(identityUser, input.UserInfo);

            return((await RequestIds4LoginAsync(input.AppId, loginResult.Code2SessionResponse.UnionId,
                                                loginResult.Code2SessionResponse.OpenId))?.Raw);
        }
        public virtual async Task <string> LoginAsync(LoginInput input)
        {
            var miniProgram = await _miniProgramRepository.GetAsync(x => x.AppId == input.AppId);

            var code2SessionResponse =
                await _loginService.Code2SessionAsync(miniProgram.AppId, miniProgram.AppSecret, input.Code);

            _signatureChecker.Check(input.RawData, code2SessionResponse.SessionKey, input.Signature);

            var openId  = code2SessionResponse.OpenId;
            var unionId = code2SessionResponse.UnionId;

            if (input.LookupUseRecentlyTenant)
            {
                Guid?tenantId;

                using (_dataFilter.Disable <IMultiTenant>())
                {
                    tenantId = await _miniProgramUserRepository.FindRecentlyTenantIdAsync(miniProgram.Id, openId);
                }

                using var tenantChange = CurrentTenant.Change(tenantId);
            }

            string loginProvider;
            string providerKey;

            // 如果 auth.code2Session 没有返回用户的 UnionId
            if (unionId.IsNullOrWhiteSpace())
            {
                if (!input.EncryptedData.IsNullOrWhiteSpace() && !input.Iv.IsNullOrWhiteSpace())
                {
                    // 方法1:通过 EncryptedData 和 Iv 解密获得用户的 UnionId
                    var decryptedData =
                        _jsonSerializer.Deserialize <Dictionary <string, object> >(
                            AesHelper.AesDecrypt(input.EncryptedData, input.Iv, code2SessionResponse.SessionKey));

                    unionId = decryptedData.GetOrDefault("unionId") as string;
                }
                else
                {
                    // 方法2:尝试通过 OpenId 在 MiniProgramUser 实体中查找用户的 UnionId
                    // Todo: should use IMiniProgramUserStore
                    unionId = await _miniProgramUserRepository.FindUnionIdByOpenIdAsync(miniProgram.Id, openId);
                }
            }

            if (unionId.IsNullOrWhiteSpace())
            {
                loginProvider = await _miniProgramLoginProviderProvider.GetAppLoginProviderAsync(miniProgram);

                providerKey = openId;
            }
            else
            {
                loginProvider = await _miniProgramLoginProviderProvider.GetOpenLoginProviderAsync(miniProgram);

                providerKey = unionId;
            }

            var identityUser = await _identityUserManager.FindByLoginAsync(loginProvider, providerKey) ??
                               await _miniProgramLoginNewUserCreator.CreateAsync(input.UserInfo, loginProvider, providerKey);

            await UpdateMiniProgramUserAsync(identityUser, miniProgram, unionId, openId, code2SessionResponse.SessionKey);
            await UpdateUserInfoAsync(identityUser, input.UserInfo);

            return((await RequestIds4LoginAsync(input.AppId, unionId, openId))?.Raw);
        }