public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var availabilityId = this.Data.Availabilities .All() .Where(a => a.AvailabilityName == "Unavailable") .FirstOrDefault() .Id; var user = new SupportAgent { UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber, AvailabilityId = availabilityId, AvailableFrom = DateTime.Now }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); //// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 //// Send an email with this link //// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); var userStore = new UserStore <SupportAgent>(new SMSContext()); var userManager = new UserManager <SupportAgent>(userStore); userManager.AddToRole(user.Id, "Support Agent"); var availability = new SupportAgentsAvailability { SupportAgentId = user.Id, AvailabilityId = user.AvailabilityId, StartTime = user.AvailableFrom }; this.Data.SupportAgentsAvailabilities.Add(availability); user.SupportAgentAvailabilities.Add(availability); this.Data.SaveChanges(); return(RedirectToAction("Index", "Home", new { area = "" })); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state) { if (requestContext.HttpContext.User.Identity.IsAuthenticated) { var username = requestContext.HttpContext.User.Identity.Name; var user = this.Data.SupportAgents.All().FirstOrDefault(u => u.UserName == username); this.userProfile = user; } return(base.BeginExecute(requestContext, callback, state)); }
public IActionResult AddEdit(Guid org, Guid id) { if (id == Guid.Empty) { SupportAgent supportAgent = new SupportAgent(); supportAgent.organizationId = org; return(View(supportAgent)); } else { return(View(_context.SupportAgent.Where(x => x.supportAgentId.Equals(id)).FirstOrDefault())); } }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Manage")); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new SupportAgent { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
private void CreateAdminUser(SMSContext context, string adminEmail, string adminUsername, string adminPhoneNumber, string adminPassword, string adminRole, int adminAvailabilityId) { var adminUser = new SupportAgent { UserName = adminUsername, Email = adminEmail, PhoneNumber = adminPhoneNumber, AvailabilityId = adminAvailabilityId }; var userStore = new UserStore <SupportAgent>(context); var userManager = new UserManager <SupportAgent>(userStore); userManager.PasswordValidator = new PasswordValidator { RequiredLength = 1, RequireNonLetterOrDigit = false, RequireDigit = false, RequireLowercase = false, RequireUppercase = false }; var userCreateResult = userManager.Create(adminUser, adminPassword); if (!userCreateResult.Succeeded) { throw new Exception(string.Join("; ", userCreateResult.Errors)); } var addAdminRoleResult = userManager.AddToRole(adminUser.Id, adminRole); if (!addAdminRoleResult.Succeeded) { throw new Exception(string.Join("; ", addAdminRoleResult.Errors)); } context.SaveChanges(); }
public void AddAgentToTeam(SupportAgent agent) { team.AddOrUpdate(agent.AgentId, agent, (key, oldValue) => agent); }
public async Task <IActionResult> PostSupportAgent([FromBody] SupportAgent supportAgent) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (supportAgent.supportAgentId == Guid.Empty) { try { var user = new ApplicationUser { UserName = supportAgent.Email, Email = supportAgent.Email, FullName = supportAgent.supportAgentName }; user.IsSupportAgent = true; var randomPassword = new Random().Next(0, 999999); var result = await _userManager.CreateAsync(user, randomPassword.ToString()); if (result.Succeeded) { var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); await _emailSender.SendEmailAsync(supportAgent.Email, "Confirm your email and Registration", $"Your email has been registered. With username:'******' and temporary password:'******' .Please confirm your account by clicking this link: <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>link</a>"); supportAgent.applicationUser = user; Organization org = _context.Organization.Where(x => x.organizationId.Equals(supportAgent.organizationId)).FirstOrDefault(); supportAgent.organization = org; supportAgent.supportAgentId = Guid.NewGuid(); _context.SupportAgent.Add(supportAgent); await _context.SaveChangesAsync(); return(Json(new { success = true, message = "Add new data success." })); } else { return(Json(new { success = false, message = "UserManager CreateAsync Fail." })); } } catch (Exception ex) { return(Json(new { success = false, message = ex.Message })); } } else { try { _context.Update(supportAgent); await _context.SaveChangesAsync(); return(Json(new { success = true, message = "Edit data success." })); } catch (Exception ex) { return(Json(new { success = false, message = ex.Message })); } } }
protected BaseController(ISMSData data, SupportAgent userProfile) : this(data) { this.userProfile = userProfile; }