-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to .net 6.0 and now performance is more good
- Loading branch information
1 parent
b6400f3
commit efb1676
Showing
40 changed files
with
732 additions
and
846 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,22 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
namespace JWTAPI.Controllers; | ||
|
||
namespace JWTAPI.Controllers | ||
[ApiController] | ||
[Route("api/protected")] | ||
public class ProtectedController : ControllerBase | ||
{ | ||
[ApiController] | ||
public class ProtectedController : Controller | ||
[HttpGet] | ||
[Authorize] | ||
[Route("for-commonusers")] | ||
public IActionResult GetProtectedData() | ||
{ | ||
[HttpGet] | ||
[Authorize] | ||
[Route("/api/protectedforcommonusers")] | ||
public IActionResult GetProtectedData() | ||
{ | ||
return Ok("Hello world from protected controller."); | ||
} | ||
return Ok("Hello world from protected controller."); | ||
} | ||
|
||
[HttpGet] | ||
[Authorize(Roles = "Administrator")] | ||
[Route("/api/protectedforadministrators")] | ||
public IActionResult GetProtectedDataForAdmin() | ||
{ | ||
return Ok("Hello admin!"); | ||
} | ||
[HttpGet] | ||
[Authorize(Roles = "Administrator")] | ||
[Route("for-administrators")] | ||
public IActionResult GetProtectedDataForAdmin() | ||
{ | ||
return Ok("Hello admin!"); | ||
} | ||
} |
20 changes: 8 additions & 12 deletions
20
src/JWTAPI/JWTAPI/Controllers/Resources/RefreshTokenResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace JWTAPI.Controllers.Resources | ||
namespace JWTAPI.Controllers.Resources; | ||
public class RefreshTokenResource | ||
{ | ||
public class RefreshTokenResource | ||
{ | ||
[Required] | ||
public string Token { get; set; } | ||
[Required] | ||
public string Token { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.EmailAddress)] | ||
[StringLength(255)] | ||
public string UserEmail { get; set; } | ||
} | ||
[Required] | ||
[EmailAddress] | ||
[StringLength(255)] | ||
public string UserEmail { get; set; } | ||
} |
16 changes: 6 additions & 10 deletions
16
src/JWTAPI/JWTAPI/Controllers/Resources/RevokeTokenResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace JWTAPI.Controllers.Resources | ||
namespace JWTAPI.Controllers.Resources; | ||
public class RevokeTokenResource | ||
{ | ||
public class RevokeTokenResource | ||
{ | ||
[Required] | ||
public string Token { get; set; } | ||
[Required] | ||
public string Token { get; set; } | ||
|
||
[Required] | ||
public string Email { get; set; } | ||
} | ||
[Required] | ||
public string Email { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
namespace JWTAPI.Controllers.Resources | ||
namespace JWTAPI.Controllers.Resources; | ||
public class AccessTokenResource | ||
{ | ||
public class AccessTokenResource | ||
{ | ||
public string AccessToken { get; set; } | ||
public string RefreshToken { get; set; } | ||
public long Expiration { get; set; } | ||
} | ||
public string AccessToken { get; set; } | ||
public string RefreshToken { get; set; } | ||
public long Expiration { get; set; } | ||
} |
1 change: 0 additions & 1 deletion
1
src/JWTAPI/JWTAPI/Controllers/Resources/UserCredentialsResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
namespace JWTAPI.Controllers.Resources; | ||
|
||
public class UserCredentialsResource | ||
{ | ||
[Required] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace JWTAPI.Controllers.Resources | ||
namespace JWTAPI.Controllers.Resources; | ||
public class UserResource | ||
{ | ||
public class UserResource | ||
{ | ||
public int Id { get; set; } | ||
public string Email { get; set; } | ||
public IEnumerable<string> Roles { get; set; } | ||
} | ||
public int Id { get; set; } | ||
public string Email { get; set; } | ||
public IEnumerable<string> Roles { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,33 @@ | ||
using AutoMapper; | ||
using JWTAPI.Controllers.Resources; | ||
using JWTAPI.Core.Models; | ||
using JWTAPI.Core.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
namespace JWTAPI.Controllers; | ||
|
||
namespace JWTAPI.Controllers | ||
[ApiController] | ||
[Route("/api/users")] | ||
public class UsersController : ControllerBase | ||
{ | ||
[ApiController] | ||
[Route("/api/[controller]")] | ||
public class UsersController : Controller | ||
private readonly IMapper _mapper; | ||
private readonly IUserService _userService; | ||
|
||
public UsersController(IUserService userService, IMapper mapper) | ||
{ | ||
private readonly IMapper _mapper; | ||
private readonly IUserService _userService; | ||
_userService = userService; | ||
_mapper = mapper; | ||
} | ||
|
||
public UsersController(IUserService userService, IMapper mapper) | ||
{ | ||
_userService = userService; | ||
_mapper = mapper; | ||
} | ||
[HttpPost] | ||
public async Task<IActionResult> CreateUserAsync( | ||
[FromBody] UserCredentialsResource userCredentials) | ||
{ | ||
var user = _mapper.Map<UserCredentialsResource, User>(userCredentials); | ||
|
||
var response = await _userService.CreateUserAsync(user, ApplicationRole.Common); | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> CreateUserAsync([FromBody] UserCredentialsResource userCredentials) | ||
if (!response.Success) | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return BadRequest(ModelState); | ||
} | ||
return BadRequest(response.Message); | ||
} | ||
|
||
var user = _mapper.Map<UserCredentialsResource, User>(userCredentials); | ||
|
||
var response = await _userService.CreateUserAsync(user, ApplicationRole.Common); | ||
if(!response.Success) | ||
{ | ||
return BadRequest(response.Message); | ||
} | ||
var userResource = _mapper.Map<User, UserResource>(response.User); | ||
|
||
var userResource = _mapper.Map<User, UserResource>(response.User); | ||
return Ok(userResource); | ||
} | ||
return Ok(userResource); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
namespace JWTAPI.Core.Models | ||
namespace JWTAPI.Core.Models; | ||
public enum ApplicationRole | ||
{ | ||
public enum ApplicationRole | ||
{ | ||
Common = 1, | ||
Administrator = 2 | ||
} | ||
Common = 1, | ||
Administrator = 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
using System.Collections.ObjectModel; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace JWTAPI.Core.Models | ||
namespace JWTAPI.Core.Models; | ||
public class Role | ||
{ | ||
public class Role | ||
{ | ||
public int Id { get; set; } | ||
public int Id { get; set; } | ||
|
||
[Required] | ||
[StringLength(50)] | ||
public string Name { get; set; } | ||
[Required] | ||
[StringLength(50)] | ||
public string Name { get; set; } | ||
|
||
public ICollection<UserRole> UsersRole { get; set; } = new Collection<UserRole>(); | ||
} | ||
public virtual ICollection<UserRole> UsersRole { get; set; } = new Collection<UserRole>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
using System.Collections.ObjectModel; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace JWTAPI.Core.Models | ||
namespace JWTAPI.Core.Models; | ||
public class User | ||
{ | ||
public class User | ||
{ | ||
public int Id { get; set; } | ||
public int Id { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.EmailAddress)] | ||
[StringLength(255)] | ||
public string Email { get; set; } | ||
[Required] | ||
[EmailAddress] | ||
[StringLength(255)] | ||
public string Email { get; set; } | ||
|
||
[Required] | ||
public string Password { get; set; } | ||
[Required] | ||
public string Password { get; set; } | ||
|
||
public ICollection<UserRole> UserRoles { get; set; } = new Collection<UserRole>(); | ||
} | ||
public ICollection<UserRole> UserRoles { get; set; } = new Collection<UserRole>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
namespace JWTAPI.Core.Models; | ||
|
||
namespace JWTAPI.Core.Models | ||
[Table("UserRoles")] | ||
public class UserRole | ||
{ | ||
[Table("UserRoles")] | ||
public class UserRole | ||
{ | ||
public int UserId { get; set; } | ||
public User User { get; set; } | ||
public int UserId { get; set; } | ||
public User User { get; set; } | ||
|
||
public int RoleId { get; set; } | ||
public Role Role { get; set; } | ||
} | ||
public int RoleId { get; set; } | ||
public Role Role { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
namespace JWTAPI.Core.Repositories | ||
namespace JWTAPI.Core.Repositories; | ||
public interface IUnitOfWork | ||
{ | ||
public interface IUnitOfWork | ||
{ | ||
Task CompleteAsync(); | ||
} | ||
Task CompleteAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
using JWTAPI.Core.Models; | ||
|
||
namespace JWTAPI.Core.Repositories | ||
namespace JWTAPI.Core.Repositories; | ||
public interface IUserRepository | ||
{ | ||
public interface IUserRepository | ||
{ | ||
Task AddAsync(User user, ApplicationRole[] userRoles); | ||
Task<User> FindByEmailAsync(string email); | ||
} | ||
Task AddAsync(User user, ApplicationRole[] userRoles); | ||
Task<User> FindByEmailAsync(string email); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
namespace JWTAPI.Core.Security.Hashing | ||
namespace JWTAPI.Core.Security.Hashing; | ||
public interface IPasswordHasher | ||
{ | ||
public interface IPasswordHasher | ||
{ | ||
string HashPassword(string password); | ||
bool PasswordMatches(string providedPassword, string passwordHash); | ||
} | ||
string HashPassword(string password); | ||
bool PasswordMatches(string providedPassword, string passwordHash); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
namespace JWTAPI.Core.Security.Tokens | ||
namespace JWTAPI.Core.Security.Tokens; | ||
public class AccessToken : JsonWebToken | ||
{ | ||
public class AccessToken : JsonWebToken | ||
{ | ||
public RefreshToken RefreshToken { get; private set; } | ||
public RefreshToken RefreshToken { get; private set; } | ||
|
||
public AccessToken(string token, long expiration, RefreshToken refreshToken) : base(token, expiration) | ||
{ | ||
if(refreshToken == null) | ||
throw new ArgumentException("Specify a valid refresh token."); | ||
|
||
RefreshToken = refreshToken; | ||
} | ||
public AccessToken(string token, long expiration, RefreshToken refreshToken) : base(token, expiration) | ||
{ | ||
RefreshToken = refreshToken | ||
?? throw new ArgumentException("Specify a valid refresh token."); | ||
} | ||
} |
Oops, something went wrong.