Changeset e294f7d


Ignore:
Timestamp:
03/19/26 20:55:06 (4 months ago)
Author:
kikisrbinoska <srbinoskakristina07@…>
Branches:
main
Children:
b62cefc
Parents:
877c13c
Message:

Added JWT authentication

Files:
9 added
6 edited

Legend:

Unmodified
Added
Removed
  • ChapterX.API/ChapterX.API.csproj

    r877c13c re294f7d  
    88
    99  <ItemGroup>
     10    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
    1011    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
    1112      <PrivateAssets>all</PrivateAssets>
  • ChapterX.API/Program.cs

    r877c13c re294f7d  
    11using ChapterX.Application;
    22using ChapterX.Infrastructure;
     3using Microsoft.AspNetCore.Authentication.JwtBearer;
     4using Microsoft.IdentityModel.Tokens;
    35using System.Reflection;
     6using System.Text;
    47
    58var builder = WebApplication.CreateBuilder(args);
     
    1013{
    1114    options.CustomSchemaIds(type => type.FullName);
     15    options.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
     16    {
     17        Name = "Authorization",
     18        Type = Microsoft.OpenApi.Models.SecuritySchemeType.Http,
     19        Scheme = "Bearer",
     20        BearerFormat = "JWT",
     21        In = Microsoft.OpenApi.Models.ParameterLocation.Header,
     22        Description = "Enter your JWT token"
     23    });
     24    options.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
     25    {
     26        {
     27            new Microsoft.OpenApi.Models.OpenApiSecurityScheme
     28            {
     29                Reference = new Microsoft.OpenApi.Models.OpenApiReference
     30                {
     31                    Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme,
     32                    Id = "Bearer"
     33                }
     34            },
     35            Array.Empty<string>()
     36        }
     37    });
    1238});
    1339
    14 builder.Services.AddAuthentication();
     40builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
     41    .AddJwtBearer(options =>
     42    {
     43        options.TokenValidationParameters = new TokenValidationParameters
     44        {
     45            ValidateIssuer = true,
     46            ValidateAudience = true,
     47            ValidateLifetime = true,
     48            ValidateIssuerSigningKey = true,
     49            ValidIssuer = builder.Configuration["Jwt:Issuer"],
     50            ValidAudience = builder.Configuration["Jwt:Audience"],
     51            IssuerSigningKey = new SymmetricSecurityKey(
     52                Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!))
     53        };
     54    });
    1555builder.Services.AddAuthorization();
    1656
  • ChapterX.API/appsettings.json

    r877c13c re294f7d  
    66    }
    77  },
    8   "AllowedHosts": "*"
     8  "AllowedHosts": "*",
     9  "Jwt": {
     10    "Key": "change-this-to-a-long-secret-key-at-least-32-chars!!",
     11    "Issuer": "ChapterX",
     12    "Audience": "ChapterX"
     13  }
    914}
  • ChapterX.Application/ChapterX.Application.csproj

    r877c13c re294f7d  
    88
    99  <ItemGroup>
     10    <PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
    1011    <PackageReference Include="MediatR" Version="14.1.0" />
    1112    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
  • ChapterX.Infrastructure/ChapterX.Infrastructure.csproj

    r877c13c re294f7d  
    88
    99  <ItemGroup>
     10    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
    1011    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
    1112      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  • ChapterX.Infrastructure/DependencyInjection.cs

    r877c13c re294f7d  
    11using ChapterX.Application.Abstractions;
     2using ChapterX.Application.Auth;
    23using ChapterX.Domain.Repositories;
    34using ChapterX.Infrastructure.Data.DataContext;
    45using ChapterX.Infrastructure.Repositories;
     6using ChapterX.Infrastructure.Services;
    57using Microsoft.EntityFrameworkCore;
    68using Microsoft.Extensions.Configuration;
     
    1719
    1820            services.AddScoped<IApplicationDbContext>(sp => sp.GetRequiredService<ApplicationDbContext>());
     21            services.AddScoped<IJwtTokenService, JwtTokenService>();
    1922
    2023            services.AddScoped<IUserRepository, UserRepository>();
Note: See TracChangeset for help on using the changeset viewer.