public ActionResult UploadUser(FormCollection formCollection) { if (Request != null) { CashMeContext _context = new CashMeContext(); HttpPostedFileBase file = Request.Files["UploadedFile"]; if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) { string fileName = file.FileName; string fileContentType = file.ContentType; byte[] fileBytes = new byte[file.ContentLength]; var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength)); using (var package = new ExcelPackage(file.InputStream)) { var currentSheet = package.Workbook.Worksheets; var workSheet = currentSheet.First(); var noOfCol = workSheet.Dimension.End.Column; var noOfRow = workSheet.Dimension.End.Row; for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++) { string email = workSheet.Cells[rowIterator, 1].Value.ToString(); string password = workSheet.Cells[rowIterator, 2].Value.ToString(); string SecurityStamp = workSheet.Cells[rowIterator, 3].Value.ToString(); string IP = workSheet.Cells[rowIterator, 4].Value.ToString(); string WalletCode = workSheet.Cells[rowIterator, 5].Value.ToString(); int Ref = int.Parse(workSheet.Cells[rowIterator, 6].Value.ToString()); string username = Regex.Split(email, "@")[0]; var user = new ApplicationUser(); user.Email = email; user.PasswordHash = password; user.SecurityStamp = SecurityStamp; user.PhoneNumberConfirmed = false; user.TwoFactorEnabled = false; user.LockoutEnabled = true; user.AccessFailedCount = 1; user.UserName = username; var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(_context)); var store = new UserStore <ApplicationUser>(_context); var manager = new UserManager <ApplicationUser>(store); var existUser = userManager.Users.FirstOrDefault(aa => aa.Id == user.Id); if (existUser != null) { manager.Update(user); var ctx = store.Context; ctx.SaveChanges(); var lstRole = new string[] { DefaultData.RoleUser }; var allRole = userManager.GetRoles(existUser.Id).ToArray(); if (allRole != null && allRole.Any()) { using (TransactionScope scope = new TransactionScope()) { userManager.RemoveFromRoles(existUser.Id, allRole); if (lstRole.Length > 0) { var roles = _roleServices.GetAllRoles().Where(aa => lstRole.Contains(aa.Id)); var rolesAdd = roles.Select(aa => aa.Name).ToArray(); userManager.AddToRoles(existUser.Id, rolesAdd); } scope.Complete(); } } } else { manager.Create(user); var ctx = store.Context; userManager.AddToRole(user.Id, DefaultData.RoleUser); ctx.SaveChanges(); } var wallet = _walletServices.GetWalletbyCode(WalletCode); var uInfo = new UserInfo(); uInfo.UserId = user.Id; uInfo.WalletId = wallet.Id; uInfo.Amount = (wallet.Claim * 25) + (Ref * 5); uInfo.IP = IP; uInfo.CreateDate = DateTime.Now; uInfo.LastLoginDate = DateTime.Now; _userInfoServices.InserUserInfo(uInfo); var claim = new Claims(); claim.UserId = user.Id; claim.CountImage = wallet.Claim; _claimsServices.InserClaims(claim); } } } } return(View("Upload")); }