public async Task <IActionResult> Register([FromBody] RegisterDTO usr) { if (!_context.User.Any(u => u.Email == usr.Email)) { var uguid = Guid.NewGuid(); var user = new User { UserName = usr.Email.ToLower(), Email = usr.Email, FullName = usr.Name, PasswordHash = usr.Password, UserGuid = uguid, }; if (usr.Type == 2) { var comp = new Company(); string emaildom = _helperService.GetDomain(usr.Email); if (!_context.User.Any(x => x.Email.Contains(emaildom))) { var addr = new Address(); addr.Phone = usr.Phone; comp.Name = usr.Company; comp.UID = Guid.NewGuid(); comp.Address = addr; user.Company = comp; comp.Created = DateTime.Now; comp.Updated = DateTime.Now; } else { return(BadRequest(new ErrorDto { StatusCode = StatusCodes.Status400BadRequest, Message = "Company already exists, contact administrator" })); } } var createUser = await _UserManager.CreateAsync(user, usr.Password); if (createUser.Succeeded) { if (usr.Type == 2) { await _UserManager.AddToRoleAsync(user, "ADMIN"); } else { await _UserManager.AddToRoleAsync(user, "CANDIDATE"); } //Gen Avatar _helperService.GenAvatar(uguid.ToString(), user.FullName); if (!string.IsNullOrEmpty(usr.Token) && usr.Type == 4) { if (usr.Token.Contains("-")) { //Find JD and select candidate by user email and company id var jdinfo = await _context.JobOrder.Where(x => (x.InviteId == usr.Token)).FirstOrDefaultAsync(); //If new add candidate and select id if (jdinfo != null) { var cand = await _context.Candidate.Where(c => (c.Email == usr.Email && c.CompanyId == jdinfo.CompanyId)).FirstOrDefaultAsync(); _context.Entry <JobOrder>(jdinfo).State = EntityState.Detached; Candidate cnew = new Candidate(); if (cand == null) { cnew.Name = user.FullName; cnew.UserId = user.Id; cnew.Email = user.Email; cnew.CompanyId = jdinfo.CompanyId; cnew.Created = DateTime.Now; await _context.Candidate.AddAsync(cnew); } else { _context.Entry <Candidate>(cand).State = EntityState.Detached; cnew.Id = cand.Id; _context.Candidate.Attach(cnew); cnew.Name = user.FullName; cnew.UserId = user.Id; } await _context.SaveChangesAsync(); var jdcand = await _context.JobCandidate.Where(x => (x.jobOrderId == jdinfo.Id && x.CandidateId == cnew.Id)).FirstOrDefaultAsync(); JobCandidate jc = new JobCandidate(); if (jdcand == null) { jc.CandidateId = cnew.Id; jc.jobOrderId = jdinfo.Id; jc.AddedOn = DateTime.Now; jc.AddedById = cnew.Id; await _context.JobCandidate.AddAsync(jc); } else { _context.Entry <JobCandidate>(jdcand).State = EntityState.Detached; _context.JobCandidate.Attach(jc); jc.CandidateId = cnew.Id; jc.jobOrderId = jdinfo.Id; jc.AddedOn = DateTime.Now; jc.AddedById = cnew.Id; } await _context.SaveChangesAsync(); } } else { InviteCandidate candid = await _context.InviteCandidate.Where(i => i.Token == usr.Token).SingleOrDefaultAsync(); if (candid == null) { } else { Candidate cnew = new Candidate(); cnew.Id = candid.CandidateId; _context.Candidate.Attach(cnew); cnew.UserId = user.Id; _context.InviteCandidate.Remove(candid); } } string token = await _UserManager.GenerateEmailConfirmationTokenAsync(user); var result = await _UserManager.ConfirmEmailAsync(user, token); if (result.Succeeded) { await _context.SaveChangesAsync(); } } else { string token = await _UserManager.GenerateEmailConfirmationTokenAsync(user); string keyval = Guid.NewGuid().ToString("N"); UserActivate usract = new UserActivate { GuiId = keyval, Token = token, UserId = user.Id }; _context.UserActivate.Add(usract); await _context.SaveChangesAsync(); await SendActivation(user, keyval); } return(Ok(new { StatusCode = StatusCodes.Status200OK, Message = "Account created successfully, Please check mail and activate account" })); } else { return(BadRequest(new ErrorDto { StatusCode = StatusCodes.Status400BadRequest, Message = "Password policy not met" })); } } else { return(BadRequest(new ErrorDto { StatusCode = StatusCodes.Status400BadRequest, Message = "Email id already exists" })); } }