Пример #1
0
        protected virtual async Task BuildStoreProductViewsAsync(Guid storeId)
        {
            var products = await _productRepository.GetListAsync(x => x.StoreId == storeId, true);

            using var uow = UnitOfWorkManager.Begin(true, true);

            await _repository.DeleteAsync(x => x.StoreId == storeId, true);

            foreach (var product in products)
            {
                var productView = ObjectMapper.Map <Product, ProductView>(product);

                await FillPriceInfoWithRealPriceAsync(product, productView);

                await _repository.InsertAsync(productView);
            }

            await uow.SaveChangesAsync();

            await uow.CompleteAsync();

            await _cache.SetAsync(await GetCacheKeyAsync(storeId), new ProductViewCacheItem(),
                                  new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(Convert.ToInt32(
                                                                           await SettingProvider.GetOrNullAsync(ProductsSettings.ProductView.CacheDurationSeconds)))
            });
        }
        public virtual async Task SendChangePhoneNumberCodeAsync(SendChangePhoneNumberCodeDto input)
        {
            var securityTokenCacheKey  = SmsSecurityTokenCacheItem.CalculateCacheKey(input.NewPhoneNumber, "SmsChangePhoneNumber");
            var securityTokenCacheItem = await SecurityTokenCache.GetAsync(securityTokenCacheKey);

            var interval = await SettingProvider.GetAsync(Settings.IdentitySettingNames.User.SmsRepetInterval, 1);

            if (securityTokenCacheItem != null)
            {
                throw new UserFriendlyException(L["SendRepeatPhoneVerifyCode", interval]);
            }

            // 是否已有用户使用手机号绑定
            if (await UserRepository.IsPhoneNumberConfirmedAsync(input.NewPhoneNumber))
            {
                throw new BusinessException(IdentityErrorCodes.DuplicatePhoneNumber);
            }
            var user = await UserManager.GetByIdAsync(CurrentUser.GetId());

            var template = await SettingProvider.GetOrNullAsync(Settings.IdentitySettingNames.User.SmsPhoneNumberConfirmed);

            var token = await UserManager.GenerateChangePhoneNumberTokenAsync(user, input.NewPhoneNumber);

            // 发送验证码
            await SecurityCodeSender.SendPhoneConfirmedCodeAsync(input.NewPhoneNumber, token, template);

            securityTokenCacheItem = new SmsSecurityTokenCacheItem(token, user.ConcurrencyStamp);
            await SecurityTokenCache
            .SetAsync(securityTokenCacheKey, securityTokenCacheItem,
                      new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(interval)
            });
        }
Пример #3
0
    public virtual async Task <EmailSettingsDto> GetAsync()
    {
        await CheckFeatureAsync();

        var settingsDto = new EmailSettingsDto
        {
            SmtpHost                  = await SettingProvider.GetOrNullAsync(EmailSettingNames.Smtp.Host),
            SmtpPort                  = Convert.ToInt32(await SettingProvider.GetOrNullAsync(EmailSettingNames.Smtp.Port)),
            SmtpUserName              = await SettingProvider.GetOrNullAsync(EmailSettingNames.Smtp.UserName),
            SmtpPassword              = await SettingProvider.GetOrNullAsync(EmailSettingNames.Smtp.Password),
            SmtpDomain                = await SettingProvider.GetOrNullAsync(EmailSettingNames.Smtp.Domain),
            SmtpEnableSsl             = Convert.ToBoolean(await SettingProvider.GetOrNullAsync(EmailSettingNames.Smtp.EnableSsl)),
            SmtpUseDefaultCredentials = Convert.ToBoolean(await SettingProvider.GetOrNullAsync(EmailSettingNames.Smtp.UseDefaultCredentials)),
            DefaultFromAddress        = await SettingProvider.GetOrNullAsync(EmailSettingNames.DefaultFromAddress),
            DefaultFromDisplayName    = await SettingProvider.GetOrNullAsync(EmailSettingNames.DefaultFromDisplayName),
        };

        if (CurrentTenant.IsAvailable)
        {
            settingsDto.SmtpHost = await SettingManager.GetOrNullForTenantAsync(EmailSettingNames.Smtp.Host, CurrentTenant.GetId(), false);

            settingsDto.SmtpUserName = await SettingManager.GetOrNullForTenantAsync(EmailSettingNames.Smtp.UserName, CurrentTenant.GetId(), false);

            settingsDto.SmtpPassword = await SettingManager.GetOrNullForTenantAsync(EmailSettingNames.Smtp.Password, CurrentTenant.GetId(), false);

            settingsDto.SmtpDomain = await SettingManager.GetOrNullForTenantAsync(EmailSettingNames.Smtp.Domain, CurrentTenant.GetId(), false);
        }

        return(settingsDto);
    }
