Skip to content

Commit b613162

Browse files
authored
AuthService: .NET 8 (#621)
1 parent d8f8e94 commit b613162

File tree

33 files changed

+207
-150
lines changed

33 files changed

+207
-150
lines changed

HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using HwProj.AuthService.Client;
1+
using System.Text;
2+
using HwProj.AuthService.Client;
23
using HwProj.ContentService.Client;
34
using HwProj.CoursesService.Client;
45
using HwProj.NotificationsService.Client;
56
using HwProj.SolutionsService.Client;
6-
using HwProj.Utils.Auth;
77
using HwProj.Utils.Configuration;
88
using HwProj.APIGateway.API.Filters;
99
using Microsoft.AspNetCore.Builder;
@@ -13,6 +13,7 @@
1313
using Microsoft.Extensions.DependencyInjection;
1414
using Microsoft.IdentityModel.Tokens;
1515
using IStudentsInfo;
16+
using Microsoft.AspNetCore.Authentication.JwtBearer;
1617
using StudentsInfo;
1718

1819
namespace HwProj.APIGateway.API
@@ -35,10 +36,15 @@ public void ConfigureServices(IServiceCollection services)
3536
Configuration["StudentsInfo:Password"],
3637
Configuration["StudentsInfo:LdapHost"], int.Parse(Configuration["StudentsInfo:LdapPort"]),
3738
Configuration["StudentsInfo:SearchBase"]));
38-
const string authenticationProviderKey = "GatewayKey";
3939

40-
services.AddAuthentication()
41-
.AddJwtBearer(authenticationProviderKey, x =>
40+
var appSettings = Configuration.GetSection("Security");
41+
42+
services.AddAuthentication(options =>
43+
{
44+
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
45+
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
46+
})
47+
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, x =>
4248
{
4349
x.RequireHttpsMetadata = false;
4450
x.TokenValidationParameters = new TokenValidationParameters
@@ -47,7 +53,8 @@ public void ConfigureServices(IServiceCollection services)
4753
ValidateIssuer = true,
4854
ValidateAudience = false,
4955
ValidateLifetime = true,
50-
IssuerSigningKey = AuthorizationKey.SecurityKey,
56+
IssuerSigningKey =
57+
new SymmetricSecurityKey(Encoding.ASCII.GetBytes(appSettings["SecurityKey"])),
5158
ValidateIssuerSigningKey = true
5259
};
5360
});

