-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c47443
commit 6aee83c
Showing
25 changed files
with
415 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
// Use IntelliSense to find out which attributes exist for C# debugging | ||
// Use hover for the description of the existing attributes | ||
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md. | ||
"name": ".NET Core Launch (web)", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
// If you have changed target frameworks, make sure to update the program path. | ||
"program": "${workspaceFolder}/API/bin/Debug/net7.0/API.dll", | ||
"args": [], | ||
"cwd": "${workspaceFolder}/API", | ||
"stopAtEntry": false, | ||
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser | ||
"serverReadyAction": { | ||
"action": "openExternally", | ||
"pattern": "\\bNow listening on:\\s+(https?://\\S+)" | ||
}, | ||
"env": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"sourceFileMap": { | ||
"/Views": "${workspaceFolder}/Views" | ||
} | ||
}, | ||
{ | ||
"name": ".NET Core Attach", | ||
"type": "coreclr", | ||
"request": "attach" | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"build", | ||
"${workspaceFolder}/DatingApp.sln", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "publish", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"publish", | ||
"${workspaceFolder}/DatingApp.sln", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "watch", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"watch", | ||
"run", | ||
"--project", | ||
"${workspaceFolder}/DatingApp.sln" | ||
], | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
} |
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
76 changes: 76 additions & 0 deletions
76
Angular/CSharp_Angular/DatingApp/API/Controllers/AccountController.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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using API.Data; | ||
using API.DTOs; | ||
using API.Entities; | ||
using API.Interfaces; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace API.Controllers; | ||
|
||
public class AccountController : BaseApiController | ||
{ | ||
private readonly DataContext _context; | ||
private readonly ITokenServices _tokenServices; | ||
|
||
public AccountController(DataContext context, ITokenServices tokenServices) | ||
{ | ||
_context = context; | ||
_tokenServices = tokenServices; | ||
} | ||
|
||
[HttpPost("register")] // POST: api/account/register?username=dave&password=pwd | ||
public async Task<ActionResult<UserDto>> Register(RegisterDto registerDto) | ||
{ | ||
if (await UserExists(registerDto.UserName)) return BadRequest("User is taken"); | ||
|
||
using var hmac = new HMACSHA512(); //Hash-based message authentication code | ||
|
||
var user = new AppUser | ||
{ | ||
UserName = registerDto.UserName.ToLower(), | ||
PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDto.Password)), | ||
PasswordSalt = hmac.Key | ||
}; | ||
|
||
_context.Users.Add(user); | ||
await _context.SaveChangesAsync(); | ||
|
||
return new UserDto | ||
{ | ||
UserName = user.UserName, | ||
Token = _tokenServices.CreateToken(user) | ||
}; | ||
} | ||
|
||
[HttpPost("login")] | ||
public async Task<ActionResult<UserDto>> Login(LoginDto loginDto) | ||
{ | ||
var user = await _context.Users.SingleOrDefaultAsync(x => | ||
x.UserName == loginDto.UserName | ||
); | ||
|
||
if (user == null) return Unauthorized("Invalid username"); | ||
|
||
using var hmac = new HMACSHA512(user.PasswordSalt); | ||
|
||
var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(loginDto.Password)); | ||
|
||
for (int i = 0; i < computedHash.Length; i++) | ||
{ | ||
if (computedHash[i] != user.PasswordHash[i]) return Unauthorized("Invalid password!"); | ||
} | ||
|
||
return new UserDto | ||
{ | ||
UserName = user.UserName, | ||
Token = _tokenServices.CreateToken(user) | ||
}; | ||
} | ||
|
||
private async Task<bool> UserExists(string userName) | ||
{ | ||
return await _context.Users.AnyAsync(x => x.UserName == userName.ToLower()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Angular/CSharp_Angular/DatingApp/API/Controllers/BaseApiController.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace API.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class BaseApiController : ControllerBase | ||
{ | ||
|
||
} |
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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace API.DTOs; | ||
|
||
public class LoginDto | ||
{ | ||
[Required] | ||
public string UserName { get; set; } | ||
[Required] | ||
public string Password { 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace API.DTOs; | ||
|
||
public class RegisterDto | ||
{ | ||
[Required] | ||
public string UserName { get; set; } | ||
[Required] | ||
public string Password { 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace API.DTOs; | ||
|
||
public class UserDto | ||
{ | ||
public string UserName { get; set; } | ||
public string Token { get; set; } | ||
} |
44 changes: 44 additions & 0 deletions
44
...CSharp_Angular/DatingApp/API/Data/Migrations/20230909193814_UserPasswordAdded.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
Angular/CSharp_Angular/DatingApp/API/Data/Migrations/20230909193814_UserPasswordAdded.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
||
namespace API.Data.Migrations | ||
{ | ||
/// <inheritdoc /> | ||
public partial class UserPasswordAdded : Migration | ||
{ | ||
/// <inheritdoc /> | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.AddColumn<byte[]>( | ||
name: "PasswordHash", | ||
table: "Users", | ||
type: "BLOB", | ||
nullable: true); | ||
|
||
migrationBuilder.AddColumn<byte[]>( | ||
name: "PasswordSalt", | ||
table: "Users", | ||
type: "BLOB", | ||
nullable: true); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropColumn( | ||
name: "PasswordHash", | ||
table: "Users"); | ||
|
||
migrationBuilder.DropColumn( | ||
name: "PasswordSalt", | ||
table: "Users"); | ||
} | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
Angular/CSharp_Angular/DatingApp/API/Extensions/ApplicationServiceExtensions.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using API.Data; | ||
using API.Interfaces; | ||
using API.Services; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace API.Extensions; | ||
|
||
public static class ApplicationServiceExtensions | ||
{ | ||
public static IServiceCollection AddApplicationServices(this IServiceCollection services, | ||
IConfiguration config) | ||
{ | ||
services.AddDbContext<DataContext>(opt => | ||
{ | ||
opt.UseSqlite(config.GetConnectionString("DefaultConnection")); | ||
}); | ||
services.AddCors(); | ||
services.AddScoped<ITokenServices, TokenService>(); | ||
|
||
return services; | ||
} | ||
|
||
} |
Oops, something went wrong.