Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 955cd92

Browse files
committed
First cut of demo complete.
1 parent e1669de commit 955cd92

File tree

130 files changed

+3932
-0
lines changed

Some content is hidden

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

130 files changed

+3932
-0
lines changed

‎.vscode/launch.json‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
// Use IntelliSense to find out which attributes exist for C# debugging
3+
// Use hover for the description of the existing attributes
4+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (web)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/WebApi/bin/Debug/netcoreapp2.0/WebApi.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}/WebApi",
16+
"stopAtEntry": false,
17+
"internalConsoleOptions": "openOnSessionStart",
18+
"launchBrowser": {
19+
"enabled": true,
20+
"args": "${auto-detect-url}",
21+
"windows": {
22+
"command": "cmd.exe",
23+
"args": "/C start ${auto-detect-url}"
24+
},
25+
"osx": {
26+
"command": "open"
27+
},
28+
"linux": {
29+
"command": "xdg-open"
30+
}
31+
},
32+
"env": {
33+
"ASPNETCORE_ENVIRONMENT": "Development"
34+
},
35+
"sourceFileMap": {
36+
"/Views": "${workspaceFolder}/Views"
37+
}
38+
},
39+
{
40+
"name": ".NET Core Attach",
41+
"type": "coreclr",
42+
"request": "attach",
43+
"processId": "${command:pickProcess}"
44+
}
45+
]
46+
}

‎.vscode/tasks.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"taskName": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/WebApi/WebApi.csproj"
11+
],
12+
"problemMatcher": "$msCompile"
13+
}
14+
]
15+
}

‎src/.angular-cli.json‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
3+
"project": {
4+
"name": "jwt-auth-demo"
5+
},
6+
"apps": [
7+
{
8+
"root": "src",
9+
"outDir": "wwwroot",
10+
"assets": [
11+
"assets",
12+
"favicon.ico",
13+
"facebook-auth.html"
14+
],
15+
"index": "index.html",
16+
"main": "main.ts",
17+
"polyfills": "polyfills.ts",
18+
"test": "test.ts",
19+
"tsconfig": "tsconfig.app.json",
20+
"testTsconfig": "tsconfig.spec.json",
21+
"prefix": "app",
22+
"styles": [
23+
"../node_modules/bootstrap/dist/css/bootstrap.min.css",
24+
"styles.scss"
25+
],
26+
"scripts": [],
27+
"environmentSource": "environments/environment.ts",
28+
"environments": {
29+
"dev": "environments/environment.ts",
30+
"prod": "environments/environment.prod.ts"
31+
}
32+
}
33+
],
34+
"e2e": {
35+
"protractor": {
36+
"config": "./protractor.conf.js"
37+
}
38+
},
39+
"lint": [
40+
{
41+
"project": "src/tsconfig.app.json",
42+
"exclude": "**/node_modules/**"
43+
},
44+
{
45+
"project": "src/tsconfig.spec.json",
46+
"exclude": "**/node_modules/**"
47+
},
48+
{
49+
"project": "e2e/tsconfig.e2e.json",
50+
"exclude": "**/node_modules/**"
51+
}
52+
],
53+
"test": {
54+
"karma": {
55+
"config": "./karma.conf.js"
56+
}
57+
},
58+
"defaults": {
59+
"styleExt": "scss",
60+
"component": {}
61+
}
62+
}

‎src/.editorconfig‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false

