Skip to content

Commit

Permalink
Make migrations for user and jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
astijusar committed Nov 5, 2023
1 parent b18b6a9 commit a03477b
Show file tree
Hide file tree
Showing 17 changed files with 1,157 additions and 190 deletions.
2 changes: 2 additions & 0 deletions src/API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.24" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.24" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.22" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
Expand Down
30 changes: 30 additions & 0 deletions src/API/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System.Reflection;
using System.Text;
using API.Filters;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json.Converters;
using Repository;
using Repository.Interfaces;
Expand Down Expand Up @@ -50,5 +54,31 @@ public static void ConfigureSwagger(this IServiceCollection services)
s.IncludeXmlComments(xmlPath);
});
}

public static void ConfigureIdentity(this IServiceCollection services)
{
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<ApplicationContext>()
.AddDefaultTokenProviders();
}

public static void ConfigureAuthenticationAndAuthorization(this IServiceCollection services, IConfiguration configuration)
{
services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(opt =>
{
opt.TokenValidationParameters.ValidAudience = configuration["Jwt:ValidAudience"];
opt.TokenValidationParameters.ValidIssuer = configuration["Jwt:ValidIssuer"];
opt.TokenValidationParameters.IssuerSigningKey =
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:Secret"]));

Check warning on line 78 in src/API/Extensions/ServiceExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 's' in 'byte[] Encoding.GetBytes(string s)'.

Check warning on line 78 in src/API/Extensions/ServiceExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 's' in 'byte[] Encoding.GetBytes(string s)'.
});

services.AddAuthorization();
}
}
}
181 changes: 0 additions & 181 deletions src/API/Migrations/ApplicationContextModelSnapshot.cs

This file was deleted.

6 changes: 6 additions & 0 deletions src/API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.IdentityModel.Tokens.Jwt;
using API.Extensions;
using Microsoft.AspNetCore.Mvc;
using Repository.Models.Mapping;

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

var builder = WebApplication.CreateBuilder(args);

builder.Services.Configure<ApiBehaviorOptions>(opt =>
Expand All @@ -18,6 +21,8 @@
builder.Services.ConfigureControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.ConfigureSwagger();
builder.Services.ConfigureIdentity();
builder.Services.ConfigureAuthenticationAndAuthorization(builder.Configuration);

var app = builder.Build();

Expand All @@ -29,6 +34,7 @@

app.UseHttpsRedirection();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
Expand Down
6 changes: 5 additions & 1 deletion src/API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Jwt": {
"ValidIssuer": "WorkoutTrackerAPI",
"ValidAudience": "WorkoutTrackerWebUI"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#nullable disable

namespace API.Migrations
namespace Repository.Migrations
{
/// <inheritdoc />
public partial class InitialMigration : Migration
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#nullable disable

namespace API.Migrations
namespace Repository.Migrations
{
/// <inheritdoc />
public partial class MakeNoteNullable : Migration
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#nullable disable

namespace API.Migrations
namespace Repository.Migrations
{
/// <inheritdoc />
public partial class MakeExerciseInstructionsNullable : Migration
Expand Down
Loading

0 comments on commit a03477b

Please sign in to comment.