//[ValidateAntiForgeryToken] public async Task <IActionResult> SignonAggregatorGroup([FromBody] AggregatorGroupRegistModel model) { if (ModelState.IsValid) { if (_accountContext.AggregatorGroups.Any(x => x.AggName == model.AggregatorGroupName)) { IdentityError error = (_userManager.ErrorDescriber as LocalizedIdentityErrorDescriber).TargetAggGroupHasAlready(model.AggregatorGroupName); IdentityResult _result = IdentityResult.Failed(error); return(BadRequest(new { Result = _result })); } AggregatorGroupEF aggregatorGroup = new AggregatorGroupEF(); aggregatorGroup.ID = Guid.NewGuid().ToString(); aggregatorGroup.AggName = model.AggregatorGroupName; aggregatorGroup.Representation = model.Represenation; aggregatorGroup.Address = model.Address; aggregatorGroup.CreateDT = DateTime.Now; aggregatorGroup.PhoneNumber = model.PhoneNumber; await _accountContext.AddAsync(aggregatorGroup); await _accountContext.SaveChangesAsync(); return(Ok(aggregatorGroup.ID)); } return(BadRequest()); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> SignonContractor([FromBody] AggregatorRegistModelBase model) { if (ModelState.IsValid) { var trans = await _accountContext.Database.BeginTransactionAsync(); try { AggregatorGroupEF aggregatorGroup = await _accountContext.AggregatorGroups.FindAsync(model.AggregatorGroupId); if (aggregatorGroup == null) { IdentityError error = (_userManager.ErrorDescriber as LocalizedIdentityErrorDescriber).AggregatorNotFounded(model.AggregatorGroupId); IdentityResult _result = IdentityResult.Failed(error); return(base.BadRequest(new { Result = _result })); } var user = CreateUserAccount(model, RegisterType.Contrator); JObject obj = JObject.FromObject(user); var result = await _userManager.CreateAsync(user, model.Password); //result.Errors if (result.Succeeded) { ContractorUserEF cu = new ContractorUserEF(); cu.AggGroupId = aggregatorGroup.ID; cu.ContractStatus = ContractStatusCodes.Signing; cu.UserId = user.Id; _accountContext.ContractorUsers.Add(cu); RegisterFileRepositaryEF registerModel = null; if (string.IsNullOrEmpty(model.RegisterFilename) == false) { registerModel = RegisterFile(user.Id, model.RegisterFilename, model.RegisterFilebase64); _accountContext.RegisterFileRepositaries.Add(registerModel); } await _userManager.AddToRoleAsync(user, UserRoleTypes.Contractor); await _accountContext.SaveChangesAsync(); if (model.NotifyEmail) { string email_contents = htmlGenerator.GenerateHtml("NotifyEmail.html", new { Name = $"{user.FirstName} {user.LastName}", Company = model.Company, Email = model.Email, Phone = model.PhoneNumber, Address = model.Address, Aggregator = aggregatorGroup.AggName }); string sender = "PEIU 운영팀"; string target = ""; List <string> supervisor_emails = (await _userManager.GetUsersInRoleAsync(UserRoleTypes.Supervisor)).Select(x => x.Email).ToList(); target = "발전사업자"; var agg_result = _accountContext.VwAggregatorusers.Where(x => x.AggGroupId == model.AggregatorGroupId); supervisor_emails.AddRange(agg_result.Select(x => x.Email)); //var aggregator_account_users = await _userManager.GetUsersInRoleAsync(UserRoleTypes.Supervisor); await _emailSender.SendEmailAsync(sender, $"새로운 {target} 가입이 요청되었습니다", email_contents, registerModel, supervisor_emails.ToArray()); logger.LogInformation($"가입 알림 메일 전송: {string.Join(", ", supervisor_emails)}"); } trans.Commit(); return(Ok(new { Result = result })); //_userManager.find //if (user.AuthRoles == (int)AuthRoles.Aggregator || user.AuthRoles == (int)AuthRoles.Business) // await Publisher.PublishMessageAsync(obj.ToString(), cancellationTokenSource.Token); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); //await _emailSender.SendEmailAsync(model.Email, "Confirm your account", // "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); //await _signInManager.SignInAsync(user, isPersistent: false); //_logger.LogInformation(3, "User created a new account with password."); } else { trans.Dispose(); return(BadRequest(new { Result = result })); } } catch (Exception ex) { trans.Dispose(); logger.LogError(ex, ex.Message); throw; } } // If we got this far, something failed, redisplay form return(BadRequest()); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> SignonAggregator([FromBody] AggregatorRegistModelBase model) { if (ModelState.IsValid) { var trans = await _accountContext.Database.BeginTransactionAsync(); try { AggregatorGroupEF aggregatorGroup = _accountContext.AggregatorGroups.FirstOrDefault(x => x.AggName == model.Company); if (aggregatorGroup == null) { aggregatorGroup = new AggregatorGroupEF(); aggregatorGroup.ID = Guid.NewGuid().ToString(); aggregatorGroup.AggName = model.Company; aggregatorGroup.Representation = ""; aggregatorGroup.Address = model.Address; aggregatorGroup.CreateDT = DateTime.Now; aggregatorGroup.PhoneNumber = model.PhoneNumber; await _accountContext.AddAsync(aggregatorGroup); } var user = CreateUserAccount(model, RegisterType.Aggregator); var result = await _userManager.CreateAsync(user, model.Password); //result.Errors if (result.Succeeded) { var role_add_result = await _userManager.AddToRoleAsync(user, UserRoleTypes.Aggregator); //_userManager.AddClaimAsync(user, new Claim()) AggregatorUserEF aggregatorUser = new AggregatorUserEF(); aggregatorUser.AggregatorGroup = aggregatorGroup; aggregatorUser.UserId = user.Id; await _accountContext.AggregatorUsers.AddAsync(aggregatorUser); RegisterFileRepositaryEF registerModel = null; if (string.IsNullOrEmpty(model.RegisterFilename) == false) { registerModel = RegisterFile(user.Id, model.RegisterFilename, model.RegisterFilebase64); _accountContext.RegisterFileRepositaries.Add(registerModel); } await _accountContext.SaveChangesAsync(); if (model.NotifyEmail) { string email_contents = htmlGenerator.GenerateHtml("NotifyEmail.html", new { Name = $"{user.FirstName} {user.LastName}", Company = model.Company, Email = model.Email, Phone = model.PhoneNumber, Address = model.Address, Aggregator = aggregatorGroup.AggName }); string sender = "PEIU 운영팀"; string target = ""; List <string> supervisor_emails = (await _userManager.GetUsersInRoleAsync(UserRoleTypes.Supervisor)).Select(x => x.Email).ToList(); target = "중개거래사업자"; var agg_result = _accountContext.VwAggregatorusers.Where(x => x.AggGroupId == model.AggregatorGroupId); supervisor_emails.AddRange(agg_result.Select(x => x.Email)); //var aggregator_account_users = await _userManager.GetUsersInRoleAsync(UserRoleTypes.Supervisor); await _emailSender.SendEmailAsync(sender, $"새로운 {target} 가입이 요청되었습니다", email_contents, registerModel, supervisor_emails.ToArray()); logger.LogInformation($"가입 알림 메일 전송: {string.Join(", ", supervisor_emails)}"); } trans.Commit(); return(Ok(new { Result = result })); } else { trans.Dispose(); return(BadRequest(new { Result = result })); } } catch (Exception ex) { trans.Dispose(); logger.LogError(ex, ex.Message); } } return(BadRequest()); }