コード例 #1
0
        public async Task <IActionResult> Login([FromBody] LoginInputDto input)
        {
            //TODO: Parse to passwordHash and use as hash
            var user = await GetIdentity(input.Username, input.Password);

            if (user == null)
            {
                return(BadRequest(new { message = "Username or Password is incorrect" }));
            }

            var key   = JwtConfigs.GetSecurityKey();
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var token       = new JwtSecurityToken(claims: user.Claims, signingCredentials: creds, expires: DateTime.UtcNow + TimeSpan.FromHours(24));
            var tokenResult = new JwtSecurityTokenHandler().WriteToken(token);

            return(Ok(new { token = tokenResult }));
        }
コード例 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            string connectionString =
                @"Server=DESKTOP-D0NSBJ1\SQLEXPRESS;Database=LearningCzechDb;Trusted_Connection=True;MultipleActiveResultSets=true";

            services.AddDbContext <LearningDbContext>(options => options.UseSqlServer(connectionString),
                                                      ServiceLifetime.Scoped);

            //services.AddAuthorization(auth =>
            //{
            //    auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
            //        .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            //        .RequireAuthenticatedUser().Build());
            //});

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    IssuerSigningKey = JwtConfigs.GetSecurityKey(),
                    ValidateAudience = false,
                    ValidateIssuer   = false
                };
            });

            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp";
            });

            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IWordGroupService, WordGroupService>();
            services.AddScoped <IWordService, WordService>();
            services.AddScoped <IArticleService, ArticleService>();
            services.AddScoped <IImagesService, ImagesService>();
        }