public ArticlesController(DbInteractor context) { _context = context; }
public async Task <IActionResult> SaveActiveProfile([FromBody] AccountProfileViewModel vm = null) { Account activeAccount = null; string fullName = null; AccountToken toSend = null; List <IdentityModel> toSave = null; Exception ex1 = null, ex2 = null; ClaimsIdentity claimsIdentity = null; try { if (vm == null) { throw new NullReferenceException(); } if (!string.IsNullOrEmpty(vm.Email) && !Helper.IsEmailValid(vm.Email)) { throw new ExceptionID(MessageIdentifier.INVALID_EMAIL); } activeAccount = _dbi.GetActiveAccount(); if (activeAccount == null || activeAccount.UID != vm.UID) { throw new NullReferenceException(); } toSave = new List <IdentityModel>(); if (!string.IsNullOrEmpty(vm.Email) && activeAccount.GetEmailValue()?.ToLower() != vm.Email.ToLower()) { string emailValue = activeAccount.GetEmailValue(); if (Helper.IsEmailValid(emailValue)) { toSave.AddRange(from t in activeAccount.AccountTokens where t.Type == AccountTokenType.EmailChange select t.SetRecordState(RecordState.Deleted)); } toSend = new AccountToken() { RecordState = RecordState.Added, Value = _dbi.GenerateTokenValue(), Type = AccountTokenType.EmailChange, EmailSentStatus = EmailSatus.NotSent, AddData = vm.Email, Account_Id = activeAccount.ID }; activeAccount.RegisterForRecordStateChange(); activeAccount.Email = activeAccount.UID; activeAccount.EmailConfirmed = false; activeAccount.UnregisterForRecordStateChange(); } activeAccount.RegisterForRecordStateChange(); activeAccount.AccountName = vm.AccountName; activeAccount.FName = vm.FName; activeAccount.LName = vm.LName; activeAccount.UnregisterForRecordStateChange(); if (activeAccount.RecordState != RecordState.None) { toSave.Add(activeAccount); } if (toSend == null) { if (toSave.Count > 0) { _dbi.ManageIdentityModels(toSave.ToArray()); } } else { await Task.WhenAll( Helper.GetFunc(() => { try { if (toSave.Count > 0) { _dbi.ManageIdentityModels(toSave.ToArray()); } } catch (Exception ex) { ex1 = ex; } return(Task.CompletedTask); })(), Helper.GetFunc(() => { try { toSend = _dbi.ManageModel(toSend); } catch (Exception ex) { ex2 = ex; } return(Task.CompletedTask); })()); if (ex1 != null) { throw ex1; } if (ex2 != null) { throw ex2; } string lang = this.GetSelectedLanguage(); using (var mailService = this.GetMailService()) { await Task.WhenAll( Helper.GetFunc(() => { try { toSend.RegisterForRecordStateChange(); toSend.EmailSentStatus = EmailSatus.Sending; toSend.UnregisterForRecordStateChange(); if (toSend.RecordState != RecordState.None) { toSend = _dbi.ManageModel(toSend); } } catch (Exception ex) { ex1 = ex; } return(Task.CompletedTask); })(), Helper.GetFunc(async() => { string link = null; try { link = Url.Action("xxXxx", "confirmemail", null, this.Request.Scheme); link += (link.EndsWith("/") ? "" : "/") + toSend.Value; link = link.Replace("/xxXxx/", "/"); await mailService.SendEmailAsync( $"{LanguageManager.GetLabel("AppTitle", lang)} {LanguageManager.GetLabel("lbl_CnfrmEmailAddr", lang)}", LanguageManager.GetOther("email_ChngEmail", lang) .Replace("{1}", LanguageManager.GetLabel("AppTitle", lang)) .Replace("{2}", link), toEmail: toSend.AddData, fromName: LanguageManager.GetLabel("AppTitle", lang), replyToEmail: "*****@*****.**", replyToName: "NO REPLY"); } catch (Exception ex) { ex2 = new ExceptionID(MessageIdentifier.INVALID_EMAIL, ex, false); } finally { link = null; } })()); if (ex1 != null) { throw ex1; } if (ex2 != null) { _dbi.ManageModel(toSend.SetRecordState(RecordState.Deleted)); throw ex2; } toSend.RegisterForRecordStateChange(); toSend.EmailSentStatus = EmailSatus.Sent; toSend.UnregisterForRecordStateChange(); if (toSend.RecordState != RecordState.None) { toSend = _dbi.ManageModel(toSend); } } } fullName = DbInteractor.GetAccountFullName(activeAccount); claimsIdentity = new ClaimsIdentity(BaseController.APP_ID); claimsIdentity.AddClaims(this.User.Claims.Where(l => l.Type != USER_FULL_NAME)); claimsIdentity.AddClaim(new Claim(USER_FULL_NAME, fullName)); if (GetRememberUser(this.User)) { await this.HttpContext.Authentication.SignInAsync( BaseController.APP_ID, new ClaimsPrincipal(claimsIdentity), new AuthenticationProperties { IsPersistent = true, ExpiresUtc = GetClaimExpDate(this.User) }); } else { await this.HttpContext.Authentication.SignInAsync(BaseController.APP_ID, new ClaimsPrincipal(claimsIdentity)); } return(Json(new { Ok = true, FullName = fullName, profile = this.GetActiveProfileViewModel() })); } catch (Exception ex) { return(InternalServerError(ex)); } finally { activeAccount = null; claimsIdentity = null; toSave?.Clear(); toSave = null; } }