aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Backend/Api/Api/Api.csproj2
-rw-r--r--Backend/Api/Api/Interfaces/IJwtService.cs9
-rw-r--r--Backend/Api/Api/Program.cs24
-rw-r--r--Backend/Api/Api/Services/JwtService.cs35
-rw-r--r--Backend/Api/Api/appsettings.json28
5 files changed, 86 insertions, 12 deletions
diff --git a/Backend/Api/Api/Api.csproj b/Backend/Api/Api/Api.csproj
index eeacb93..5f63f8e 100644
--- a/Backend/Api/Api/Api.csproj
+++ b/Backend/Api/Api/Api.csproj
@@ -7,8 +7,10 @@
</PropertyGroup>
<ItemGroup>
+ <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.10" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
+ <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.24.0" />
</ItemGroup>
<ItemGroup>
diff --git a/Backend/Api/Api/Interfaces/IJwtService.cs b/Backend/Api/Api/Interfaces/IJwtService.cs
new file mode 100644
index 0000000..075ea6c
--- /dev/null
+++ b/Backend/Api/Api/Interfaces/IJwtService.cs
@@ -0,0 +1,9 @@
+using Api.Models;
+
+namespace Api.Interfaces
+{
+ public interface IJwtService
+ {
+ Task<string> GenToken(User user);
+ }
+} \ No newline at end of file
diff --git a/Backend/Api/Api/Program.cs b/Backend/Api/Api/Program.cs
index 7ae8798..2c2a444 100644
--- a/Backend/Api/Api/Program.cs
+++ b/Backend/Api/Api/Program.cs
@@ -1,7 +1,10 @@
+using System.Text;
using Api.Database;
using Api.Interfaces;
using Api.Services;
+using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Options;
+using Microsoft.IdentityModel.Tokens;
using MongoDB.Driver;
var builder = WebApplication.CreateBuilder(args);
@@ -19,6 +22,24 @@ builder.Services.AddSingleton<IMongoClient>(s =>
builder.Services.AddScoped<IUserService, UserService>();
+
+
+
+//Add Authentication
+builder.Services.AddAuthentication(
+ JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => {
+ options.TokenValidationParameters = new TokenValidationParameters
+ {
+ ValidateIssuerSigningKey = true,
+ IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(builder.Configuration.GetSection("AppSettings:JwtToken").Value)),
+ ValidateIssuer = false,
+ ValidateAudience = false
+ };
+
+ });
+
+
+
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
@@ -35,6 +56,9 @@ if (app.Environment.IsDevelopment())
app.UseAuthorization();
+//Add Authentication
+app.UseAuthentication();
+
app.MapControllers();
app.Run();
diff --git a/Backend/Api/Api/Services/JwtService.cs b/Backend/Api/Api/Services/JwtService.cs
new file mode 100644
index 0000000..0700619
--- /dev/null
+++ b/Backend/Api/Api/Services/JwtService.cs
@@ -0,0 +1,35 @@
+using System.Data;
+using System.IdentityModel.Tokens.Jwt;
+using System.Security.Claims;
+using System.Text;
+using System.Xml.Linq;
+using Api.Interfaces;
+using Api.Models;
+using Microsoft.Extensions.Configuration;
+using Microsoft.IdentityModel.Tokens;
+
+namespace Api.Services
+{
+ public class JwtService : IJwtService
+ {
+ private readonly IConfiguration _config;
+ public JwtService(IConfiguration config)
+ {
+ _config = config;
+ }
+
+ public async Task<string> GenToken(User user)
+ {
+ var tokenHandler = new JwtSecurityTokenHandler();
+ var key = Encoding.ASCII.GetBytes(_config.GetSection("AppSettings:JwtToken").Value);
+ var tokenDescriptor = new SecurityTokenDescriptor
+ {
+ Subject = new ClaimsIdentity(new[] { new Claim("id", user._id) }),
+ Expires = DateTime.UtcNow.AddDays(7),
+ SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
+ };
+ var token = tokenHandler.CreateToken(tokenDescriptor);
+ return tokenHandler.WriteToken(token);
+ }
+ }
+}
diff --git a/Backend/Api/Api/appsettings.json b/Backend/Api/Api/appsettings.json
index e02ac8d..7cc504d 100644
--- a/Backend/Api/Api/appsettings.json
+++ b/Backend/Api/Api/appsettings.json
@@ -1,16 +1,20 @@
{
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft.AspNetCore": "Warning"
- }
- },
- "AllowedHosts": "*",
- "DatabaseSettings": {
+ "AppSettings": {
+ "JwtToken": "PjrVqQJ1P2VOkuWLw7NaZUluT4z7bkau"
+ },
+
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
+ }
+ },
+ "AllowedHosts": "*",
+ "DatabaseSettings": {
- "ConnectionString": "mongodb://127.0.0.1:27017/",
- "DatabaseName": "Odyssey",
- "UserCollectionName": "users"
+ "ConnectionString": "mongodb://127.0.0.1:27017/",
+ "DatabaseName": "Odyssey",
+ "UserCollectionName": "users"
- }
+ }
}