Beispiel #1
0
        public async Task <IActionResult> Authenticate([FromBody] PartnerLoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var partner = await partnerService.AuthenticatePartner(model.Name, model.Password);

            if (partner != null)
            {
                // Generate Jwt token
                var tokenHandler    = new JwtSecurityTokenHandler();
                var key             = Encoding.ASCII.GetBytes(appSettings.Secret);
                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject = new ClaimsIdentity(new Claim[]
                    {
                        new Claim(ClaimTypes.NameIdentifier, partner.Id.ToString()),
                        new Claim(ClaimTypes.Name, partner.Name),
                        new Claim(ClaimTypes.Role, "partner")
                    }),
                    Expires            = DateTime.UtcNow.AddDays(7),
                    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                };
                var token = tokenHandler.CreateToken(tokenDescriptor);
                return(Ok(new { token = tokenHandler.WriteToken(token) }));
            }
            else
            {
                return(Unauthorized("Incorrect Email or Password"));
            }
        }
Beispiel #2
0
        public async void AuthenticatePartner()
        {
            var controller = new PartnerController(partnerService, mapper, appSettings);
            var model      = new PartnerLoginViewModel()
            {
                Name     = "ikea",
                Password = "******",
            };
            var response = await controller.Authenticate(model);

            var okResult = response as OkObjectResult;

            Assert.NotNull(okResult);
            Assert.Equal(200, okResult.StatusCode);
            Assert.NotNull(okResult.Value);

            var token = okResult.Value as dynamic;

            Assert.NotNull(token);
        }