Skip to content

Commit cd66272

Browse files
committed
Adding it all
1 parent e8eab1e commit cd66272

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1702
-55
lines changed

.dockerignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**/.classpath
2+
**/.dockerignore
3+
**/.env
4+
**/.git
5+
**/.gitignore
6+
**/.project
7+
**/.settings
8+
**/.toolstarget
9+
**/.vs
10+
**/.vscode
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/Dockerfile*
19+
**/node_modules
20+
**/npm-debug.log
21+
**/obj
22+
**/secrets.dev.yaml
23+
**/values.dev.yaml
24+
LICENSE
25+
README.md

API/API.csproj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
6+
<UserSecretsId>3a292c2f-70b7-4312-a396-a8b5ff2cd567</UserSecretsId>
7+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
8+
</PropertyGroup>
9+
10+
11+
<ItemGroup>
12+
<PackageReference Include="AutoMapper" Version="9.0.0" />
13+
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
14+
<PackageReference Include="Microsoft.AspNetCore.App" />
15+
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
16+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.5" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<Folder Include="Properties\" />
21+
</ItemGroup>
22+
23+
</Project>

API/Controllers/UsersController.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.IdentityModel.Tokens.Jwt;
5+
using System.Security.Claims;
6+
using System.Text;
7+
using API.Dtos;
8+
using API.Entities;
9+
using API.Helpers;
10+
using API.Services;
11+
using AutoMapper;
12+
using Microsoft.AspNetCore.Authorization;
13+
using Microsoft.AspNetCore.Mvc;
14+
using Microsoft.IdentityModel.Tokens;
15+
using Microsoft.Extensions.Options;
16+
17+
namespace API.Controllers
18+
{
19+
[Authorize]
20+
[ApiController]
21+
[Route("[controller]")]
22+
public class UsersController : ControllerBase
23+
{
24+
private IUserService _userService;
25+
private IMapper _mapper;
26+
private readonly AppSettings _appSettings;
27+
28+
public UsersController(IUserService userService, IMapper mapper, IOptions<AppSettings> appSettings)
29+
{
30+
_userService = userService;
31+
_mapper = mapper;
32+
_appSettings = appSettings.Value;
33+
}
34+
35+
[AllowAnonymous]
36+
[HttpPost("authenticate")]
37+
public IActionResult Autheticate([FromBody]UserDto userDto)
38+
{
39+
var user = _userService.Authenticate(userDto.UserName, userDto.Password);
40+
41+
if(user == null)
42+
return BadRequest(new { message = "Username or password is incorrext"});
43+
44+
var tokenHandler = new JwtSecurityTokenHandler();
45+
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
46+
var tokenDescriptor = new SecurityTokenDescriptor
47+
{
48+
Subject = new ClaimsIdentity(new Claim[]
49+
{
50+
new Claim(ClaimTypes.Name, user.Id.ToString())
51+
}),
52+
Expires = DateTime.UtcNow.AddDays(7),
53+
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
54+
};
55+
var token = tokenHandler.CreateToken(tokenDescriptor);
56+
var tokenString = tokenHandler.WriteToken(token);
57+
58+
return Ok(new {
59+
Id = user.Id,
60+
UserName = user.UserName,
61+
FirrstName = user.FirstName,
62+
LastName = user.LastName,
63+
Token = tokenString
64+
});
65+
}
66+
67+
[AllowAnonymous]
68+
[HttpPost("register")]
69+
public IActionResult Register([FromBody]UserDto userDto)
70+
{
71+
var user = _mapper.Map<User>(userDto);
72+
try
73+
{
74+
_userService.Create(user, userDto.Password);
75+
return Ok();
76+
}
77+
catch (AppException ex)
78+
{
79+
return BadRequest(new { message = ex.Message});
80+
}
81+
}
82+
83+
[HttpGet]
84+
public IActionResult GetAll()
85+
{
86+
var users = _userService.GetAll();
87+
var userDtos = _mapper.Map<IList<UserDto>>(users);
88+
return Ok(userDtos);
89+
}
90+
91+
[HttpGet("{id}")]
92+
public IActionResult Update(int id, [FromBody]UserDto userDto)
93+
{
94+
var user = _mapper.Map<User>(userDto);
95+
user.Id = id;
96+
97+
try
98+
{
99+
_userService.Update(user, userDto.Password);
100+
return Ok();
101+
}
102+
catch (AppException ex)
103+
{
104+
return BadRequest(new { message = ex.Message });
105+
}
106+
}
107+
108+
[HttpDelete("{id}")]
109+
public IActionResult Delete(int id)
110+
{
111+
_userService.Delete(id);
112+
return Ok();
113+
}
114+
}
115+
}

