Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions ToDoList.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,63 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList", "ToDoList\TodoList.csproj", "{D58E669D-A851-4711-BDC8-9F6402B6A8A3}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Presentation", "Presentation", "{7EFD5263-C6E2-40F1-874F-70269A72D0AD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TodoList", "ToDoList\TodoList.csproj", "{22EA097C-F848-4FAE-9F5A-508584F88C7A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BLL", "BLL", "{D1C32702-34EF-4606-BBA4-524C0182DC2A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DAL", "DAL", "{5E77DB42-17FE-4461-BD36-9440429871E6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList.DataAccess", "TodoList.DataAccess\TodoList.DataAccess.csproj", "{738A90B6-1482-4E22-B6C8-FC41CE5EF7F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList.BusinessLogic", "TodoList.BusinessLogic\TodoList.BusinessLogic.csproj", "{7F6D7D39-18FC-4E40-A184-371348773D54}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DTOs", "DTOs", "{06FCC980-F331-4ACF-8DB0-D34E6FBF33EE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList.DTOs", "TodoList.DTOs\TodoList.DTOs.csproj", "{8EFA0D3D-5568-49A8-B432-88389598A191}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{B065DE16-FDE9-4C1E-AA14-70EEECECD660}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList.Core", "TodoList.Core\TodoList.Core.csproj", "{DC68F972-5FD5-4468-A5C0-00E50E30DB26}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D58E669D-A851-4711-BDC8-9F6402B6A8A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D58E669D-A851-4711-BDC8-9F6402B6A8A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D58E669D-A851-4711-BDC8-9F6402B6A8A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D58E669D-A851-4711-BDC8-9F6402B6A8A3}.Release|Any CPU.Build.0 = Release|Any CPU
{22EA097C-F848-4FAE-9F5A-508584F88C7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{22EA097C-F848-4FAE-9F5A-508584F88C7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{22EA097C-F848-4FAE-9F5A-508584F88C7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{22EA097C-F848-4FAE-9F5A-508584F88C7A}.Release|Any CPU.Build.0 = Release|Any CPU
{738A90B6-1482-4E22-B6C8-FC41CE5EF7F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{738A90B6-1482-4E22-B6C8-FC41CE5EF7F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{738A90B6-1482-4E22-B6C8-FC41CE5EF7F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{738A90B6-1482-4E22-B6C8-FC41CE5EF7F1}.Release|Any CPU.Build.0 = Release|Any CPU
{7F6D7D39-18FC-4E40-A184-371348773D54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7F6D7D39-18FC-4E40-A184-371348773D54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F6D7D39-18FC-4E40-A184-371348773D54}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F6D7D39-18FC-4E40-A184-371348773D54}.Release|Any CPU.Build.0 = Release|Any CPU
{8EFA0D3D-5568-49A8-B432-88389598A191}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8EFA0D3D-5568-49A8-B432-88389598A191}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EFA0D3D-5568-49A8-B432-88389598A191}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EFA0D3D-5568-49A8-B432-88389598A191}.Release|Any CPU.Build.0 = Release|Any CPU
{DC68F972-5FD5-4468-A5C0-00E50E30DB26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DC68F972-5FD5-4468-A5C0-00E50E30DB26}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC68F972-5FD5-4468-A5C0-00E50E30DB26}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC68F972-5FD5-4468-A5C0-00E50E30DB26}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{22EA097C-F848-4FAE-9F5A-508584F88C7A} = {7EFD5263-C6E2-40F1-874F-70269A72D0AD}
{738A90B6-1482-4E22-B6C8-FC41CE5EF7F1} = {5E77DB42-17FE-4461-BD36-9440429871E6}
{7F6D7D39-18FC-4E40-A184-371348773D54} = {D1C32702-34EF-4606-BBA4-524C0182DC2A}
{8EFA0D3D-5568-49A8-B432-88389598A191} = {06FCC980-F331-4ACF-8DB0-D34E6FBF33EE}
{DC68F972-5FD5-4468-A5C0-00E50E30DB26} = {B065DE16-FDE9-4C1E-AA14-70EEECECD660}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1649D7CC-B602-4854-AF45-4A4E73B3E09D}
EndGlobalSection
Expand Down
7 changes: 5 additions & 2 deletions ToDoList/CurrentUser.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Security.Claims;
using TodoList.Core.Common.Contracts;
namespace TodoList;

public class CurrentUser(IHttpContextAccessor httpContextAccessor)
public class CurrentUser(IHttpContextAccessor httpContextAccessor) : ICurrentUser
{
public string Id => httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier) ?? string.Empty;
private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;

public string Id => _httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier) ?? string.Empty;
}
4 changes: 2 additions & 2 deletions ToDoList/Endpoints.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TodoList.BusinessLogic.Contracts;
using TodoList.Core.Constants;
using TodoList.DTOs.Todo;
using TodoList.DTOs.User;
using TodoList.Services.Contracts;
using ToDoList.Services.Contracts;

namespace TodoList;

Expand Down
109 changes: 109 additions & 0 deletions ToDoList/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Text;
using TodoList.BusinessLogic.Services;
using TodoList.Core.Common.Contracts;
using TodoList.Core.Common.IOptions;

namespace TodoList.Extensions
{
public static class ServiceExtensions
{
public static IServiceCollection AddPresentation(this IServiceCollection services, IConfiguration configuration)
{
services.ConfigureIOptions(configuration);

services.ConfigureJwtAuthentication(
services.BuildServiceProvider().GetRequiredService<IOptions<JwtSettings>>()
);

services.ConfigureSwaggerGen();

services.ConfigureDI();

return services;
}

private static IServiceCollection ConfigureIOptions(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<JwtSettings>(configuration.GetSection("JwtSettings"));

return services;
}

private static IServiceCollection ConfigureJwtAuthentication(this IServiceCollection services, IOptions<JwtSettings> jwtSettings)
{
JwtSettings _jwtSettings = jwtSettings.Value;

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = _jwtSettings.Issuer,
ValidAudience = _jwtSettings.Audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SecretKey))
};
});

