Skip to content

Commit 7f72851

Browse files
author
ReketeBravo
committed
Added Identity and Authorization JWT
1 parent ecdc323 commit 7f72851

25 files changed

+1291
-186
lines changed

src/BareMetalApi/.vscode/launch.json renamed to .vscode/launch.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
{
22
"version": "0.2.0",
33
"configurations": [
4+
{
5+
"name": ".NET Core Launch (console)",
6+
"type": "coreclr",
7+
"request": "launch",
8+
"preLaunchTask": "build",
9+
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
10+
"args": [],
11+
"cwd": "${workspaceRoot}",
12+
"stopAtEntry": false,
13+
"externalConsole": false
14+
},
415
{
516
"name": ".NET Core Launch (web)",
617
"type": "coreclr",
718
"request": "launch",
819
"preLaunchTask": "build",
9-
"program": "${workspaceRoot}\\bin\\Debug\\netcoreapp1.1\\BareMetalApi.dll",
20+
"program": "${workspaceRoot}/src/baremetalapi/bin/Debug/netcoreapp1.1/BareMetalApi.dll",
1021
"args": [],
1122
"cwd": "${workspaceRoot}",
1223
"stopAtEntry": false,
13-
"internalConsoleOptions": "openOnSessionStart",
1424
"launchBrowser": {
1525
"enabled": true,
1626
"args": "${auto-detect-url}",

src/BareMetalApi/.vscode/tasks.json renamed to .vscode/tasks.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
24
"version": "0.1.0",
35
"command": "dotnet",
46
"isShellCommand": true,
@@ -7,9 +9,10 @@
79
{
810
"taskName": "build",
911
"args": [
10-
"${workspaceRoot}\\project.json"
12+
"src\\baremetalapi\\project.json"
1113
],
1214
"isBuildCommand": true,
15+
"showOutput": "silent",
1316
"problemMatcher": "$msCompile"
1417
}
1518
]

src/BareMetalApi/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ bower_components/
1414
node_modules/
1515
debugSettings.json
1616
project.lock.json
17+
appsettings.json
1718
*.user
1819
*.suo
1920
*.cache
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Identity Example from Microsoft Github
2+
//https://github.com/aspnet/Identity/blob/dev/samples/IdentitySample.Mvc/Controllers/AccountController.cs
3+
//TODO *Implement External Login (fb,google,github) *Email verification
4+
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Authorization;
7+
using Microsoft.AspNetCore.Identity;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Logging;
10+
using BareMetalApi.Models;
11+
12+
13+
namespace BareMetalApi.Controllers
14+
{
15+
[Route("blog/[controller]")]
16+
public class AccountController : ControllerBase
17+
{
18+
private readonly UserManager<ApplicationUser> _userManager;
19+
private readonly SignInManager<ApplicationUser> _signInManager;
20+
private readonly ILogger _logger;
21+
22+
public AccountController(
23+
UserManager<ApplicationUser> userManager,
24+
SignInManager<ApplicationUser> signInManager,
25+
ILoggerFactory loggerFactory)
26+
{
27+
_userManager = userManager;
28+
_signInManager = signInManager;
29+
_logger = loggerFactory.CreateLogger<AccountController>();
30+
}
31+
32+
// POST: /Account/login
33+
[HttpPost("login")]
34+
[AllowAnonymous]
35+
public async Task<dynamic> Login([FromBody] ApplicationUser model)
36+
{
37+
38+
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
39+
var result = await _signInManager.PasswordSignInAsync(model.Email, model.PasswordHash, isPersistent: true, lockoutOnFailure: false);
40+
if (result.Succeeded)
41+
{
42+
43+
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
44+
// Send an email with this link
45+
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
46+
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
47+
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
48+
49+
return Ok();
50+
}
51+
return BadRequest();
52+
53+
54+
55+
56+
// This doesn't count login failures towards account lockout
57+
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
58+
//if (req.Email == null || req.Password == null) return new { authenticated = false };
59+
//var result = await _signInManager.PasswordSignInAsync(req.Email, req.Password, isPersistent: true, lockoutOnFailure: false);
60+
//if (result.Succeeded)
61+
//{
62+
//DateTime? expires = DateTime.UtcNow.AddMinutes(2);
63+
//var token = GetToken(req.Email, expires);
64+
//return new { authenticated = true, entityId = 1, token = token, tokenExpires = expires };
65+
//}
66+
67+
//return new { authenticated = false };
68+
}
69+
70+
71+
72+
// POST: /Account/register
73+
[HttpPost("register")]
74+
[AllowAnonymous]
75+
public async Task<dynamic> Register([FromBody] ApplicationUser model)
76+
{
77+
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
78+
var result = await _userManager.CreateAsync(user, model.PasswordHash);
79+
if (result.Succeeded)
80+
{
81+
await _signInManager.SignInAsync(user, false);
82+
83+
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
84+
// Send an email with this link
85+
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
86+
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
87+
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
88+
89+
return Ok();
90+
}
91+
return BadRequest();
92+
}
93+
94+
//
95+
// POST: /Account/LogOff
96+
[HttpPost]
97+
public async Task<IActionResult> LogOff()
98+
{
99+
await _signInManager.SignOutAsync();
100+
_logger.LogInformation(4, "User logged out.");
101+
return Ok();
102+
}
103+
}
104+
}

src/BareMetalApi/Controllers/BlogArticleController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.AspNetCore.Http;
4-
using BareMetalApi.Data;
5-
using BareMetalApi.Data.Entities;
4+
using BareMetalApi.Models;
5+
using Microsoft.AspNetCore.Authorization;
6+
using BareMetalApi.Repositories.Interfaces;
67

78
namespace BareMetalApi.Controllers
89
{
910
[Route("blog/[controller]")]
11+
[Authorize("Bearer")]
1012
public class BlogArticleController : ControllerBase
1113
{
1214
private readonly IBlogArticleRepository _repository;
@@ -26,9 +28,9 @@ public IActionResult Get()
2628

2729
// GET blog/blogarticle/5
2830
[HttpGet("{id}")]
29-
public string Get(int id)
31+
public IActionResult Get(int id)
3032
{
31-
return "value";
33+
return Ok( _repository.GetById(id).Result);
3234
}
3335

3436
// POST blog/blogarticle

src/BareMetalApi/Data/Migrations/20170314_InitialCreate.Designer.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/BareMetalApi/Data/Migrations/20170314_InitialCreate.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/BareMetalApi/Data/Migrations/ApplicationDbContextModelSnapshot.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/BareMetalApi/Data/Services/BlogArticleRepository.cs

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)