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"
                }));
            }
        }
        public async Task <IActionResult> Login(LoginDTO loginDto)
        {
            //var user = _context.User.Include(x => x.Manufacturer).Include(x => x.Roles).Where(x => x.UserName == loginDto.UserName && x.PasswordHash == loginDto.Password).SingleOrDefault();
            var loginres = await _signInManager.PasswordSignInAsync(loginDto.Email, loginDto.Password, true, lockoutOnFailure : false);

            if (loginres.Succeeded)
            {
                //  var user = _context.User.Include(x => x.Manufacturer).Include(x => x.UserRole).Where(x => x.UserName == loginDto.UserName).SingleOrDefault();
                var user = _context.User.Include(u => u.UserRoles).ThenInclude(ur => ur.Role).Where(x => x.UserName == loginDto.Email).SingleOrDefault();
                if (!string.IsNullOrEmpty(loginDto.Token))
                {
                    if (loginDto.Token.Contains("-"))
                    {
                        //Find JD and select candidate by user email and company id
                        var jdinfo = await _context.JobOrder.Where(x => (x.InviteId == loginDto.Token)).FirstOrDefaultAsync();

                        //If new add candidate and select id
                        if (jdinfo != null)
                        {
                            var cand = await _context.Candidate.Where(c => (c.Email == loginDto.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;
                            }

                            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;
                            //}
                            await _context.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        InviteCandidate candid = await _context.InviteCandidate.Where(i => i.Token == loginDto.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);

                            await _context.SaveChangesAsync();
                        }
                    }
                }
                var jwtToken = _accountService.Login(user);
                if (jwtToken == null)
                {
                    return(Ok(new ErrorDto {
                        StatusCode = StatusCodes.Status401Unauthorized, Message = "Invalid User Info Detected"
                    }));
                }
                var result = new Dictionary <string, object>();
                result.Add("authtoken", jwtToken);
                result.Add("userid", user.Id);
                result.Add("fullname", user.FullName);
                result.Add("email", user.Email);
                result.Add("companyid", user.CompanyId);
                result.Add("photo", _helperService.GetUserPhoto(user));
                var roles = "";
                foreach (UserRole ur in user.UserRoles)
                {
                    roles = String.Join(",", ur.Role.Name);
                }
                result.Add("Role", roles);

                //result.Add("User", user);
                //return Ok(token);
                return(Ok(new { StatusCode = StatusCodes.Status200OK, Result = result }));
            }
            else
            {
                return(BadRequest(new ErrorDto {
                    StatusCode = StatusCodes.Status401Unauthorized, Message = "Invalid username or password"
                }));
            }
        }