Exemplo n.º 1
0
        public async Task <ObjectResult> Login([FromBody] LoginDto model)
        {
            var user = await _signInManager.UserManager.Users.FirstOrDefaultAsync(u => u.Email == model.Email);

            var result = await _signInManager.PasswordSignInAsync(user, model.Password, true, false);

            if (!result.Succeeded)
            {
                return(BadRequest(JsonConvert.SerializeObject(new AuthObject {
                    Error = "Incorrect username or password entered."
                })));
            }

            var userRoles = await _userManager.GetRolesAsync(user);

            var jwt = await _jwtTokenService.GenerateJwtToken(model.Email, user, userRoles);

            var returnObj = new AuthObject
            {
                Token    = jwt.ToString(),
                Roles    = userRoles.ToList(),
                UserName = user.UserName
            };

            var json = JsonConvert.SerializeObject(returnObj);

            return(Ok(json));
        }
        public string GetAuthorizeToken(USER user)
        {
            var jwtTokenService = new JwtTokenService();
            var existsUser      = this.CreateService <IGetAccountSvc, string, USER>(_getAccountSvc, user.USER_ID);

            if (existsUser.xIsNotNull())
            {
                if (existsUser.PASSWORD == user.PASSWORD)
                {
                    var token = jwtTokenService.GenerateJwtToken(existsUser.USER_ID);
                    return(token);
                }
            }

            return(string.Empty);
        }
Exemplo n.º 3
0
        public async Task <string> GetToken([FromBody] Account account)
        {
            var     jwtTokenService = new JwtTokenService();
            Account result          = null;

            using var executor = new ServiceExecutorManager <IGetAccountSvc>(_svc);
            await executor.SetRequest(o => o.Request = account)
            .AddFilter(o => o.Request.isNotNull())
            .OnExecutedAsync(async o => {
                result = o.Result;
                return(true);
            });

            var jwtToken = jwtTokenService.GenerateJwtToken(result.Id);

            return(jwtToken);
        }
Exemplo n.º 4
0
        public void GenerateJwtToken_Should_generate_and_return_token()
        {
            // Arrange
            jwtTokenSerivce = new JwtTokenService();

            var request = new JwtTokenRequest()
            {
                Email        = "*****@*****.**",
                FirstName    = "first",
                LastName     = "last",
                JsonWebToken = new JsonWebToken
                {
                    Domain    = "",
                    SecretKey = "thisIsSecrectKeyValue"
                }
            };

            // Act
            var response = jwtTokenSerivce.GenerateJwtToken(request);

            // Assert
            Assert.IsNotNull(response);
            Assert.IsTrue(response.Length > 50);
        }