Пример #4
0
        public virtual async Task OnGetAsync(Guid storeId, Guid?categoryId)
        {
            ProductGroups =
                (await _service.GetProductGroupListAsync()).Items
                .Select(dto => new SelectListItem(dto.DisplayName, dto.Name)).ToList();

            Categories =
                (await _categoryAppService.GetListAsync(new GetCategoryListDto
            {
                MaxResultCount = LimitedResultRequestDto.MaxMaxResultCount
            }))?.Items
                .Select(dto => new SelectListItem(dto.DisplayName, dto.Id.ToString())).ToList();

            Product = new CreateEditProductViewModel
            {
                StoreId       = storeId,
                ProductDetail = new CreateEditProductDetailViewModel
                {
                    StoreId = storeId
                }
            };

            if (categoryId.HasValue)
            {
                Product.CategoryIds = new List <Guid>(new[] { categoryId.Value });
            }

            var paymentExpireInString =
                await SettingProvider.GetOrNullAsync(ProductsConsts.DefaultPaymentExpireInSettingName);

            Product.PaymentExpireIn =
                !paymentExpireInString.IsNullOrEmpty() ? TimeSpan.Parse(paymentExpireInString) : null;
        }
        public virtual async Task SendPhoneRegisterCodeAsync(SendPhoneRegisterCodeDto input)
        {
            await CheckSelfRegistrationAsync();
            await CheckNewUserPhoneNumberNotBeUsedAsync(input.PhoneNumber);

            var securityTokenCacheKey  = SmsSecurityTokenCacheItem.CalculateCacheKey(input.PhoneNumber, "SmsVerifyCode");
            var securityTokenCacheItem = await SecurityTokenCache.GetAsync(securityTokenCacheKey);

            var interval = await SettingProvider.GetAsync(IdentitySettingNames.User.SmsRepetInterval, 1);

            if (securityTokenCacheItem != null)
            {
                throw new UserFriendlyException(L["SendRepeatSmsVerifyCode", interval]);
            }

            var template = await SettingProvider.GetOrNullAsync(IdentitySettingNames.User.SmsNewUserRegister);

            // 安全令牌
            var securityToken = GuidGenerator.Create().ToString("N");

            var code = TotpService.GenerateCode(Encoding.Unicode.GetBytes(securityToken), securityTokenCacheKey);

            securityTokenCacheItem = new SmsSecurityTokenCacheItem(code.ToString(), securityToken);

            await SecurityCodeSender.SendPhoneConfirmedCodeAsync(
                input.PhoneNumber, securityTokenCacheItem.Token, template);

            await SecurityTokenCache
            .SetAsync(securityTokenCacheKey, securityTokenCacheItem,
                      new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(interval)
            });
        }
Пример #6
0
        public virtual async Task <GetPcLoginACodeOutput> GetPcLoginACodeAsync(string miniProgramName)
        {
            var miniProgram = await _miniProgramRepository.GetAsync(x => x.Name == miniProgramName);

            var options = new AbpWeChatMiniProgramOptions
            {
                OpenAppId      = miniProgram.OpenAppIdOrName,
                AppId          = miniProgram.AppId,
                AppSecret      = miniProgram.AppSecret,
                EncodingAesKey = miniProgram.EncodingAesKey,
                Token          = miniProgram.Token
            };

            using (_weChatMiniProgramAsyncLocal.Change(options))
            {
                var token = Guid.NewGuid().ToString("N");

                var handlePage = await SettingProvider.GetOrNullAsync(MiniProgramsSettings.PcLogin.HandlePage);

                var aCodeResponse = await _aCodeService.GetUnlimitedACodeAsync(token, handlePage);

                if (aCodeResponse.ErrorCode != 0)
                {
                    throw new WeChatBusinessException(aCodeResponse.ErrorCode, aCodeResponse.ErrorMessage);
                }

                return(new GetPcLoginACodeOutput
                {
                    Token = token,
                    ACode = aCodeResponse.BinaryData
                });
            }
        }
        public virtual async Task SendPhoneSigninCodeAsync(SendPhoneSigninCodeDto input)
        {
            var securityTokenCacheKey  = SmsSecurityTokenCacheItem.CalculateCacheKey(input.PhoneNumber, "SmsVerifyCode");
            var securityTokenCacheItem = await SecurityTokenCache.GetAsync(securityTokenCacheKey);

            var interval = await SettingProvider.GetAsync(IdentitySettingNames.User.SmsRepetInterval, 1);

            if (securityTokenCacheItem != null)
            {
                throw new UserFriendlyException(L["SendRepeatSmsVerifyCode", interval]);
            }
            // 传递 isConfirmed 验证过的用户才允许通过手机登录
            var user = await GetUserByPhoneNumberAsync(input.PhoneNumber, isConfirmed : true);

            var code = await UserManager.GenerateTwoFactorTokenAsync(user, TokenOptions.DefaultPhoneProvider);

            var template = await SettingProvider.GetOrNullAsync(IdentitySettingNames.User.SmsUserSignin);

            // 发送登录验证码短信
            await SecurityCodeSender.SendPhoneConfirmedCodeAsync(input.PhoneNumber, code, template);

            // 缓存登录验证码状态,防止同一手机号重复发送
            securityTokenCacheItem = new SmsSecurityTokenCacheItem(code, user.SecurityStamp);
            await SecurityTokenCache
            .SetAsync(securityTokenCacheKey, securityTokenCacheItem,
                      new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(interval)
            });
        }
        public async Task HandleEventAsync(CreateEventData eventData)
        {
            var userDefaultCultureName = await SettingProvider.GetOrNullAsync(LocalizationSettingNames.DefaultLanguage);

            if (userDefaultCultureName.IsNullOrWhiteSpace())
            {
                userDefaultCultureName = CultureInfo.CurrentUICulture.Name;
            }
            // 使用系统区域语言发布通知
            using (CultureHelper.Use(userDefaultCultureName, userDefaultCultureName))
            {
                var tenantAdminUserIdentifier = new UserIdentifier(eventData.AdminUserId, eventData.AdminEmailAddress);

                var notificationData = new NotificationData();
                notificationData.WriteStandardData(
                    L("NewTenantRegisteredNotificationTitle"),
                    L("NewTenantRegisteredNotificationMessage", eventData.Name),
                    DateTime.Now, eventData.AdminEmailAddress);

                // 发布租户创建通知
                await NotificationSender
                .SendNofiterAsync(
                    TenantNotificationNames.NewTenantRegistered,
                    notificationData,
                    tenantAdminUserIdentifier,
                    eventData.Id,
                    NotificationSeverity.Success);
            }
        }
