コード例 #1
0
        public async Task <IActionResult> CreateToken([FromBody] TokenCreateRequestDto tokenCreateRequestDto)
        {
            if (tokenCreateRequestDto == null)
            {
                return(BadRequest(ErrorDto.FromMessage(ErrorMessages.EmptyDtoReceived)));
            }

            var(username, password) = tokenCreateRequestDto;
            var user = await _userService.GetUser(username, password);

            if (user == null)
            {
                return(BadRequest(ErrorDto.FromMessage(ErrorMessages.InvalidCredentials)));
            }

            var tokens = _tokenGenerator.Generate(user);

            var tokenDto = new TokenDto(user, tokens);

            return(Ok(tokenDto));
        }
コード例 #2
0
        public IActionResult ValidateToken([FromBody] TokenValidationRequestDto tokenValidationRequestDto)
        {
            if (tokenValidationRequestDto == null)
            {
                return(BadRequest(ErrorDto.FromMessage(ErrorMessages.EmptyDtoReceived)));
            }

            var tokenToValidate = tokenValidationRequestDto.AccessToken;

            if (string.IsNullOrWhiteSpace(tokenToValidate))
            {
                return(BadRequest(ErrorDto.FromMessage(ErrorMessages.AccessTokenInvalid)));
            }

            var isValid = _tokenGenerator.IsValid(tokenToValidate);

            if (!isValid)
            {
                return(BadRequest(ErrorDto.FromMessage(ErrorMessages.AccessTokenInvalid)));
            }

            return(NoContent());
        }
コード例 #3
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddCors();

            services.Configure <TokenGenerationConfiguration>(Configuration.GetSection("TokenGeneration"));

            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = context =>
                {
                    var errors = context.ModelState.Values.Where(x => x.Errors.Count > 0)
                                 .SelectMany(x => x.Errors)
                                 .Select(x => x.ErrorMessage);

                    var errorMsg = $"One or multiple validation errors occurred. {string.Join(' ', errors)}";
                    return(new BadRequestObjectResult(ErrorDto.FromMessage(errorMsg)));
                };
            });

            services.AddAutoMapper(cfg =>
            {
                // add AutoMapper profiles here...
            });

            services.AddScoped <ITokenGenerator, TokenGenerator>();
            services.AddScoped <IPasswordHasher, PasswordHasher>();

            services.AddCoreLayer();
            services.AddInfrastructureLayer(Configuration);

            if (Environment.IsDevelopment())
            {
                services.AddSwaggerGen();
            }
        }