コード例 #1
0
        public async Task InvokeAsync(HttpContext context)
        {
            Console.WriteLine("called");
            try
            {
                var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
                var key   = Encoding.UTF8.GetBytes(_configuration.GetSection("Auth:Secret").Value);

                var tokenHandler = new JwtSecurityTokenHandler();
                tokenHandler.ValidateToken(token, new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false,
                    ClockSkew = TimeSpan.Zero
                }, out var validatedToken);

                var jwtToken = validatedToken as JwtSecurityToken;
                var claim    = jwtToken.Claims.FirstOrDefault(x => x.Type == "id");
                if (claim != null)
                {
                    int.TryParse(claim.Value.ToString(), out var id);
                    context.Items["User"] = _dataService.GetUser(id);
                }
            }
            catch { }

            await _next(context);
        }
コード例 #2
0
        public IActionResult Register(User user)
        {
            if (_dataService.GetUser(user.UserName) != null)
            {
                return(BadRequest());
            }

            int.TryParse(_configuration.GetSection("Auth:PasswordSize").Value, out int pwdSize);

            if (pwdSize == 0)
            {
                throw new ArgumentException("No password size");
            }

            var salt = PasswordService.GenerateSalt(pwdSize);
            var pwd  = PasswordService.HashPassword(user.Password, salt, pwdSize);

            _dataService.CreateUser(user.UserName, user.Name, user.Email, pwd, salt);

            return(CreatedAtRoute(null, new { user.UserName }));
        }
コード例 #3
0
        public IActionResult GetUser(string userName)
        {
            var user = (User)ds.GetUser(userName);

            Console.WriteLine(user);
            if (user == null)
            {
                return(NotFound());
            }

            var dto = _mapper.Map <UserDto>(user);

            return(Ok(dto));
        }