Пример #9
0
        protected virtual async Task CheckFile(FileDto input)
        {
            if (input.Bytes.IsNullOrEmpty())
            {
                throw new AbpValidationException("Bytes can not be null or empty!",
                                                 new List <ValidationResult>
                {
                    new ValidationResult("Bytes can not be null or empty!", new[] { "Bytes" })
                });
            }

            var allowedMaxFileSize = await SettingProvider.GetAsync <int>(FileManagementSettings.AllowedMaxFileSize);//kb

            var allowedUploadFormats = (await SettingProvider.GetOrNullAsync(FileManagementSettings.AllowedUploadFormats))
                                       ?.Split(",", StringSplitOptions.RemoveEmptyEntries);

            if (input.Bytes.Length > allowedMaxFileSize * 1024)
            {
                throw new UserFriendlyException(L["FileManagement.ExceedsTheMaximumSize", allowedMaxFileSize]);
            }

            if (allowedUploadFormats == null || !allowedUploadFormats.Contains(Path.GetExtension(input.FileName)))
            {
                throw new UserFriendlyException(L["FileManagement.NotValidFormat"]);
            }
        }
Пример #10
0
        protected virtual async Task BuildStoreProductViewsAsync(Guid storeId)
        {
            var resultDto = await _productAppService.GetListAsync(new GetProductListInput
            {
                StoreId = storeId
            });

            using var uow = UnitOfWorkManager.Begin(true, true);

            await _repository.DeleteAsync(x => x.StoreId == storeId, true);

            foreach (var product in resultDto.Items)
            {
                await _repository.InsertAsync(ObjectMapper.Map <ProductDto, ProductView>(product));
            }

            await uow.CompleteAsync();

            await _cache.SetAsync(await GetCacheKeyAsync(storeId), new ProductViewCacheItem(),
                                  new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(Convert.ToInt32(
                                                                           await SettingProvider.GetOrNullAsync(ProductsSettings.ProductView.CacheDurationSeconds)))
            });
        }
Пример #11
0
        private async Task NormalizeMaxResultCountAsync(PagedAndSortedResultRequestDto input)
        {
            var maxPageSize = (await SettingProvider.GetOrNullAsync(ProductManagementSettings.MaxPageSize))?.To <int>();

            if (maxPageSize.HasValue && input.MaxResultCount > maxPageSize.Value)
            {
                input.MaxResultCount = maxPageSize.Value;
            }
        }
Пример #12
0
        public virtual async Task SendAsync(SmsMessage smsMessage)
        {
            var domain = await SettingProvider.GetOrNullAsync(AliyunSmsSettingNames.Sms.Domain);

            var action = await SettingProvider.GetOrNullAsync(AliyunSmsSettingNames.Sms.ActionName);

            var version = await SettingProvider.GetOrNullAsync(AliyunSmsSettingNames.Sms.Version);

            Check.NotNullOrWhiteSpace(domain, AliyunSmsSettingNames.Sms.Domain);
            Check.NotNullOrWhiteSpace(action, AliyunSmsSettingNames.Sms.ActionName);
            Check.NotNullOrWhiteSpace(version, AliyunSmsSettingNames.Sms.Version);

            CommonRequest request = new CommonRequest
            {
                Method  = MethodType.POST,
                Domain  = domain,
                Action  = action,
                Version = version
            };

            await TryAddTemplateCodeAsync(request, smsMessage);
            await TryAddSignNameAsync(request, smsMessage);
            await TryAddSendPhoneAsync(request, smsMessage);

            TryAddTemplateParam(request, smsMessage);

            try
            {
                var client = await AcsClientFactory.CreateAsync();

                CommonResponse response        = client.GetCommonResponse(request);
                var            responseContent = Encoding.Default.GetString(response.HttpResponse.Content);
                var            aliyunResponse  = JsonSerializer.Deserialize <AliyunSmsResponse>(responseContent);
                if (!aliyunResponse.IsSuccess())
                {
                    if (await SettingProvider.IsTrueAsync(AliyunSmsSettingNames.Sms.VisableErrorToClient))
                    {
                        throw new UserFriendlyException(aliyunResponse.Code, aliyunResponse.Message);
                    }
                    throw new AliyunSmsException(aliyunResponse.Code, $"Text message sending failed, code:{aliyunResponse.Code}, message:{aliyunResponse.Message}!");
                }
            }
            catch (ServerException se)
            {
                throw new AliyunSmsException(se.ErrorCode, $"Sending text messages to aliyun server is abnormal,type: {se.ErrorType}, error: {se.ErrorMessage}");
            }
            catch (ClientException ce)
            {
                throw new AliyunSmsException(ce.ErrorCode, $"A client exception occurred in sending SMS messages,type: {ce.ErrorType}, error: {ce.ErrorMessage}");
            }
        }