‎src/.gitignore‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# compiled output
4+
/dist
5+
/tmp
6+
/out-tsc
7+
8+
# dependencies
9+
/node_modules
10+
11+
# IDEs and editors
12+
/.idea
13+
.project
14+
.classpath
15+
.c9/
16+
*.launch
17+
.settings/
18+
*.sublime-workspace
19+
20+
# IDE - VSCode
21+
.vscode/*
22+
!.vscode/settings.json
23+
!.vscode/tasks.json
24+
!.vscode/launch.json
25+
!.vscode/extensions.json
26+
27+
# misc
28+
/.sass-cache
29+
/connect.lock
30+
/coverage
31+
/libpeerconnection.log
32+
npm-debug.log
33+
testem.log
34+
/typings
35+
36+
# e2e
37+
/e2e/*.js
38+
/e2e/*.map
39+
40+
# System Files
41+
.DS_Store
42+
Thumbs.db
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<TypeScriptToolsVersion>2.3</TypeScriptToolsVersion>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Folder Include="Migrations\" />
8+
<Folder Include="wwwroot\" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="3.2.0" />
12+
<PackageReference Include="fluentvalidation.aspnetcore" Version="7.3.3" />
13+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
14+
</ItemGroup>
15+
<ItemGroup>
16+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
17+
<!--https://github.com/aspnet/EntityFrameworkCore/issues/8996-->
18+
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
19+
</ItemGroup>
20+
<Target Name="AngularBuild" AfterTargets="Build">
21+
<Exec Command="npm run build" />
22+
</Target>
23+
</Project>

‎src/AngularASPNETCore2WebApiAuth.sln‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26906.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngularASPNETCore2WebApiAuth", "AngularASPNETCore2WebApiAuth.csproj", "{27E2D292-D66B-47A1-AFF0-5DBADD669799}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{27E2D292-D66B-47A1-AFF0-5DBADD669799}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{27E2D292-D66B-47A1-AFF0-5DBADD669799}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{27E2D292-D66B-47A1-AFF0-5DBADD669799}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{27E2D292-D66B-47A1-AFF0-5DBADD669799}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {6A8165CF-7DCC-4821-9956-A3E79FF34691}
24+
EndGlobalSection
25+
EndGlobal

‎src/Auth/IJwtFactory.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+

2+
using System.Security.Claims;
3+
using System.Threading.Tasks;
4+
5+
namespace AngularASPNETCore2WebApiAuth.Auth
6+
{
7+
public interface IJwtFactory
8+
{
9+
Task<string> GenerateEncodedToken(string userName, ClaimsIdentity identity);
10+
ClaimsIdentity GenerateClaimsIdentity(string userName, string id);
11+
}
12+
}

‎src/Auth/JwtFactory.cs‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+

2+
3+
using System;
4+
using System.IdentityModel.Tokens.Jwt;
5+
using System.Security.Claims;
6+
using System.Security.Principal;
7+
using System.Threading.Tasks;
8+
using AngularASPNETCore2WebApiAuth.Models;
9+
using Microsoft.Extensions.Options;
10+
11+
12+
namespace AngularASPNETCore2WebApiAuth.Auth
13+
{
14+
public class JwtFactory : IJwtFactory
15+
{
16+
private readonly JwtIssuerOptions _jwtOptions;
17+
18+
public JwtFactory(IOptions<JwtIssuerOptions> jwtOptions)
19+
{
20+
_jwtOptions = jwtOptions.Value;
21+
ThrowIfInvalidOptions(_jwtOptions);
22+
}
23+
24+
public async Task<string> GenerateEncodedToken(string userName, ClaimsIdentity identity)
25+
{
26+
var claims = new[]
27+
{
28+
new Claim(JwtRegisteredClaimNames.Sub, userName),
29+
new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()),
30+
new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
31+
identity.FindFirst(Helpers.Constants.Strings.JwtClaimIdentifiers.Rol),
32+
identity.FindFirst(Helpers.Constants.Strings.JwtClaimIdentifiers.Id)
33+
};
34+
35+
// Create the JWT security token and encode it.
36+
var jwt = new JwtSecurityToken(
37+
issuer: _jwtOptions.Issuer,
38+
audience: _jwtOptions.Audience,
39+
claims: claims,
40+
notBefore: _jwtOptions.NotBefore,
41+
expires: _jwtOptions.Expiration,
42+
signingCredentials: _jwtOptions.SigningCredentials);
43+
44+
var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
45+
46+
return encodedJwt;
47+
}
48+
49+
public ClaimsIdentity GenerateClaimsIdentity(string userName, string id)
50+
{
51+
return new ClaimsIdentity(new GenericIdentity(userName, "Token"), new[]
52+
{
53+
new Claim(Helpers.Constants.Strings.JwtClaimIdentifiers.Id, id),
54+
new Claim(Helpers.Constants.Strings.JwtClaimIdentifiers.Rol, Helpers.Constants.Strings.JwtClaims.ApiAccess)
55+
});
56+
}
57+
58+
/// <returns>Date converted to seconds since Unix epoch (Jan 1, 1970, midnight UTC).</returns>
59+
private static long ToUnixEpochDate(DateTime date)
60+
=> (long)Math.Round((date.ToUniversalTime() -
61+
new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero))
62+
.TotalSeconds);
63+
64+
private static void ThrowIfInvalidOptions(JwtIssuerOptions options)
65+
{
66+
if (options == null) throw new ArgumentNullException(nameof(options));
67+
68+
if (options.ValidFor <= TimeSpan.Zero)
69+
{
70+
throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(JwtIssuerOptions.ValidFor));
71+
}
72+
73+
if (options.SigningCredentials == null)
74+
{
75+
throw new ArgumentNullException(nameof(JwtIssuerOptions.SigningCredentials));
76+
}
77+
78+
if (options.JtiGenerator == null)
79+
{
80+
throw new ArgumentNullException(nameof(JwtIssuerOptions.JtiGenerator));
81+
}
82+
}
83+
}
84+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+

2+
3+
using System.Threading.Tasks;
4+
using AngularASPNETCore2WebApiAuth.Data;
5+
using AngularASPNETCore2WebApiAuth.Helpers;
6+
using AngularASPNETCore2WebApiAuth.Models.Entities;
7+
using AngularASPNETCore2WebApiAuth.ViewModels;
8+
using AutoMapper;
9+
using Microsoft.AspNetCore.Identity;
10+
using Microsoft.AspNetCore.Mvc;
11+
12+
13+
namespace AngularASPNETCore2WebApiAuth.Controllers
14+
{
15+
[Route("api/[controller]")]
16+
public class AccountsController : Controller
17+
{
18+
private readonly ApplicationDbContext _appDbContext;
19+
private readonly UserManager<AppUser> _userManager;
20+
private readonly IMapper _mapper;
21+
22+
public AccountsController(UserManager<AppUser> userManager, IMapper mapper, ApplicationDbContext appDbContext)
23+
{
24+
_userManager = userManager;
25+
_mapper = mapper;
26+
_appDbContext = appDbContext;
27+
}
28+
29+
// POST api/accounts
30+
[HttpPost]
31+
public async Task<IActionResult> Post([FromBody]RegistrationViewModel model)
32+
{
33+
if (!ModelState.IsValid)
34+
{
35+
return BadRequest(ModelState);
36+
}
37+
38+
var userIdentity = _mapper.Map<AppUser>(model);
39+
40+
var result = await _userManager.CreateAsync(userIdentity, model.Password);
41+
42+
if (!result.Succeeded) return new BadRequestObjectResult(Errors.AddErrorsToModelState(result, ModelState));
43+
44+
await _appDbContext.Customers.AddAsync(new Customer { IdentityId = userIdentity.Id, Location = model.Location });
45+
await _appDbContext.SaveChangesAsync();
46+
47+
return new OkObjectResult("Account created");
48+
}
49+
}
50+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /