Skip to content

Commit 0fbf506

Browse files
author
FinGY
committed
AuthController with DTO
1 parent bd66c01 commit 0fbf506

File tree

58 files changed

+18973
-62
lines changed

Some content is hidden

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

58 files changed

+18973
-62
lines changed

BAuth.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.9" />
1012
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
1113
</ItemGroup>
1214

Controllers/AuthController.cs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Microsoft.AspNetCore.Identity;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Security.Cryptography;
4+
5+
namespace BAuth.Controllers
6+
{
7+
[ApiController]
8+
[Route("api/[controller]")]
9+
public class AuthController : Controller
10+
{
11+
public static User user = new User();
12+
[HttpPost("register")]
13+
public async Task<ActionResult<User>> Register(UserDto request)
14+
{
15+
CreatePasswordHash(request.Password,out byte[] passwordHash,out byte[] passwordSalt);
16+
user.Name = request.Name;
17+
user.PasswordSalt = passwordSalt;
18+
user.PasswordHash = passwordHash;
19+
return Ok(user);
20+
21+
}
22+
23+
[HttpPost("login")]
24+
public async Task<ActionResult<User>> LogIn(UserDto request)
25+
{
26+
if(user.Name != request.Name)
27+
{
28+
return BadRequest("User not found!");
29+
}
30+
if(!VerifyPassword(request.Password, user.PasswordHash,user.PasswordSalt))
31+
{
32+
return BadRequest("Wrong password!");
33+
}
34+
var token = CreateToken()
35+
return Ok(token);
36+
}
37+
38+
private void CreatePasswordHash(string password, out byte[]passwordHash, out byte[]PasswordSalt)
39+
{
40+
using (var hmac = new HMACSHA256())
41+
{
42+
PasswordSalt = hmac.Key;
43+
passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
44+
}
45+
}
46+
private bool VerifyPassword(string password, out byte[]passwordHash, out byte[]PasswordSalt)
47+
{
48+
using (var hmac = new HMACSHA256(PasswordSalt))
49+
{
50+
var computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
51+
return computedHash.SequenceEqual(passwordHash);
52+
}
53+
}
54+
55+
56+
}
57+
}

Controllers/UserController.cs

+51-51
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
using Microsoft.AspNetCore.Identity;
2-
using Microsoft.AspNetCore.Mvc;
1+
// using Microsoft.AspNetCore.Identity;
2+
// using Microsoft.AspNetCore.Mvc;
33

4-
namespace BAuth.Controllers
5-
{
6-
[ApiController]
7-
[Route("api/[controller]")]
8-
public class UserController : Controller
9-
{
10-
public static List<User> users = new List<User>()
11-
{
12-
new User
13-
{
14-
Name = "John",
15-
Password = "pass1"
16-
},
17-
new User
18-
{
19-
Name = "Dave",
20-
Password = "pass11"
21-
}
4+
// namespace BAuth.Controllers
5+
// {
6+
// [ApiController]
7+
// [Route("api/[controller]")]
8+
// public class UserController : Controller
9+
// {
10+
// public static List<User> users = new List<User>()
11+
// {
12+
// new User
13+
// {
14+
// Name = "John",
15+
// Password = "pass1"
16+
// },
17+
// new User
18+
// {
19+
// Name = "Dave",
20+
// Password = "pass11"
21+
// }
2222

23-
};
23+
// };
2424

25-
[HttpGet]
26-
public async Task<ActionResult<List<User>>> GetAll()
27-
{
28-
return Ok(users);
29-
}
25+
// [HttpGet]
26+
// public async Task<ActionResult<List<User>>> GetAll()
27+
// {
28+
// return Ok(users);
29+
// }
3030

31-
[HttpGet ("{name}")]
32-
public async Task<ActionResult<List<User>>>GetUser( string name)
33-
{
34-
var user = users.Find(user=>user.Name == name);
35-
if (user == null)
36-
{
37-
return BadRequest("User not found");
38-
}
39-
return Ok(user);
40-
}
31+
// [HttpGet ("{name}")]
32+
// public async Task<ActionResult<List<User>>>GetUser( string name)
33+
// {
34+
// var user = users.Find(user=>user.Name == name);
35+
// if (user == null)
36+
// {
37+
// return BadRequest("User not found");
38+
// }
39+
// return Ok(user);
40+
// }
4141

42-
[HttpPost]
43-
public async Task<ActionResult<List<User>>>AddUser(User user)
44-
{
45-
users.Add(user);
46-
return Ok(users);
47-
}
48-
[HttpDelete ("{name}")]
49-
public async Task<ActionResult<List<User>>>DeleteUSer(string name)
50-
{
51-
var user = users.Find(user=>user.Name == name);
52-
users.Remove(user);
53-
return Ok(users);
54-
}
55-
}
56-
}
42+
// [HttpPost]
43+
// public async Task<ActionResult<List<User>>>AddUser(User user)
44+
// {
45+
// users.Add(user);
46+
// return Ok(users);
47+
// }
48+
// [HttpDelete ("{name}")]
49+
// public async Task<ActionResult<List<User>>>DeleteUSer(string name)
50+
// {
51+
// var user = users.Find(user=>user.Name == name);
52+
// users.Remove(user);
53+
// return Ok(users);
54+
// }
55+
// }
56+
// }

Data/UserDbContext.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace BAuth
4+
{
5+
public class UserDbContext : DbContext
6+
{
7+
public UserDbContext(DbContextOptions<UserDbContext> options):base(options)
8+
{
9+
10+
}
11+
public DbSet<User> Users {get;set;}
12+
}
13+
}

Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var builder = WebApplication.CreateBuilder(args);
22

33
// Add services to the container.
4+
// builder.Services.AddDbContext<UserDbContext>
45

56
builder.Services.AddControllers();
67
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

User.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ namespace BAuth
44
{
55
public class User
66
{
7+
public Int32 ID {get;set;}
78
public string Name { get; set; }
8-
public string Password {get;set;}
9+
public byte[] PasswordSalt {get;set;}
10+
public byte[] PasswordHash {get;set;}
911
}
1012
}

UserDto.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace BAuth
2+
{
3+
public class UserDto
4+
{
5+
public string Name {get;set;} = string.Empty;
6+
public string Password{get;set;} = string.Empty;
7+
}
8+
}

0 commit comments

Comments
 (0)