return services;
}

private static IServiceCollection ConfigureSwaggerGen(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Swagger",
Contact = new OpenApiContact
{
Name = "Behzad Dara",
Email = "Behzad.Dara.99@gmail.com",
Url = new Uri("https://www.linkedin.com/in/behzaddara/")
}
});

c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please insert JWT into field",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "bearer"
});

c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});

return services;
}

private static IServiceCollection ConfigureDI(this IServiceCollection services)
{
services.AddSingleton<ICurrentUser, CurrentUser>();

return services;
}
}
}
68 changes: 7 additions & 61 deletions ToDoList/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,24 @@
using Microsoft.OpenApi.Models;
using System.Text;
using TodoList;
using TodoList.Services;
using TodoList.Services.Contracts;
using ToDoList.Services.Contracts;
using TodoList.BusinessLogic.Extensions;
using TodoList.Core.Common.Contracts;
using TodoList.DataAccess.ServiceExtensions;
using TodoList.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "test-issuer",
ValidAudience = "test-audience",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Constants.TOKEN_KEY))
};
});
builder.Services.AddPresentation(builder.Configuration);

builder.Services.AddAuthorization();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Swagger",
Contact = new OpenApiContact
{
Name = "Behzad Dara",
Email = "Behzad.Dara.99@gmail.com",
Url = new Uri("https://www.linkedin.com/in/behzaddara/")
}
});

c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please insert JWT into field",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "bearer"
});

c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
builder.Services.AddBusiness();

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<TodoListDBContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddDataAccess(builder.Configuration);

builder.Services.AddHttpContextAccessor();
builder.Services.AddSingleton(typeof(CurrentUser));

builder.Services.AddScoped(typeof(TokenService));
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<ITodoService, TodoService>();

var app = builder.Build();

Expand Down
94 changes: 0 additions & 94 deletions ToDoList/Services/TodoService.cs

This file was deleted.

Loading