Пример #13
0
        private async Task TryAddTemplateCodeAsync(CommonRequest request, SmsMessage smsMessage)
        {
            if (smsMessage.Properties.TryGetValue("TemplateCode", out object template) && template != null)
            {
                request.AddQueryParameters("TemplateCode", template.ToString());
                smsMessage.Properties.Remove("TemplateCode");
            }
            else
            {
                var defaultTemplateCode = await SettingProvider.GetOrNullAsync(AliyunSmsSettingNames.Sms.DefaultTemplateCode);

                Check.NotNullOrWhiteSpace(defaultTemplateCode, "TemplateCode");
                request.AddQueryParameters("TemplateCode", defaultTemplateCode);
            }
        }
        public virtual async Task SendPhoneResetPasswordCodeAsync(SendPhoneResetPasswordCodeDto input)
        {
            /*
             * 注解: 微软的重置密码方法通过 UserManager.GeneratePasswordResetTokenAsync 接口生成密码重置Token
             *       而这个Token设计的意义就是用户通过链接来重置密码,所以不适合短信验证
             *       某些企业是把链接生成一个短链发送短信的,不过这种方式不是很推荐,因为现在是真没几个人敢随便点短信链接的
             *
             *  此处设计方式为:
             *
             *  step1: 例行检查是否重复发送,这一点是很有必要的
             *  step2: 通过已确认的手机号来查询用户,如果用户未确认手机号,那就不能发送,这一点也是很有必要的
             *  step3(重点): 通过 UserManager.GenerateTwoFactorTokenAsync 接口来生成二次认证码,这就相当于伪验证码,只是用于确认用户传递的验证码是否通过
             *               比起自己生成随机数,这个验证码利用了TOTP算法,有时间限制的
             *  step4(重点): 用户传递验证码后,通过 UserManager.VerifyTwoFactorTokenAsync 接口来校验验证码
             *               验证通过后,再利用 UserManager.GeneratePasswordResetTokenAsync 接口来生成真正的用于重置密码的Token
             */

            var securityTokenCacheKey  = SmsSecurityTokenCacheItem.CalculateCacheKey(input.PhoneNumber, "SmsVerifyCode");
            var securityTokenCacheItem = await SecurityTokenCache.GetAsync(securityTokenCacheKey);

            var interval = await SettingProvider.GetAsync(IdentitySettingNames.User.SmsRepetInterval, 1);

            // 传递 isConfirmed 用户必须是已确认过手机号的
            var user = await GetUserByPhoneNumberAsync(input.PhoneNumber, isConfirmed : true);

            // 能查询到缓存就是重复发送
            if (securityTokenCacheItem != null)
            {
                throw new UserFriendlyException(L["SendRepeatSmsVerifyCode", interval]);
            }

            var template = await SettingProvider.GetOrNullAsync(IdentitySettingNames.User.SmsResetPassword);

            // 生成二次认证码
            var code = await UserManager.GenerateTwoFactorTokenAsync(user, TokenOptions.DefaultPhoneProvider);

            // 发送短信验证码
            await SecurityCodeSender.SendPhoneConfirmedCodeAsync(input.PhoneNumber, code, template);

            // 缓存这个手机号的记录,防重复
            securityTokenCacheItem = new SmsSecurityTokenCacheItem(code, user.SecurityStamp);
            await SecurityTokenCache
            .SetAsync(securityTokenCacheKey, securityTokenCacheItem,
                      new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(interval)
            });
        }
Пример #15
0
        protected virtual async Task <string> SendResetPasswordVerifyCodeAsync(string phoneNumber, string phoneVerifyCode)
        {
            // 查找用户信息
            var user = await GetUserByPhoneNumberAsync(phoneNumber);

            // 获取登录验证码模板号
            var templateCode = await SettingProvider.GetOrNullAsync(AccountSettingNames.SmsResetPasswordTemplateCode);

            // 生成重置密码验证码
            var phoneVerifyToken = await UserManager.GeneratePasswordResetTokenAsync(user);

            // 发送短信验证码
            await SendPhoneVerifyMessageAsync(templateCode, user.PhoneNumber, phoneVerifyCode);

            return(phoneVerifyToken);
        }
Пример #16
0
        /// <summary>
        /// 发送登录验证码
        /// </summary>
        /// <param name="phoneNumber">手机号</param>
        /// <returns>返回登录验证码</returns>
        protected virtual async Task <string> SendSigninVerifyCodeAsync(string phoneNumber)
        {
            // 查找用户信息
            var user = await GetUserByPhoneNumberAsync(phoneNumber);

            // 获取登录验证码模板号
            var templateCode = await SettingProvider.GetOrNullAsync(AccountSettingNames.SmsSigninTemplateCode);

            // 生成手机验证码
            var phoneVerifyCode = await PhoneNumberTokenProvider.GenerateAsync("phone_verify", UserManager, user);

            // 发送短信验证码
            await SendPhoneVerifyMessageAsync(templateCode, user.PhoneNumber, phoneVerifyCode);

            return(phoneVerifyCode);
        }
