Example #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = true;
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = AuthOptions.ISSUER,
                    ValidateAudience         = true,
                    ValidAudience            = AuthOptions.AUDIENCE,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = AuthOptions.GetSymmetricSecurity(),
                    ValidateLifetime         = true,
                };
            });

            services.AddHashService();
            services.AddFileService();
            services.AddFaceDetectionPathSystem();
            services.AddStorageContext();

            services.AddFaceService();

            services.AddDefaultModelsToStorage(Configuration);
        }
Example #2
0
        public async Task <IActionResult> Post([FromBody] LoginModel login)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            logger.LogInformation($"Get identity for {login.Email}.");
            var identity = await GetIdentity(login.Email, login.Password);

            if (identity == null)
            {
                logger.LogInformation("Auth data invalid.");
                ModelState.AddModelError("Password", "Invalid email or password.");
                return(BadRequest(ModelState));
            }

            var timeNow = DateTime.Now;

            var jwt = new JwtSecurityToken(
                issuer: AuthOptions.ISSUER,
                audience: AuthOptions.AUDIENCE,
                notBefore: timeNow,
                claims: identity.Claims,
                expires: timeNow.Add(TimeSpan.FromMinutes(AuthOptions.LIFETIME)),
                signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurity(), SecurityAlgorithms.HmacSha256));
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            var responseModel = new
            {
                access_token = encodedJwt,
                username     = identity.Name
            };

            logger.LogInformation("Return answer.");
            return(Ok(responseModel));
        }