Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-Version #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": null
}
8 changes: 8 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ExpandedNodes": [
"",
"\\Services"
],
"SelectedNode": "\\Services\\UserService.cs",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/WebApi/DesignTimeBuild/.dtbcache
Binary file not shown.
Binary file added .vs/WebApi/v16/.suo
Binary file not shown.
Empty file.
Binary file added .vs/WebApi/v16/Server/sqlite3/storage.ide
Binary file not shown.
1,017 changes: 1,017 additions & 0 deletions .vs/aspnet-core-3-registration-login-api/config/applicationhost.config

Large diffs are not rendered by default.

Binary file not shown.
Binary file added .vs/slnx.sqlite
Binary file not shown.
6 changes: 3 additions & 3 deletions Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public UsersController(
[HttpPost("authenticate")]
public IActionResult Authenticate([FromBody]AuthenticateModel model)
{
var user = _userService.Authenticate(model.Username, model.Password);
var user = _userService.Authenticate(model.Dni, model.Password);

if (user == null)
return BadRequest(new { message = "Username or password is incorrect" });
return BadRequest(new { message = "Dni o password incorrecta" });

var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
Expand All @@ -61,7 +61,7 @@ public IActionResult Authenticate([FromBody]AuthenticateModel model)
return Ok(new
{
Id = user.Id,
Username = user.Username,
Dni = user.Dni,
FirstName = user.FirstName,
LastName = user.LastName,
Token = tokenString
Expand Down
2 changes: 1 addition & 1 deletion Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class User
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public string Dni { get; set; }
public byte[] PasswordHash { get; set; }
public byte[] PasswordSalt { get; set; }
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
.Annotation("SqlServer:Identity", "1, 1"),
FirstName = table.Column<string>(nullable: true),
LastName = table.Column<string>(nullable: true),
Username = table.Column<string>(nullable: true),
Dni = table.Column<string>(nullable: true),
PasswordHash = table.Column<byte[]>(nullable: true),
PasswordSalt = table.Column<byte[]>(nullable: true)
},
Expand Down
2 changes: 1 addition & 1 deletion Migrations/SqlServerMigrations/DataContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<byte[]>("PasswordSalt")
.HasColumnType("varbinary(max)");

b.Property<string>("Username")
b.Property<string>("Dni")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
.Annotation("Sqlite:Autoincrement", true),
FirstName = table.Column<string>(nullable: true),
LastName = table.Column<string>(nullable: true),
Username = table.Column<string>(nullable: true),
Dni = table.Column<string>(nullable: true),
PasswordHash = table.Column<byte[]>(nullable: true),
PasswordSalt = table.Column<byte[]>(nullable: true)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<byte[]>("PasswordSalt")
.HasColumnType("BLOB");

b.Property<string>("Username")
b.Property<string>("Dni")
.HasColumnType("TEXT");

b.HasKey("Id");
Expand Down
2 changes: 1 addition & 1 deletion Models/Users/AuthenticateModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace WebApi.Models.Users
public class AuthenticateModel
{
[Required]
public string Username { get; set; }
public string Dni { get; set; }

[Required]
public string Password { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Models/Users/RegisterModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class RegisterModel
public string LastName { get; set; }

[Required]
public string Username { get; set; }
public string Dni { get; set; }

[Required]
public string Password { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Models/Users/UpdateModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class UpdateModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public string Dni { get; set; }
public string Password { get; set; }
}
}
2 changes: 1 addition & 1 deletion Models/Users/UserModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public class UserModel
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public string Dni { get; set; }
}
}
26 changes: 13 additions & 13 deletions Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace WebApi.Services
{
public interface IUserService
{
User Authenticate(string username, string password);
User Authenticate(string dni, string password);
IEnumerable<User> GetAll();
User GetById(int id);
User Create(User user, string password);
Expand All @@ -25,14 +25,14 @@ public UserService(DataContext context)
_context = context;
}

public User Authenticate(string username, string password)
public User Authenticate(string dni, string password)
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
if (string.IsNullOrEmpty(dni) || string.IsNullOrEmpty(password))
return null;

var user = _context.Users.SingleOrDefault(x => x.Username == username);
var user = _context.Users.SingleOrDefault(x => x.dni == dni);

// check if username exists
// check if dni exists
if (user == null)
return null;

Expand Down Expand Up @@ -60,8 +60,8 @@ public User Create(User user, string password)
if (string.IsNullOrWhiteSpace(password))
throw new AppException("Password is required");

if (_context.Users.Any(x => x.Username == user.Username))
throw new AppException("Username \"" + user.Username + "\" is already taken");
if (_context.Users.Any(x => x.dni == user.dni))
throw new AppException("dni \"" + user.dni + "\" is already taken");

byte[] passwordHash, passwordSalt;
CreatePasswordHash(password, out passwordHash, out passwordSalt);
Expand All @@ -82,14 +82,14 @@ public void Update(User userParam, string password = null)
if (user == null)
throw new AppException("User not found");

// update username if it has changed
if (!string.IsNullOrWhiteSpace(userParam.Username) && userParam.Username != user.Username)
// update dni if it has changed
if (!string.IsNullOrWhiteSpace(userParam.dni) && userParam.dni != user.dni)
{
// throw error if the new username is already taken
if (_context.Users.Any(x => x.Username == userParam.Username))
throw new AppException("Username " + userParam.Username + " is already taken");
// throw error if the new dni is already taken
if (_context.Users.Any(x => x.dni == userParam.dni))
throw new AppException("dni " + userParam.dni + " is already taken");

user.Username = userParam.Username;
user.dni = userParam.dni;
}

// update user properties if provided
Expand Down
25 changes: 25 additions & 0 deletions WebApi.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi", "WebApi.csproj", "{03A2C7FA-2FD1-4095-AA9A-BA0BE809439B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{03A2C7FA-2FD1-4095-AA9A-BA0BE809439B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{03A2C7FA-2FD1-4095-AA9A-BA0BE809439B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03A2C7FA-2FD1-4095-AA9A-BA0BE809439B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03A2C7FA-2FD1-4095-AA9A-BA0BE809439B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BF49A40E-181F-4814-A7D5-4445FEC87189}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"WebApiDatabase": "Data Source=LocalDatabase.db"
"WebApiDatabase": "Data Source=DESKTOP-QF24B4L;Initial Catalog=CruzRojaDB;Integrated Security=True;"
},
"Logging": {
"LogLevel": {
Expand Down
28 changes: 14 additions & 14 deletions appsettings.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"AppSettings": {
"Secret": "THIS IS USED TO SIGN AND VERIFY JWT TOKENS, REPLACE IT WITH YOUR OWN SECRET, IT CAN BE ANY STRING"
},
"ConnectionStrings": {
"WebApiDatabase": "ENTER PRODUCTION SQL SERVER CONNECTION STRING HERE"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
"AppSettings": {
"Secret": "administration-password"
},
"ConnectionStrings": {
"WebApiDatabase": "Data Source=DESKTOP-QF24B4L;Initial Catalog=CruzRojaDB;Integrated Security=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}