HwProj.APIGateway/HwProj.APIGateway.API/appsettings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
"Notifications": "http://localhost:5006",
66
"Solutions": "http://localhost:5007",
77
"Content": "http://localhost:5008"
8-
},
8+
},
9+
"Security": {
10+
"SecurityKey": "U8_.wpvk93fPWG<f2$Op[vwegmQGF25_fNG2V0ijnm2e0igv24g"
11+
},
912
"StudentsInfo": {
1013
"Login": "",
1114
"Password": "",

HwProj.AuthService/HwProj.AuthService.API/Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
1+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
22
WORKDIR /src
33

44
COPY ["Directory.Build.props", "Directory.Build.props"]
55

66
COPY ["HwProj.AuthService/HwProj.AuthService.API/", "HwProj.AuthService/HwProj.AuthService.API/"]
7-
COPY ["HwProj.Common/HwProj.Utils/", "HwProj.Common/HwProj.Utils/"]
8-
COPY ["HwProj.EventBus/HwProj.EventBus.Client/", "HwProj.EventBus/HwProj.EventBus.Client/"]
7+
COPY ["HwProj.Common/HwProj.Common.Net8/", "HwProj.Common/HwProj.Common.Net8/"]
98
COPY ["HwProj.Common/HwProj.Models/", "HwProj.Common/HwProj.Models/"]
10-
COPY ["HwProj.Common/HwProj.Repositories/", "HwProj.Common/HwProj.Repositories/"]
9+
COPY ["HwProj.EventBus/HwProj.EventBus.Client/", "HwProj.EventBus/HwProj.EventBus.Client/"]
10+
COPY ["HwProj.NotificationsService/HwProj.NotificationService.Events/", "HwProj.NotificationsService/HwProj.NotificationService.Events/"]
1111

1212
WORKDIR "/src/HwProj.AuthService/HwProj.AuthService.API"
1313
RUN dotnet publish "HwProj.AuthService.API.csproj" -c Release -o /app/publish
1414

15-
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS final
15+
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
1616
WORKDIR /app
1717

1818
COPY --from=build /app/publish .
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
66
<DockerComposeProjectPath>..\..\docker-compose.dcproj</DockerComposeProjectPath>
77
<DockerfileContext>..\..</DockerfileContext>
88
<LangVersion>$(CSharpLanguageVersion)</LangVersion>
99
<UserSecretsId>603911e4-ace8-4439-96f8-1705a0dae761</UserSecretsId>
10-
<Nullable>$(NullableReferenceTypes)</Nullable>
10+
<Nullable>disable</Nullable>
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
1514
<PackageReference Include="Microsoft.AspNetCore.App" />
16-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
17-
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
18-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.0" />
19-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.3" />
20-
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.13" />
15+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.20" />
16+
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.20" />
17+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.9" />
2118
<PackageReference Include="Octokit" Version="10.0.0" />
2219
</ItemGroup>
2320

2421
<ItemGroup>
22+
<ProjectReference Include="..\..\HwProj.Common\HwProj.Common.Net8\HwProj.Common.Net8.csproj" />
2523
<ProjectReference Include="..\..\HwProj.Common\HwProj.Models\HwProj.Models.csproj" />
26-
<ProjectReference Include="..\..\HwProj.Common\HwProj.Utils\HwProj.Utils.csproj" />
2724
<ProjectReference Include="..\..\HwProj.EventBus\HwProj.EventBus.Client\HwProj.EventBus.Client.csproj" />
25+
<ProjectReference Include="..\..\HwProj.NotificationsService\HwProj.NotificationService.Events\HwProj.NotificationService.Events.csproj" />
2826
</ItemGroup>
2927

3028
</Project>

HwProj.AuthService/HwProj.AuthService.API/Repositories/ExpertsRepository.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System.Linq;
22
using System.Threading.Tasks;
33
using HwProj.AuthService.API.Models;
4-
using HwProj.Models.AuthService.ViewModels;
54
using HwProj.Repositories;
6-
using Microsoft.AspNetCore.Identity;
75
using Microsoft.EntityFrameworkCore;
86

97
namespace HwProj.AuthService.API.Repositories

HwProj.AuthService/HwProj.AuthService.API/Repositories/IExpertsRepository.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.Threading.Tasks;
22
using HwProj.AuthService.API.Models;
3-
using HwProj.Models.AuthService.DTO;
4-
using HwProj.Models.AuthService.ViewModels;
53
using HwProj.Repositories;
64

75
namespace HwProj.AuthService.API.Repositories

HwProj.AuthService/HwProj.AuthService.API/RoleInitializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Microsoft.AspNetCore.Identity;
22
using System.Threading.Tasks;
3-
using HwProj.AuthService.API.Events;
43
using HwProj.EventBus.Client.Interfaces;
54
using HwProj.Models.Roles;
5+
using HwProj.NotificationService.Events.AuthService;
66
using User = HwProj.AuthService.API.Models.User;
77

88
namespace HwProj.AuthService.API

HwProj.AuthService/HwProj.AuthService.API/Services/AccountService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
using AutoMapper;
99
using HwProj.AuthService.API.Extensions;
1010
using HwProj.Models.Roles;
11-
using HwProj.AuthService.API.Events;
12-
using HwProj.AuthService.API.Repositories;
1311
using HwProj.EventBus.Client.Interfaces;
1412
using HwProj.Models.AuthService.DTO;
1513
using HwProj.Models.AuthService.ViewModels;
1614
using HwProj.Models.Result;
17-
using HwProj.Utils.Authorization;
15+
using HwProj.NotificationService.Events.AuthService;
1816
using Microsoft.EntityFrameworkCore;
1917
using Microsoft.Extensions.Configuration;
2018
using Octokit;

HwProj.AuthService/HwProj.AuthService.API/Services/ExpertsService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using HwProj.Models.Result;
1010
using HwProj.Models.Roles;
1111
using Microsoft.AspNetCore.Identity;
12-
using Microsoft.EntityFrameworkCore.Internal;
1312
using User = HwProj.AuthService.API.Models.User;
1413

1514
namespace HwProj.AuthService.API.Services
@@ -69,7 +68,7 @@ await _expertsRepository.AddAsync(new ExpertData
6968
Id = user.Id,
7069
LecturerId = lecturerId,
7170
IsProfileEdited = false,
72-
Tags = model.Tags.Join(";")
71+
Tags = string.Join(';', model.Tags)
7372
});
7473