Пример #17
0
        /// <summary>
        /// 验证手机号码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <remarks>
        /// 用户传递手机号码及认证类型
        /// 1、如果认证类型为注册:
        ///     先查询是否存在此手机号的缓存验证码信息,如果存在,抛出不能重复发送验证码异常
        ///     随机生成6位纯数字验证码,通过短信接口服务发送到用户手机,并缓存验证码,设定一个有效时长
        ///
        /// 2、如果认证类型为登录:
        ///     先查询是否存在此手机号的缓存验证码信息,如果存在,抛出不能重复发送验证码异常
        ///     通过手机号查询用户信息,如果用户不存在,抛出手机号未注册异常
        ///     调用PhoneNumberTokenProvider接口生成6位手机验证码,用途为 phone_verify
        ///     发送手机验证码到用户手机,并缓存验证码,设定一个有效时长
        ///
        ///     用户调用 IdentityServer4/connect/token 登录系统(需要引用LINGYUN.Abp.IdentityServer.SmsValidator模块)
        ///         参数1:grant_type=phone_verify
        ///         参数2:phone_number=手机号码
        ///         参数3:phone_verify_code=手机验证码
        ///         参数4:client_id=客户端标识
        ///         参数5:client_secret=客户端密钥
        /// </remarks>
        public virtual async Task VerifyPhoneNumberAsync(VerifyDto input)
        {
            var verifyCodeExpiration = await SettingProvider.GetAsync <int>(AccountSettingNames.PhoneVerifyCodeExpiration);

            var phoneVerifyCacheKey = NormalizeCacheKey(input.PhoneNumber);
            var verifyCacheItem     = await Cache.GetAsync(phoneVerifyCacheKey);

            if (verifyCacheItem != null)
            {
                throw new UserFriendlyException(L["PhoneVerifyCodeNotRepeatSend", verifyCodeExpiration]);
            }
            verifyCacheItem = new AccountRegisterVerifyCacheItem
            {
                PhoneNumber = input.PhoneNumber,
            };
            switch (input.VerifyType)
            {
            case PhoneNumberVerifyType.Register:
                var phoneVerifyCode = new Random().Next(100000, 999999);
                verifyCacheItem.VerifyCode = phoneVerifyCode.ToString();
                var templateCode = await SettingProvider.GetOrNullAsync(AccountSettingNames.SmsRegisterTemplateCode);
                await SendPhoneVerifyMessageAsync(templateCode, input.PhoneNumber, phoneVerifyCode.ToString());

                return;

            case PhoneNumberVerifyType.Signin:
                var phoneSigninCode = await SendSigninVerifyCodeAsync(input.PhoneNumber);

                verifyCacheItem.VerifyCode = phoneSigninCode;
                break;

            case PhoneNumberVerifyType.ResetPassword:
                var resetPasswordCode = new Random().Next(100000, 999999);
                verifyCacheItem.VerifyCode = resetPasswordCode.ToString();
                var resetPasswordToken = await SendResetPasswordVerifyCodeAsync(input.PhoneNumber, verifyCacheItem.VerifyCode);

                verifyCacheItem.VerifyToken = resetPasswordToken;
                break;
            }

            var cacheOptions = new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(verifyCodeExpiration)
            };
            await Cache.SetAsync(phoneVerifyCacheKey, verifyCacheItem, cacheOptions);
        }
        protected virtual async Task <AliyunBasicSessionCredentialsCacheItem> GetCacheItemAsync(string accessKeyId, string accessKeySecret, string regionId)
        {
            var cacheItem = await Cache.GetAsync(AliyunBasicSessionCredentialsCacheItem.CacheKey);

            if (cacheItem == null)
            {
                var roleArn = await SettingProvider.GetOrNullAsync(AliyunSettingNames.Authorization.RamRoleArn);

                var roleSession = await SettingProvider.GetOrNullAsync(AliyunSettingNames.Authorization.RoleSessionName);

                Check.NotNullOrWhiteSpace(roleArn, AliyunSettingNames.Authorization.RamRoleArn);

                var policy = await SettingProvider.GetOrNullAsync(AliyunSettingNames.Authorization.Policy);

                var durationSeconds = await SettingProvider.GetAsync(AliyunSettingNames.Authorization.DurationSeconds, 3000);

                var profile = DefaultProfile.GetProfile(regionId, accessKeyId, accessKeySecret);
                var request = new AssumeRoleRequest
                {
                    AcceptFormat    = FormatType.JSON,
                    RoleArn         = roleArn,
                    RoleSessionName = roleSession,
                    DurationSeconds = durationSeconds,
                    Policy          = policy.IsNullOrWhiteSpace() ? null : policy
                };

                var client   = new DefaultAcsClient(profile);
                var response = client.GetAcsResponse(request);

                cacheItem = new AliyunBasicSessionCredentialsCacheItem(
                    response.Credentials.AccessKeyId,
                    response.Credentials.AccessKeySecret,
                    response.Credentials.SecurityToken);

                await Cache.SetAsync(
                    AliyunBasicSessionCredentialsCacheItem.CacheKey,
                    cacheItem,
                    new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(durationSeconds - 10)
                });
            }

            return(cacheItem);
        }
Пример #19
0
        private async Task TryAddSendPhoneAsync(CommonRequest request, SmsMessage smsMessage)
        {
            if (smsMessage.PhoneNumber.IsNullOrWhiteSpace())
            {
                var defaultPhoneNumber = await SettingProvider.GetOrNullAsync(AliyunSmsSettingNames.Sms.DefaultPhoneNumber);

                // check phone number length...
                Check.NotNullOrWhiteSpace(
                    defaultPhoneNumber,
                    AliyunSmsSettingNames.Sms.DefaultPhoneNumber,
                    maxLength: 11, minLength: 11);
                request.AddQueryParameters("PhoneNumbers", defaultPhoneNumber);
            }
            else
            {
                request.AddQueryParameters("PhoneNumbers", smsMessage.PhoneNumber);
            }
        }
