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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using JWTAuth.API.Data; | |
using Microsoft.EntityFrameworkCore; | |
namespace JWTAuth.API.Controllers | |
{ |
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
namespace JWTAuth.API.Models | |
{ | |
public class User | |
{ | |
public int Id { get; set; } | |
public string Username { get; set; } | |
public byte[] PasswordHash { get; set; } | |
public byte[] PasswordSalt { 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
using JWTAuth.API.Models; | |
using Microsoft.EntityFrameworkCore; | |
namespace JWTAuth.API.Data | |
{ | |
public class DataContext : DbContext | |
{ | |
public DataContext(DbContextOptions<DataContext> options) : base(options) {} | |
public DbSet<Value> Values { get; set; } | |
public DbSet<User> Users { 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
using System; | |
using Microsoft.EntityFrameworkCore.Metadata; | |
using Microsoft.EntityFrameworkCore.Migrations; | |
namespace JWTAuth.API.Migrations | |
{ | |
public partial class AddedUserModel : Migration | |
{ | |
protected override void Up(MigrationBuilder migrationBuilder) | |
{ |
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
using System.Threading.Tasks; | |
using JWTAuth.API.Models; | |
namespace JWTAuth.API.Data | |
{ | |
public interface IAuthRepository | |
{ | |
Task<User> Register(User user, string password); | |
Task<User> Login(string username, string pasword); | |
Task<bool> UserExists(string username); |
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
using System.Threading.Tasks; | |
namespace JWTAuth.API.Data | |
{ | |
public class AuthRepository : IAuthRepository | |
{ | |
public async Task<User> Login(string username, string password) | |
{ | |
} |
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
public async Task<User> Login(string username, string password) | |
{ | |
var user = await _context.Users.FirstOrDefaultAsync(x => x.Username == username); //Get user from database. | |
if(user == null) | |
return null; // User does not exist. | |
if(!VerifyPassword(password, user.PasswordHash,user.PasswordSalt)) | |
return null; | |
return user; |
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
public async Task<bool> UserExists(string username) | |
{ | |
if (await _context.Users.AnyAsync(x => x.Username == username)) | |
return true; | |
return false; | |
} |
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 method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddDbContext<DataContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); | |
services.AddControllers() | |
.AddNewtonsoftJson(); | |
services.AddScoped<IAuthRepository, AuthRepository>(); | |
} |
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
using Microsoft.AspNetCore.Mvc; | |
namespace JWTAuth.API.Controllers | |
{ | |
public class AuthController : Controller | |
{ | |
} | |
} |