API/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
2+
WORKDIR /app
3+
EXPOSE 80
4+
EXPOSE 443
5+
6+
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
7+
WORKDIR /src
8+
COPY ["API/API.csproj", "API/"]
9+
RUN dotnet restore "API/API.csproj"
10+
COPY . .
11+
WORKDIR "/src/API"
12+
RUN dotnet build "API.csproj" -c Release -o /app/build
13+
14+
FROM build AS publish
15+
RUN dotnet publish "API.csproj" -c Release -o /app/publish
16+
17+
FROM base AS final
18+
WORKDIR /app
19+
COPY --from=publish /app/publish .
20+
ENTRYPOINT ["dotnet", "API.dll"]

API/Dtos/UserDto.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace API.Dtos
2+
{
3+
public class UserDto
4+
{
5+
public int Id { get; set; }
6+
public string FirstName { get; set; }
7+
public string LastName { get; set; }
8+
public string UserName { get; set; }
9+
public string Password { get; set; }
10+
}
11+
}

API/Entities/User.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace API.Entities
2+
{
3+
public class User
4+
{
5+
public int Id { get; set; }
6+
public string FirstName { get; set; }
7+
public string LastName { get; set; }
8+
public string UserName { get; set; }
9+
public byte[] PassWordHash { get; set; }
10+
public byte[] PasswordSalt { get; set; }
11+
}
12+
}

API/Helpers/AppDbContext.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using API.Entities;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace AuthAPI.Helpers
5+
{
6+
public class AppDbContext : DbContext
7+
{
8+
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
9+
{
10+
11+
}
12+
13+
public DbSet<User> Users { get; set; }
14+
}
15+
}

API/Helpers/AppExeption.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Globalization;
3+
4+
namespace API.Helpers
5+
{
6+
public class AppException : Exception
7+
{
8+
public AppException() : base() {}
9+
public AppException(string message) : base(message){}
10+
public AppException(string message, params object[] args)
11+
: base(String.Format(CultureInfo.CurrentCulture, message, args))
12+
{
13+
14+
}
15+
}
16+
}

API/Helpers/AppSettings.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace API.Helpers
2+
{
3+
public class AppSettings
4+
{
5+
public string Secret { get; set; }
6+
}
7+
}

API/Helpers/AutoMapperProfile.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using AutoMapper;
2+
using API.Dtos;
3+
using API.Entities;
4+
5+
namespace API.Helpers
6+
{
7+
public class AutoMapperProfile : Profile
8+
{
9+
public AutoMapperProfile()
10+
{
11+
CreateMap<User, UserDto>();
12+
CreateMap<UserDto, User>();
13+
}
14+
}
15+
}

API/Program.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using AuthAPI;
7+
using Microsoft.AspNetCore;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.Logging;
11+
12+
namespace API
13+
{
14+
public class Program
15+
{
16+
public static void Main(string[] args)
17+
{
18+
BuildWebHost(args).Run();
19+
}
20+
21+
public static IWebHost BuildWebHost(string[] args) =>
22+
WebHost.CreateDefaultBuilder(args)
23+
.UseStartup<Startup>()
24+
//.UseUrls("http://localhost:4000")
25+
.Build();
26+
}
27+
}

0 commit comments

Comments
 (0)