Пример #20
0
        public virtual async Task <OssObjectDto> CreateAsync(CreateOssObjectInput input)
        {
            if (!input.Content.IsNullOrEmpty())
            {
                // 检查文件大小
                var fileSizeLimited = await SettingProvider
                                      .GetAsync(
                    AbpOssManagementSettingNames.FileLimitLength,
                    AbpOssManagementSettingNames.DefaultFileLimitLength);

                if (fileSizeLimited * 1024 * 1024 < input.Content.Length)
                {
                    ThrowValidationException(L["UploadFileSizeBeyondLimit", fileSizeLimited], nameof(input.Content));
                }

                // 文件扩展名
                var fileExtensionName  = FileHelper.GetExtension(input.Object);
                var fileAllowExtension = await SettingProvider.GetOrNullAsync(AbpOssManagementSettingNames.AllowFileExtensions);

                // 检查文件扩展名
                if (!fileAllowExtension.Split(',')
                    .Any(fe => fe.Equals(fileExtensionName, StringComparison.CurrentCultureIgnoreCase)))
                {
                    ThrowValidationException(L["NotAllowedFileExtensionName", fileExtensionName], "FileName");
                }
            }

            var oss = CreateOssContainer();

            var createOssObjectRequest = new CreateOssObjectRequest(
                input.Bucket,
                input.Object,
                input.Content,
                input.Path,
                input.ExpirationTime)
            {
                Overwrite = input.Overwrite
            };
            var ossObject = await oss.CreateObjectAsync(createOssObjectRequest);

            return(ObjectMapper.Map <OssObject, OssObjectDto>(ossObject));
        }
Пример #21
0
        public async Task DeleteFileByDocumentIdAndFileNameAsync(DeleteFileByDocumentIdAndFileNameInput input)
        {
            var document = await DocumentRepository.GetAsync(input.Id);

            if (document?.Id < 0)
            {
                throw new BusinessException(code: DocumentManagementErrorCodes.Document.CodeNotExists)
                      .WithData("Code", "Not Found");
            }

            if (!string.IsNullOrEmpty(document.FileName))
            {
                List <string> files = document.FileName.Split(';').ToList();
                files.Remove(input.FileName);
                document.FileName = string.Join(";", files);
                document.LinkFile = string.Empty;
                string uploadFolderPath = await SettingProvider.GetOrNullAsync(DocumentManagementSettings.UploadFilePath);

                File.Delete($"{uploadFolderPath}/{document.FolderName}/{input.FileName}");
            }
        }
        public virtual async Task <TClient> CreateAsync()
        {
            var regionId = await SettingProvider.GetOrNullAsync(AliyunSettingNames.Authorization.RegionId);

            var accessKey = await SettingProvider.GetOrNullAsync(AliyunSettingNames.Authorization.AccessKeyId);

            var accessKeySecret = await SettingProvider.GetOrNullAsync(AliyunSettingNames.Authorization.AccessKeySecret);

            Check.NotNullOrWhiteSpace(regionId, AliyunSettingNames.Authorization.RegionId);
            Check.NotNullOrWhiteSpace(accessKey, AliyunSettingNames.Authorization.AccessKeyId);
            Check.NotNullOrWhiteSpace(accessKeySecret, AliyunSettingNames.Authorization.AccessKeySecret);

            if (await SettingProvider.IsTrueAsync(AliyunSettingNames.Authorization.UseSecurityTokenService))
            {
                var cacheItem = await GetCacheItemAsync(accessKey, accessKeySecret, regionId);

                return(GetSecurityTokenClient(regionId, cacheItem.AccessKeyId, cacheItem.AccessKeySecret, cacheItem.SecurityToken));
            }

            return(GetClient(regionId, accessKey, accessKeySecret));
        }
Пример #23
0
        public virtual async Task <string> CreateAsync(FileUploadInputDto input)
        {
            if (input.Bytes.IsNullOrEmpty())
            {
                throw new AbpValidationException("Bytes can not be null or empty!",
                                                 new List <ValidationResult>
                {
                    new ValidationResult("Bytes can not be null or empty!", new[] { "Bytes" })
                });
            }

            var allowedMaxFileSize = await SettingProvider.GetAsync <int>(FileManagementSettings.AllowedMaxFileSize);//kb

            var allowedUploadFormats = (await SettingProvider.GetOrNullAsync(FileManagementSettings.AllowedUploadFormats))
                                       ?.Split(",", StringSplitOptions.RemoveEmptyEntries);

            if (input.Bytes.Length > allowedMaxFileSize * 1024)
            {
                throw new UserFriendlyException(L["FileManagement.ExceedsTheMaximumSize", allowedMaxFileSize]);
            }

            if (allowedUploadFormats == null || !allowedUploadFormats.Contains(Path.GetExtension(input.Name)))
            {
                throw new UserFriendlyException(L["FileManagement.NotValidFormat"]);
            }

            var fileName = Guid.NewGuid().ToString("N") + Path.GetExtension(input.Name);
            var filePath = Path.Combine(FileOptions.FileUploadLocalFolder, fileName);

            if (!Directory.Exists(FileOptions.FileUploadLocalFolder))
            {
                Directory.CreateDirectory(FileOptions.FileUploadLocalFolder);
            }

            File.WriteAllBytes(filePath, input.Bytes);

            return(fileName);
        }
