develop #10
43
Serwer/Serwer.Api/Controllers/UserController.cs
Normal file
43
Serwer/Serwer.Api/Controllers/UserController.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Serwer.Infrastructure.DTO;
|
||||
using Serwer.Infrastructure.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Api.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
public UserController(IUserService userService)
|
||||
{
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
[HttpPost("Register")]
|
||||
public async Task<IActionResult> Register(string email, string name, string surname, string login, string password)
|
||||
{
|
||||
await _userService.RegisterAsync(email, name, surname, login, password);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("SignIn")]
|
||||
public async Task<IActionResult> SignIn(string login, string password)
|
||||
{
|
||||
var user = await _userService.SignInAsync(login, password);
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
[HttpGet("Test")]
|
||||
public IActionResult Test()
|
||||
{
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
26
Serwer/Serwer.Api/Program.cs
Normal file
26
Serwer/Serwer.Api/Program.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Serwer.Api
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
@ -1,30 +1,31 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:50287",
|
||||
"sslPort": 44305
|
||||
"applicationUrl": "http://localhost:50760",
|
||||
"sslPort": 44371
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "api/values",
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Serwer": {
|
||||
"Serwer.Api": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": "true",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "api/values",
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
18
Serwer/Serwer.Api/Serwer.Api.csproj
Normal file
18
Serwer/Serwer.Api/Serwer.Api.csproj
Normal file
@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" NoWarn="NU1605" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" NoWarn="NU1605" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Serwer.Core\Serwer.Core.csproj" />
|
||||
<ProjectReference Include="..\Serwer.Infrastructure\Serwer.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
92
Serwer/Serwer.Api/Startup.cs
Normal file
92
Serwer/Serwer.Api/Startup.cs
Normal file
@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Serwer.Core.Repositories;
|
||||
using Serwer.Infrastructure.Mappers;
|
||||
using Serwer.Infrastructure.Repositories;
|
||||
using Serwer.Infrastructure.Services;
|
||||
using Serwer.Infrastructure.Settings;
|
||||
|
||||
namespace Serwer.Api
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllers();
|
||||
services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Serwer.Api", Version = "v1" });
|
||||
});
|
||||
|
||||
var jwtSettings = new JwtSettings()
|
||||
{
|
||||
Issuer = "PoszukiwaczInc",
|
||||
ExpiryMinutes = 120,
|
||||
Key = "Fjjji0Hdsa4$JgrwIO1j678dCelgFymdo"
|
||||
};
|
||||
|
||||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(c =>
|
||||
{
|
||||
c.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Key)),
|
||||
ValidIssuer = jwtSettings.Issuer,
|
||||
ValidateAudience = false,
|
||||
ValidateLifetime = true
|
||||
};
|
||||
});
|
||||
|
||||
services.AddSingleton<IMapper>(AutoMapperConfig.Initialize());
|
||||
services.AddSingleton<IJwtHandler, JwtHandler>(sp => new JwtHandler(jwtSettings));
|
||||
services.AddScoped<IUserRepository, UserRepository>();
|
||||
services.AddScoped<IUserService, UserService>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Serwer.Api v1"));
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
9
Serwer/Serwer.Api/appsettings.Development.json
Normal file
9
Serwer/Serwer.Api/appsettings.Development.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
}
|
||||
}
|
10
Serwer/Serwer.Api/appsettings.json
Normal file
10
Serwer/Serwer.Api/appsettings.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
106
Serwer/Serwer.Core/Domain/User.cs
Normal file
106
Serwer/Serwer.Core/Domain/User.cs
Normal file
@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Core.Domain
|
||||
{
|
||||
public class User
|
||||
{
|
||||
public Guid Id { get; protected set; }
|
||||
public string Email { get; protected set; }
|
||||
public string Name { get; protected set; }
|
||||
public string Surname { get; protected set; }
|
||||
public string Login { get; protected set; }
|
||||
public string Password { get; protected set; }
|
||||
public DateTime CreatedAt { get; protected set; }
|
||||
public DateTime UpdatedAt { get; protected set; }
|
||||
|
||||
protected User() { }
|
||||
public User(string email, string name, string surname, string login, string password)
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
SetEmail(email);
|
||||
SetName(name);
|
||||
SetSurname(surname);
|
||||
SetLogin(login);
|
||||
SetPassword(password);
|
||||
}
|
||||
|
||||
public void SetEmail(string email)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(email))
|
||||
{
|
||||
throw new Exception("Email cannot be empty");
|
||||
}
|
||||
if (Email == email)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Email = email;
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public void SetName(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
throw new Exception("Name cannot be empty");
|
||||
}
|
||||
if (Name == name)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Name = name;
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public void SetSurname(string surname)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(surname))
|
||||
{
|
||||
throw new Exception("Surname cannot be empty");
|
||||
}
|
||||
if (Surname == surname)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Surname = surname;
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public void SetLogin(string login)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(login))
|
||||
{
|
||||
throw new Exception("Login cannot be empty");
|
||||
}
|
||||
if (Login == login)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Login = login;
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public void SetPassword(string password)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(password))
|
||||
{
|
||||
throw new Exception("Password cannot be empty");
|
||||
}
|
||||
if (Password == password)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Password = password;
|
||||
UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
}
|
12
Serwer/Serwer.Core/Repositories/IRepository.cs
Normal file
12
Serwer/Serwer.Core/Repositories/IRepository.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Core.Repositories
|
||||
{
|
||||
public interface IRepository
|
||||
{
|
||||
}
|
||||
}
|
19
Serwer/Serwer.Core/Repositories/IUserRepository.cs
Normal file
19
Serwer/Serwer.Core/Repositories/IUserRepository.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using Serwer.Core.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Core.Repositories
|
||||
{
|
||||
public interface IUserRepository : IRepository
|
||||
{
|
||||
Task<User> GetAsync(Guid id);
|
||||
Task<User> GetAsync(string login);
|
||||
Task<IEnumerable<User>> GetAllAsync();
|
||||
Task AddAsync(User user);
|
||||
Task UpdateAsync(User user);
|
||||
Task RemoveAsync(Guid id);
|
||||
}
|
||||
}
|
7
Serwer/Serwer.Core/Serwer.Core.csproj
Normal file
7
Serwer/Serwer.Core/Serwer.Core.csproj
Normal file
@ -0,0 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
15
Serwer/Serwer.Infrastructure/DTO/JwtDto.cs
Normal file
15
Serwer/Serwer.Infrastructure/DTO/JwtDto.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.DTO
|
||||
{
|
||||
public class JwtDto
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
public string Token { get; set; }
|
||||
public long Expires { get; set; }
|
||||
}
|
||||
}
|
14
Serwer/Serwer.Infrastructure/DTO/SignedUserDto.cs
Normal file
14
Serwer/Serwer.Infrastructure/DTO/SignedUserDto.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.DTO
|
||||
{
|
||||
public class SignedUserDto
|
||||
{
|
||||
public UserDto User { get; set; }
|
||||
public JwtDto Jwt { get; set; }
|
||||
}
|
||||
}
|
18
Serwer/Serwer.Infrastructure/DTO/UserDto.cs
Normal file
18
Serwer/Serwer.Infrastructure/DTO/UserDto.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.DTO
|
||||
{
|
||||
public class UserDto
|
||||
{
|
||||
public string Email { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Surname { get; set; }
|
||||
public string Login { get; set; }
|
||||
public DateTime CreatedAt { get; protected set; }
|
||||
public DateTime UpdatedAt { get; protected set; }
|
||||
}
|
||||
}
|
19
Serwer/Serwer.Infrastructure/Extentions/Extentions.cs
Normal file
19
Serwer/Serwer.Infrastructure/Extentions/Extentions.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Extentions
|
||||
{
|
||||
public static class Extentions
|
||||
{
|
||||
public static long ToTimestamp(this DateTime dateTime)
|
||||
{
|
||||
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
var time = dateTime.Ticks - epoch.Ticks;
|
||||
|
||||
return time / TimeSpan.TicksPerSecond;
|
||||
}
|
||||
}
|
||||
}
|
21
Serwer/Serwer.Infrastructure/Mappers/AutoMapperConfig.cs
Normal file
21
Serwer/Serwer.Infrastructure/Mappers/AutoMapperConfig.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using AutoMapper;
|
||||
using Serwer.Core.Domain;
|
||||
using Serwer.Infrastructure.DTO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Mappers
|
||||
{
|
||||
public static class AutoMapperConfig
|
||||
{
|
||||
public static IMapper Initialize()
|
||||
=> new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<User, UserDto>();
|
||||
})
|
||||
.CreateMapper();
|
||||
}
|
||||
}
|
51
Serwer/Serwer.Infrastructure/Repositories/UserRepository.cs
Normal file
51
Serwer/Serwer.Infrastructure/Repositories/UserRepository.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using Serwer.Core.Domain;
|
||||
using Serwer.Core.Repositories;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Repositories
|
||||
{
|
||||
public class UserRepository : IUserRepository
|
||||
{
|
||||
private static ISet<User> _users = new HashSet<User>();
|
||||
|
||||
public async Task AddAsync(User user)
|
||||
{
|
||||
await Task.FromResult(_users.Add(user));
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<User>> GetAllAsync()
|
||||
{
|
||||
return await Task.FromResult(_users);
|
||||
}
|
||||
|
||||
public async Task<User> GetAsync(Guid id)
|
||||
{
|
||||
return await Task.FromResult(_users.SingleOrDefault(x => x.Id == id));
|
||||
}
|
||||
|
||||
public async Task<User> GetAsync(string login)
|
||||
{
|
||||
return await Task.FromResult(_users.SingleOrDefault(x => x.Login == login));
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(Guid id)
|
||||
{
|
||||
var user = await GetAsync(id);
|
||||
_users.Remove(user);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(User user)
|
||||
{
|
||||
var dbuser = await GetAsync(user.Id);
|
||||
dbuser.SetEmail(user.Email);
|
||||
dbuser.SetLogin(user.Login);
|
||||
dbuser.SetName(user.Name);
|
||||
dbuser.SetSurname(user.Surname);
|
||||
dbuser.SetPassword(user.Password);
|
||||
}
|
||||
}
|
||||
}
|
14
Serwer/Serwer.Infrastructure/Services/IJwtHandler.cs
Normal file
14
Serwer/Serwer.Infrastructure/Services/IJwtHandler.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using Serwer.Infrastructure.DTO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
public interface IJwtHandler
|
||||
{
|
||||
JwtDto CreateToken(Guid userId);
|
||||
}
|
||||
}
|
15
Serwer/Serwer.Infrastructure/Services/IUserService.cs
Normal file
15
Serwer/Serwer.Infrastructure/Services/IUserService.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using Serwer.Infrastructure.DTO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
public interface IUserService
|
||||
{
|
||||
Task RegisterAsync(string email, string name, string surname, string login, string password);
|
||||
Task<SignedUserDto> SignInAsync(string login, string password);
|
||||
}
|
||||
}
|
55
Serwer/Serwer.Infrastructure/Services/JwtHandler.cs
Normal file
55
Serwer/Serwer.Infrastructure/Services/JwtHandler.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Serwer.Infrastructure.DTO;
|
||||
using Serwer.Infrastructure.Extentions;
|
||||
using Serwer.Infrastructure.Settings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
public class JwtHandler: IJwtHandler
|
||||
{
|
||||
private readonly JwtSettings _settings;
|
||||
|
||||
public JwtHandler(JwtSettings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public JwtDto CreateToken(Guid userId)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var claims = new Claim[]
|
||||
{
|
||||
new Claim(JwtRegisteredClaimNames.Sub, userId.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.UniqueName, userId.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Iat, now.ToTimestamp().ToString(), ClaimValueTypes.Integer64)
|
||||
};
|
||||
|
||||
var expires = now.AddMinutes(_settings.ExpiryMinutes);
|
||||
var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.Key)),
|
||||
SecurityAlgorithms.HmacSha256);
|
||||
var jwt = new JwtSecurityToken(
|
||||
issuer: _settings.Issuer,
|
||||
claims: claims,
|
||||
notBefore: now,
|
||||
expires: expires,
|
||||
signingCredentials: signingCredentials
|
||||
);
|
||||
var token = new JwtSecurityTokenHandler().WriteToken(jwt);
|
||||
|
||||
return new JwtDto
|
||||
{
|
||||
UserId = userId,
|
||||
Token = token,
|
||||
Expires = expires.ToTimestamp()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
58
Serwer/Serwer.Infrastructure/Services/UserService.cs
Normal file
58
Serwer/Serwer.Infrastructure/Services/UserService.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using AutoMapper;
|
||||
using Serwer.Core.Domain;
|
||||
using Serwer.Core.Repositories;
|
||||
using Serwer.Infrastructure.DTO;
|
||||
using Serwer.Infrastructure.Mappers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
public class UserService: IUserService
|
||||
{
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IJwtHandler _jwtHandler;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public UserService(IUserRepository userRepository, IJwtHandler jwtHandler, IMapper mapper)
|
||||
{
|
||||
_userRepository = userRepository;
|
||||
_jwtHandler = jwtHandler;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task RegisterAsync(string email, string name, string surname, string login, string password)
|
||||
{
|
||||
if(await _userRepository.GetAsync(login) != null)
|
||||
{
|
||||
throw new Exception($"User with login: {login} already exists.");
|
||||
}
|
||||
var user = new User(email, name, surname, login, password);
|
||||
await _userRepository.AddAsync(user);
|
||||
}
|
||||
|
||||
public async Task<SignedUserDto> SignInAsync(string login, string password)
|
||||
{
|
||||
var user = await _userRepository.GetAsync(login);
|
||||
if(user == null)
|
||||
{
|
||||
throw new Exception("User not found.");
|
||||
}
|
||||
if(user.Password != password)
|
||||
{
|
||||
throw new Exception("Incorrect password.");
|
||||
}
|
||||
|
||||
var jwt = _jwtHandler.CreateToken(user.Id);
|
||||
|
||||
return new SignedUserDto()
|
||||
{
|
||||
User = _mapper.Map<UserDto>(user),
|
||||
Jwt = _mapper.Map<JwtDto>(jwt)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
17
Serwer/Serwer.Infrastructure/Serwer.Infrastructure.csproj
Normal file
17
Serwer/Serwer.Infrastructure/Serwer.Infrastructure.csproj
Normal file
@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="10.1.1" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.8.0" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Serwer.Core\Serwer.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
15
Serwer/Serwer.Infrastructure/Settings/JwtSettings.cs
Normal file
15
Serwer/Serwer.Infrastructure/Settings/JwtSettings.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Settings
|
||||
{
|
||||
public class JwtSettings
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Issuer { get; set; }
|
||||
public int ExpiryMinutes { get; set; }
|
||||
}
|
||||
}
|
@ -1,11 +1,22 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.28307.852
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30717.126
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serwer", "Serwer\Serwer.csproj", "{CCE1E4CC-EC14-46A3-BCC9-2CBD8F2284D6}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serwer.Tests", "Serwer.Tests\Serwer.Tests.csproj", "{4169F6FD-E08D-4329-BF87-A1411A9F1EF4}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serwer.Tests", "Serwer.Tests\Serwer.Tests.csproj", "{4169F6FD-E08D-4329-BF87-A1411A9F1EF4}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serwer.Core", "Serwer.Core\Serwer.Core.csproj", "{115E0BB1-FCC1-4E45-92B0-D3B6B4A3DA82}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serwer.Infrastructure", "Serwer.Infrastructure\Serwer.Infrastructure.csproj", "{523E9EE6-B8D9-4E08-A9A0-50D7F872C79C}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{115E0BB1-FCC1-4E45-92B0-D3B6B4A3DA82} = {115E0BB1-FCC1-4E45-92B0-D3B6B4A3DA82}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serwer.Api", "Serwer.Api\Serwer.Api.csproj", "{335159FF-5AB8-48E5-A04C-778A46058204}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{115E0BB1-FCC1-4E45-92B0-D3B6B4A3DA82} = {115E0BB1-FCC1-4E45-92B0-D3B6B4A3DA82}
|
||||
{523E9EE6-B8D9-4E08-A9A0-50D7F872C79C} = {523E9EE6-B8D9-4E08-A9A0-50D7F872C79C}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -13,14 +24,22 @@ Global
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{CCE1E4CC-EC14-46A3-BCC9-2CBD8F2284D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CCE1E4CC-EC14-46A3-BCC9-2CBD8F2284D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CCE1E4CC-EC14-46A3-BCC9-2CBD8F2284D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CCE1E4CC-EC14-46A3-BCC9-2CBD8F2284D6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4169F6FD-E08D-4329-BF87-A1411A9F1EF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4169F6FD-E08D-4329-BF87-A1411A9F1EF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4169F6FD-E08D-4329-BF87-A1411A9F1EF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4169F6FD-E08D-4329-BF87-A1411A9F1EF4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{115E0BB1-FCC1-4E45-92B0-D3B6B4A3DA82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{115E0BB1-FCC1-4E45-92B0-D3B6B4A3DA82}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{115E0BB1-FCC1-4E45-92B0-D3B6B4A3DA82}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{115E0BB1-FCC1-4E45-92B0-D3B6B4A3DA82}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{523E9EE6-B8D9-4E08-A9A0-50D7F872C79C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{523E9EE6-B8D9-4E08-A9A0-50D7F872C79C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{523E9EE6-B8D9-4E08-A9A0-50D7F872C79C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{523E9EE6-B8D9-4E08-A9A0-50D7F872C79C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{335159FF-5AB8-48E5-A04C-778A46058204}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{335159FF-5AB8-48E5-A04C-778A46058204}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{335159FF-5AB8-48E5-A04C-778A46058204}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{335159FF-5AB8-48E5-A04C-778A46058204}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -1,45 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Serwer.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ValuesController : ControllerBase
|
||||
{
|
||||
// GET api/values
|
||||
[HttpGet]
|
||||
public ActionResult<IEnumerable<string>> Get()
|
||||
{
|
||||
return new string[] { "value1", "value2" };
|
||||
}
|
||||
|
||||
// GET api/values/5
|
||||
[HttpGet("{id}")]
|
||||
public ActionResult<string> Get(int id)
|
||||
{
|
||||
return "value";
|
||||
}
|
||||
|
||||
// POST api/values
|
||||
[HttpPost]
|
||||
public void Post([FromBody] string value)
|
||||
{
|
||||
}
|
||||
|
||||
// PUT api/values/5
|
||||
[HttpPut("{id}")]
|
||||
public void Put(int id, [FromBody] string value)
|
||||
{
|
||||
}
|
||||
|
||||
// DELETE api/values/5
|
||||
[HttpDelete("{id}")]
|
||||
public void Delete(int id)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Serwer
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateWebHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseStartup<Startup>();
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -1,48 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Serwer
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseMvc();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
Loading…
Reference in New Issue
Block a user