7574
return Result.Success();
@@ -172,7 +171,7 @@ public async Task<Result> UpdateExpertTags(string lecturerId, UpdateExpertTagsDT
172171

173172
await _expertsRepository.UpdateAsync(updateExpertTagsDto.ExpertId, data => new ExpertData()
174173
{
175-
Tags = updateExpertTagsDto.Tags.Join(";")
174+
Tags = string.Join(';', updateExpertTagsDto.Tags)
176175
});
177176

178177
return Result.Success();

HwProj.AuthService/HwProj.AuthService.API/Startup.cs

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
using Microsoft.AspNetCore.Builder;
2-
using Microsoft.AspNetCore.Hosting;
32
using Microsoft.Extensions.Configuration;
43
using Microsoft.Extensions.DependencyInjection;
54
using HwProj.AuthService.API.Models;
65
using HwProj.AuthService.API.Repositories;
76
using Microsoft.AspNetCore.Identity;
87
using Microsoft.EntityFrameworkCore;
98
using HwProj.AuthService.API.Services;
9+
using HwProj.Common.Net8;
1010
using HwProj.EventBus.Client;
1111
using HwProj.EventBus.Client.Interfaces;
12-
using Microsoft.IdentityModel.Tokens;
13-
using Microsoft.AspNetCore.Authentication.JwtBearer;
14-
using HwProj.Utils.Configuration;
15-
using HwProj.Models.AuthService.ViewModels;
16-
using HwProj.Models.Roles;
17-
using HwProj.Utils.Auth;
12+
using Microsoft.Extensions.Hosting;
1813
using User = HwProj.AuthService.API.Models.User;
1914

2015
namespace HwProj.AuthService.API
@@ -31,25 +26,6 @@ public Startup(IConfiguration configuration)
3126
public void ConfigureServices(IServiceCollection services)
3227
{
3328
services.ConfigureHwProjServices("AuthService API");
34-
35-
//var appSettingsSection = Configuration.GetSection("AppSettings");
36-
//services.Configure<AppSettings>(appSettingsSection);
37-
38-
services.AddAuthentication(options => { options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; })
39-
.AddJwtBearer(x =>
40-
{
41-
x.RequireHttpsMetadata = false; //TODO: dev env setting
42-
x.TokenValidationParameters = new TokenValidationParameters
43-
{
44-
ValidIssuer = "AuthService",
45-
ValidateIssuer = true,
46-
ValidateAudience = false,
47-
ValidateLifetime = true,
48-
IssuerSigningKey = AuthorizationKey.SecurityKey,
49-
ValidateIssuerSigningKey = true
50-
};
51-
});
52-
5329
services.AddHttpClient();
5430

5531
var connectionString = ConnectionString.GetConnectionString(Configuration);
@@ -79,22 +55,19 @@ public void ConfigureServices(IServiceCollection services)
7955
.AddScoped<IExpertsRepository, ExpertsRepository>();
8056
}
8157

82-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IdentityContext context)
58+
public void Configure(IApplicationBuilder app, IHostEnvironment env, IdentityContext context)
8359
{
8460
app.ConfigureHwProj(env, "AuthService API", context);
8561

86-
using (var scope = app.ApplicationServices.CreateScope())
87-
{
88-
var userManager = scope.ServiceProvider.GetService(typeof(UserManager<User>)) as UserManager<User>;
62+
using var scope = app.ApplicationServices.CreateScope();
63+
var userManager = scope.ServiceProvider.GetService<UserManager<User>>();
8964

90-
var rolesManager =
91-
scope.ServiceProvider.GetService(typeof(RoleManager<IdentityRole>)) as RoleManager<IdentityRole>;
92-
var eventBus = scope.ServiceProvider.GetService<IEventBus>();
65+
var rolesManager = scope.ServiceProvider.GetService<RoleManager<IdentityRole>>();
66+
var eventBus = scope.ServiceProvider.GetService<IEventBus>();
9367

94-
if (env.IsDevelopment())
95-
{
96-
RoleInitializer.InitializeAsync(userManager, rolesManager, eventBus).Wait();
97-
}
68+
if (env.IsDevelopment())
69+
{
70+
RoleInitializer.InitializeAsync(userManager, rolesManager, eventBus).Wait();
9871
}
9972
}
10073
}

0 commit comments

Comments
 (0)