Added a way to login and register and a tools project

This commit is contained in:
Giovani
2019-05-06 13:12:35 -04:00
parent 62306c3530
commit 59b3cf87b0
15 changed files with 231 additions and 31 deletions

View File

@@ -18,6 +18,7 @@
<ItemGroup>
<ProjectReference Include="..\BrightGlimmer.Data\BrightGlimmer.Data.csproj" />
<ProjectReference Include="..\BrightGlimmer.Domain\BrightGlimmer.Domain.csproj" />
<ProjectReference Include="..\BrightGlimmer.Service\BrightGlimmer.Service.csproj" />
</ItemGroup>
</Project>

View File

@@ -5,35 +5,40 @@ using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using BrightGlimmer.Auth.Request;
using BrightGlimmer.Domain.Auth;
using BrightGlimmer.Service.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
namespace BrightGlimmer.Auth.Controllers
{
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IUserService userService;
public UserController(IUserService userService)
{
this.userService = userService;
}
[AllowAnonymous]
[HttpGet]
public ActionResult Login(string username, string password)
{
/* TODO: Move token creation to service */
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.UTF8.GetBytes(Startup.Configuration.GetSection("Keys")["JwtPrivateKey"]);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.NameIdentifier, username)
}),
Expires = DateTime.UtcNow.AddDays(3),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var tokenSecurity = tokenHandler.CreateToken(tokenDescriptor);
var token = tokenHandler.WriteToken(tokenSecurity);
var token = userService.Login(username, password);
return new JsonResult(token);
}
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult> Register(RegisterUserRequest request)
{
var token = await userService.RegisterAsync(request.User, request.Password);
return new JsonResult(token);
}
}

View File

@@ -0,0 +1,14 @@
using BrightGlimmer.Domain.Auth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BrightGlimmer.Auth.Request
{
public class RegisterUserRequest
{
public User User { get; set; }
public string Password { get; set; }
}
}

View File

@@ -4,6 +4,8 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrightGlimmer.Data;
using BrightGlimmer.Service.Interfaces;
using BrightGlimmer.Service.Services;
using JsonNet.PrivateSettersContractResolvers;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
@@ -26,7 +28,7 @@ namespace BrightGlimmer.Auth
Configuration = configuration;
}
public static IConfiguration Configuration { get; private set; }
public IConfiguration Configuration { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
@@ -63,6 +65,9 @@ namespace BrightGlimmer.Auth
services.AddDbContext<AuthContext>(options => options.UseLazyLoadingProxies()
.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<AuthContext, AuthContext>();
/* Inject Services */
services.AddTransient<IUserService, UserService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.