public void PutAuthCode(UserInfo user, bool again) { if (user == null || Equals(user, Constants.LostUser)) { throw new Exception(Resource.ErrorUserNotFound); } if (!StudioSmsNotificationSettingsHelper.IsVisibleSettings() || !StudioSmsNotificationSettingsHelper.Enable) { throw new MethodAccessException(); } var mobilePhone = SmsSender.GetPhoneValueDigits(user.MobilePhone); if (SmsKeyStorage.ExistsKey(mobilePhone) && !again) { return; } if (!SmsKeyStorage.GenerateKey(mobilePhone, out var key)) { throw new Exception(Resource.SmsTooMuchError); } if (SmsSender.SendSMS(mobilePhone, string.Format(Resource.SmsAuthenticationMessageToUser, key))) { TenantManager.SetTenantQuotaRow(new TenantQuotaRow { Tenant = TenantManager.GetCurrentTenant().TenantId, Path = "/sms", Counter = 1 }, true); } }
public SmsManager( UserManager userManager, SecurityContext securityContext, TenantManager tenantManager, SmsKeyStorage smsKeyStorage, SmsSender smsSender, StudioSmsNotificationSettingsHelper studioSmsNotificationSettingsHelper) { UserManager = userManager; SecurityContext = securityContext; TenantManager = tenantManager; SmsKeyStorage = smsKeyStorage; SmsSender = smsSender; StudioSmsNotificationSettingsHelper = studioSmsNotificationSettingsHelper; }
public static void ValidateSmsCode(UserInfo user, string code) { if (!StudioSmsNotificationSettings.IsVisibleSettings || !StudioSmsNotificationSettings.Enable) { return; } if (user == null || Equals(user, Constants.LostUser)) { throw new Exception(Resource.ErrorUserNotFound); } code = (code ?? "").Trim(); if (string.IsNullOrEmpty(code)) { throw new Exception(Resource.ActivateMobilePhoneEmptyCode); } int counter; int.TryParse(CodeCache.Get <String>("loginsec/" + user.ID), out counter); if (++counter % 5 == 0) { Thread.Sleep(TimeSpan.FromSeconds(10)); } CodeCache.Insert("loginsec/" + user.ID, counter.ToString(), DateTime.UtcNow.Add(TimeSpan.FromMinutes(1))); if (!SmsKeyStorage.ValidateKey(user.MobilePhone, code)) { throw new ArgumentException(Resource.SmsAuthenticationMessageError); } if (!SecurityContext.IsAuthenticated) { var cookiesKey = SecurityContext.AuthenticateMe(user.ID); CookiesManager.SetCookies(CookiesType.AuthKey, cookiesKey); } if (user.MobilePhoneActivationStatus == MobilePhoneActivationStatus.NotActivated) { user.MobilePhoneActivationStatus = MobilePhoneActivationStatus.Activated; CoreContext.UserManager.SaveUserInfo(user); } }
public void ValidateSmsCode(UserInfo user, string code) { if (!StudioSmsNotificationSettingsHelper.IsVisibleSettings() || !StudioSmsNotificationSettingsHelper.Enable) { return; } if (user == null || Equals(user, Constants.LostUser)) { throw new Exception(Resource.ErrorUserNotFound); } var valid = SmsKeyStorage.ValidateKey(user.MobilePhone, code); switch (valid) { case SmsKeyStorage.Result.Empty: throw new Exception(Resource.ActivateMobilePhoneEmptyCode); case SmsKeyStorage.Result.TooMuch: throw new BruteForceCredentialException(Resource.SmsTooMuchError); case SmsKeyStorage.Result.Timeout: throw new TimeoutException(Resource.SmsAuthenticationTimeout); case SmsKeyStorage.Result.Invalide: throw new ArgumentException(Resource.SmsAuthenticationMessageError); } if (valid != SmsKeyStorage.Result.Ok) { throw new Exception("Error: " + valid); } if (!SecurityContext.IsAuthenticated) { var cookiesKey = SecurityContext.AuthenticateMe(user.ID); //CookiesManager.SetCookies(CookiesType.AuthKey, cookiesKey); } if (user.MobilePhoneActivationStatus == MobilePhoneActivationStatus.NotActivated) { user.MobilePhoneActivationStatus = MobilePhoneActivationStatus.Activated; UserManager.SaveUserInfo(user); } }
public static void ValidateSmsCode(UserInfo user, string code, bool isEntryPoint = false) { if (!StudioSmsNotificationSettings.IsVisibleAndAvailableSettings || !StudioSmsNotificationSettings.Enable) { return; } if (user == null || Equals(user, Constants.LostUser)) { throw new Exception(Resource.ErrorUserNotFound); } var valid = SmsKeyStorage.ValidateKey(user.MobilePhone, code); switch (valid) { case SmsKeyStorage.Result.Empty: throw new Exception(Resource.ActivateMobilePhoneEmptyCode); case SmsKeyStorage.Result.TooMuch: throw new Authorize.BruteForceCredentialException(Resource.SmsTooMuchError); case SmsKeyStorage.Result.Timeout: throw new TimeoutException(Resource.SmsAuthenticationTimeout); case SmsKeyStorage.Result.Invalide: throw new ArgumentException(Resource.SmsAuthenticationMessageError); } if (valid != SmsKeyStorage.Result.Ok) { throw new Exception("Error: " + valid); } if (!SecurityContext.IsAuthenticated) { var action = isEntryPoint ? MessageAction.LoginSuccessViaApiSms : MessageAction.LoginSuccessViaSms; CookiesManager.AuthenticateMeAndSetCookies(user.Tenant, user.ID, action); } if (user.MobilePhoneActivationStatus == MobilePhoneActivationStatus.NotActivated) { user.MobilePhoneActivationStatus = MobilePhoneActivationStatus.Activated; CoreContext.UserManager.SaveUserInfo(user); } }
public static void PutAuthCode(UserInfo user, bool again) { if (user == null || Equals(user, Constants.LostUser)) { throw new Exception(Resource.ErrorUserNotFound); } var mobilePhone = GetPhoneValueDigits(user.MobilePhone); if (SmsKeyStorage.ExistsKey(mobilePhone) && !again) { return; } var key = SmsKeyStorage.GenerateKey(mobilePhone); SmsSender.SendSMS(mobilePhone, string.Format(Resource.SmsAuthenticationMessageToUser, key)); }
public SmsManager( UserManager userManager, TenantExtra tenantExtra, SecurityContext securityContext, TenantManager tenantManager, SmsKeyStorage smsKeyStorage, IConfiguration configuration, SmsSender smsSender, StudioSmsNotificationSettingsHelper studioSmsNotificationSettingsHelper) { UserManager = userManager; TenantExtra = tenantExtra; SecurityContext = securityContext; TenantManager = tenantManager; SmsKeyStorage = smsKeyStorage; Configuration = configuration; SmsSender = smsSender; StudioSmsNotificationSettingsHelper = studioSmsNotificationSettingsHelper; }