Пример #24
0
        public virtual async Task AppendFileAsync([FromQuery] VersionFileCreateDto versionFileCreate)
        {
            // 检查文件大小
            var fileSizeLimited = await SettingProvider
                                  .GetAsync(PlatformSettingNames.AppVersion.VersionFileLimitLength, AppVersionConsts.DefaultVersionFileLimitLength);

            if (fileSizeLimited * 1024 * 1024 < versionFileCreate.TotalByte)
            {
                throw new UserFriendlyException(L["UploadFileSizeBeyondLimit", fileSizeLimited]);
            }
            // 采用分块模式上传文件

            // 保存分块到临时目录
            var fileName = versionFileCreate.FileName;
            // 文件扩展名
            var fileExtensionName  = FileHelper.GetExtension(fileName);
            var fileAllowExtension = await SettingProvider
                                     .GetOrNullAsync(PlatformSettingNames.AppVersion.AllowVersionFileExtensions);

            if (fileAllowExtension.IsNullOrWhiteSpace())
            {
                fileAllowExtension = AppVersionConsts.DefaultAllowVersionFileExtensions;
            }
            // 检查文件扩展名
            if (!fileAllowExtension.Split(',').Any(fe => fe.Equals(fileExtensionName, StringComparison.CurrentCultureIgnoreCase)))
            {
                throw new UserFriendlyException(L["NotAllowedFileExtensionName", fileExtensionName]);
            }
            // 当前计算机临时目录
            var tempFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Templates);

            // 以上传的文件名创建一个临时目录
            tempFilePath = Path.Combine(tempFilePath, "lingyun-platform", Path.GetFileNameWithoutExtension(fileName));
            // 以上传的分片索引创建临时文件
            var tempSavedFile = Path.Combine(tempFilePath, $"{versionFileCreate.CurrentByte}.{fileExtensionName}");

            if (!Directory.Exists(tempFilePath))
            {
                // 临时目录不存在则创建
                Directory.CreateDirectory(tempFilePath);
            }
            try
            {
                if (HttpContext.RequestAborted.IsCancellationRequested)
                {
                    // 如果取消请求,删除临时目录
                    Directory.Delete(tempFilePath, true);
                    return;
                }
                // 保存临时文件
                using (var fs = new FileStream(tempSavedFile, FileMode.Create, FileAccess.Write))
                {
                    // 写入当前分片文件
                    await Request.Body.CopyToAsync(fs);
                }

                if (versionFileCreate.CurrentByte == versionFileCreate.TotalByte)
                {
                    // 合并文件
                    var mergeSavedFile = Path.Combine(tempFilePath, $"{fileName}");
                    // 获取并排序所有分片文件
                    var mergeFiles = Directory.GetFiles(tempFilePath).OrderBy(f => f.Length).ThenBy(f => f);
                    // 创建临时合并文件
                    using (var mergeSavedFileStream = new FileStream(mergeSavedFile, FileMode.Create))
                    {
                        foreach (var mergeFile in mergeFiles)
                        {
                            // 读取当前文件字节
                            var mergeFileBytes = await FileHelper.ReadAllBytesAsync(mergeFile);

                            // 写入到合并文件流
                            await mergeSavedFileStream.WriteAsync(mergeFileBytes, 0, mergeFileBytes.Length);

                            // 删除已参与合并的临时文件分片
                            FileHelper.DeleteIfExists(mergeFile);
                        }
                        // 上传最终合并的文件并取得SHA256指纹
                        var fileData = await mergeSavedFileStream.GetAllBytesAsync();

                        versionFileCreate.SHA256 = await _versionFileManager.SaveFileAsync(versionFileCreate.Version,
                                                                                           versionFileCreate.FilePath, versionFileCreate.FileName, versionFileCreate.FileVersion, fileData);
                    }
                    // 添加到版本信息
                    await _versionAppService.AppendFileAsync(versionFileCreate);

                    // 文件保存之后删除临时文件目录
                    Directory.Delete(tempFilePath, true);
                }
            }
            catch
            {
                // 发生异常删除临时文件目录
                Directory.Delete(tempFilePath, true);
                throw;
            }
        }
Пример #25
0
 /// <summary>
 /// 获取更新的时间戳, 用于播放时进行比较
 /// </summary>
 /// <returns></returns>
 public async Task <int> GetUpdatedTimeStampAsync()
 {
     return(int.Parse(await SettingProvider.GetOrNullAsync(CMSSettings.AnnouncementUpdatedTimeStamp)));
 }
Пример #26
0
 public Task <string> GetDomainAsync()
 {
     return(SettingProvider.GetOrNullAsync(EmailSettingNames.Smtp.Domain));
 }
