Пример #1
0
 private Investor MapToEntity(InvestorVm vmToMap)
 {
     return(new Investor
     {
         InvestorId = vmToMap.InvestorId.ToString(),
         FirstName = vmToMap.FirstName,
         LastName = vmToMap.LastName,
         LoginName = vmToMap.LoginName,
     });
 }
Пример #2
0
        public IActionResult AuthenticateInvestor([FromBody] InvestorVm newInvestor)
        {
            /*  Description:
             *  Upon successful authentication, a JSON Web Token is generated via JwtSecurityTokenHandler();  the generated
             *  token is digitally signed using a secret key stored in appsettings.json. The JWT is returned to the client,
             *  who then must include it in the HTTP Authorization header of any subsequent web api request(s) for authentication.
             */

            Investor registeredInvestor = _investorSvc.Authenticate(newInvestor.LoginName, newInvestor.Password);

            if (registeredInvestor == null)
            {
                return(BadRequest(new { message = "Unable to validate registration; please check login name and/or password." }));
            }

            // Generate jwt.
            JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();

            byte[] key = Encoding.ASCII.GetBytes(_appSettings.Secret);
            SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[] {
                    new Claim(ClaimTypes.Name, registeredInvestor.InvestorId.ToString())
                }),
                Expires            = DateTime.Now.AddDays(1), // modified for testing - DateTime.Now.AddMinutes(20),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };

            SecurityToken generatedToken = tokenHandler.CreateToken(tokenDescriptor);
            string        tokenString    = tokenHandler.WriteToken(generatedToken);

            if (registeredInvestor.InvestorId != string.Empty)
            {
                Log.Information("Login successful for: {0}", registeredInvestor.LoginName);
            }

            // Return investor info for use/storage by UI.
            return(Ok(new
            {
                Id = registeredInvestor.InvestorId,
                Username = registeredInvestor.LoginName,
                registeredInvestor.FirstName,
                registeredInvestor.LastName,
                Token = tokenString,
                registeredInvestor.Role
            }));
        }
Пример #3
0
        private IQueryable <InvestorVm> MapToVm(IQueryable <Investor> investorEntitiesToMap)
        {
            IQueryable <InvestorVm> mappedVms = null;

            foreach (var entity in investorEntitiesToMap)
            {
                var mappedVm = new InvestorVm
                {
                    InvestorId = Guid.Parse(entity.InvestorId),
                    FirstName  = entity.FirstName,
                    LastName   = entity.LastName,
                    LoginName  = entity.LoginName,
                };
                mappedVms.Append(mappedVm);
            }
            return(mappedVms);
        }
Пример #4
0
        public IActionResult RegisterInvestor([FromBody] InvestorVm investorToRegister)
        {
            Investor duplicateInvestor = _investorSvc.GetByLogin(investorToRegister.LoginName.Trim());

            if (duplicateInvestor != null)
            {
                return(BadRequest(new { message = "Duplicate registration." }));
            }


            Investor mappedInvestor = MapToEntity(investorToRegister);

            try
            {
                _investorSvc.Create(mappedInvestor, investorToRegister.Password);
                Log.Information("Registration successful for: {0}", mappedInvestor.FirstName + " " + mappedInvestor.LastName);
                return(Ok(mappedInvestor));
            }
            catch (AppException ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }