//public async Task<AuthCode> SendCode(string phone, AuthCodeMessageType messageType) //{ // if (string.IsNullOrEmpty(phone)) // throw new ApplicationException(Resx.AppResources.InvalidPhoneException); // var code = OtpTools.GenRandomNumber(6); // var authCode = new AuthCode() // { // Phone = phone, // IsRegistered = false, // MessageType = AuthCodeMessageType.SmsMessageWithCode, // IsPassword = false, // CodeHash = CryptoProvider.SHA1(CryptoProvider.SHA1(code)).ToLower(), // //Token = CryptoProvider.HMACSHA1(phone, OtpTools.GetOtpTime()).ToLower(), // }; // return await Task.Run<AuthCode>(async () => // { // var user = _dataManager.Get<User>(new { Phone = phone }); // if (user != null) // { // authCode.IsRegistered = true; // } // authCode.CreateTime = DateTimeOffset.UtcNow; // authCode.ExpieryTime = DateTimeOffset.UtcNow.AddSeconds(180); // authCode.Id = _dataManager.Insert<AuthCode, long>(authCode); // // Send Message // _notificationProvider?.SendPhoneVerificationMessage(phone, code, user?.AppName, messageType); // return authCode; // }); //} public async Task <AuthCode> SendCode(string recipient, AuthCodeMessageType messageType, string appName) { if (string.IsNullOrEmpty(recipient)) { throw new ApplicationException("Invalid recipient."); } User user = null; if (messageType == AuthCodeMessageType.Email) { user = _dataManager.Get <User>(new { Email = recipient }); } else { user = _dataManager.Get <User>(new { Phone = recipient }); } var code = OtpTools.GenRandomNumber(6); var authCode = new AuthCode() { Recipient = recipient, IsRegistered = user != null, MessageType = messageType, IsPassword = false, CodeHash = CryptoProvider.SHA1(CryptoProvider.SHA1(code)).ToLower(), //Token = CryptoProvider.HMACSHA1(phone, OtpTools.GetOtpTime()).ToLower(), CreateTime = DateTimeOffset.UtcNow, ExpieryTime = messageType == AuthCodeMessageType.Email ? DateTimeOffset.UtcNow.AddDays(30) : DateTimeOffset.UtcNow.AddSeconds(180) }; authCode.Id = _dataManager.Insert <AuthCode, long>(authCode); // Send Message switch (messageType) { case AuthCodeMessageType.SmsMessageWithCode: case AuthCodeMessageType.SmsMessageWithAppLink: case AuthCodeMessageType.ChatMessage: case AuthCodeMessageType.PhoneCall: case AuthCodeMessageType.PushMessage: await _notificationProvider?.SendPhoneVerificationMessage(recipient, user?.DisplayName, code, appName); break; case AuthCodeMessageType.Email: var token = Convert.ToBase64String(Encoding.Unicode.GetBytes($"{recipient}&{code}&{authCode.ExpieryTime}")); var link = $"{EmailVerificationUrl}?token={HttpUtility.UrlEncode(token)}"; await _notificationProvider?.SendEmailVerificationMessage(recipient, user?.DisplayName, link, appName); break; default: break; } return(authCode); }