Пример #27
0
        public virtual async Task CreateFileAsync([FromForm] FileUploadDto input)
        {
            // 检查文件大小
            var fileSizeLimited = await SettingProvider
                                  .GetAsync(
                AbpFileManagementSettingNames.FileLimitLength,
                AbpFileManagementSettingNames.DefaultFileLimitLength);

            if (fileSizeLimited * 1024 * 1024 < input.TotalSize)
            {
                throw new UserFriendlyException(L["UploadFileSizeBeyondLimit", fileSizeLimited]);
            }
            // 采用分块模式上传文件

            // 保存分块到临时目录
            var fileName = input.FileName;
            // 文件扩展名
            var fileExtensionName  = FileHelper.GetExtension(fileName);
            var fileAllowExtension = await SettingProvider
                                     .GetOrNullAsync(AbpFileManagementSettingNames.AllowFileExtensions);

            if (fileAllowExtension.IsNullOrWhiteSpace())
            {
                fileAllowExtension = AbpFileManagementSettingNames.DefaultAllowFileExtensions;
            }
            // 检查文件扩展名
            if (!fileAllowExtension.Split(',')
                .Any(fe => fe.Equals(fileExtensionName, StringComparison.CurrentCultureIgnoreCase)))
            {
                throw new UserFriendlyException(L["NotAllowedFileExtensionName", fileExtensionName]);
            }
            // 以上传的文件名创建一个临时目录
            var tempFilePath = Path.Combine(
                Path.GetTempPath(),
                "lingyun-abp-file-management",
                "upload",
                string.Concat(input.Path ?? "", input.FileName).ToMd5());

            DirectoryHelper.CreateIfNotExists(tempFilePath);
            // 以上传的分片索引创建临时文件
            var tempSavedFile = Path.Combine(tempFilePath, $"{input.ChunkNumber}.{fileExtensionName}");

            try
            {
                if (HttpContext.RequestAborted.IsCancellationRequested)
                {
                    // 如果取消请求,删除临时目录
                    Directory.Delete(tempFilePath, true);
                    return;
                }

                if (input.File != null)
                {
                    // 保存临时文件
                    using (var fs = new FileStream(tempSavedFile, FileMode.Create, FileAccess.Write))
                    {
                        // 写入当前分片文件
                        await input.File.CopyToAsync(fs);
                    }
                }

                if (input.ChunkNumber == input.TotalChunks)
                {
                    // 合并文件
                    var mergeSavedFile = Path.Combine(tempFilePath, $"{fileName}");
                    // 获取并排序所有分片文件
                    var mergeFiles = Directory.GetFiles(tempFilePath).OrderBy(f => f.Length).ThenBy(f => f);
                    // 创建临时合并文件
                    input.Data = new byte[0];
                    foreach (var mergeFile in mergeFiles)
                    {
                        // 读取当前文件字节
                        var mergeFileBytes = await FileHelper.ReadAllBytesAsync(mergeFile);

                        // 写入到合并文件流
                        input.Data = input.Data.Concat(mergeFileBytes).ToArray();
                        Array.Clear(mergeFileBytes, 0, mergeFileBytes.Length);
                        // 删除已参与合并的临时文件分片
                        FileHelper.DeleteIfExists(mergeFile);
                    }
                    await FileSystemAppService.CreateFileAsync(input);

                    // 文件保存之后删除临时文件目录
                    Directory.Delete(tempFilePath, true);
                }
            }
            catch
            {
                // 发生异常删除临时文件目录
                Directory.Delete(tempFilePath, true);
                throw;
            }
        }
Пример #28
0
        public override async Task <IActionResult> OnGetAsync()
        {
            MiniProgramName ??= await SettingProvider.GetOrNullAsync(MiniProgramsSettings.PcLogin.DefaultProgramName);

            return(await base.OnGetAsync());
        }
        public virtual async Task <IActionResult> OnPostAsync()
        {
            var user = await SignInManager.GetTwoFactorAuthenticationUserAsync();

            if (user == null)
            {
                Alerts.Warning(L["TwoFactorAuthenticationInvaidUser"]);
                return(Page());
            }

            if (Input.SelectedProvider == "Authenticator")
            {
                // 用户通过邮件/短信链接进入授权页面
                return(RedirectToPage("VerifyAuthenticatorCode", new
                {
                    returnUrl = ReturnUrl,
                    returnUrlHash = ReturnUrlHash,
                    rememberMe = RememberMe
                }));
            }
            // 生成验证码
            var code = await UserManager.GenerateTwoFactorTokenAsync(user, Input.SelectedProvider);

            if (string.IsNullOrWhiteSpace(code))
            {
                Alerts.Warning(L["InvaidGenerateTwoFactorToken"]);
                return(Page());
            }

            if (Input.SelectedProvider == "Email")
            {
                var appName = "MVC"; // TODO: 跟随Abp框架的意思变动
                await AccountEmailVerifySender
                .SendMailLoginVerifyLinkAsync(
                    user, code, appName,
                    Input.SelectedProvider,
                    RememberMe, ReturnUrl, ReturnUrlHash);
            }
            else if (Input.SelectedProvider == "Phone")
            {
                var phoneNumber = await UserManager.GetPhoneNumberAsync(user);

                var templateCode = await SettingProvider.GetOrNullAsync(AccountSettingNames.SmsSigninTemplateCode);

                Check.NotNullOrWhiteSpace(templateCode, nameof(AccountSettingNames.SmsSigninTemplateCode));

                // TODO: 以后扩展短信模板发送
                var smsMessage = new SmsMessage(phoneNumber, code);
                smsMessage.Properties.Add("code", code);
                smsMessage.Properties.Add("TemplateCode", templateCode);

                await SmsSender.SendAsync(smsMessage);
            }

            return(RedirectToPage("VerifyCode", new
            {
                provider = Input.SelectedProvider,
                returnUrl = ReturnUrl,
                returnUrlHash = ReturnUrlHash,
                rememberMe = RememberMe
            }));
        }