Skip to content

Commit 50fcffb

Browse files
committed
Added new demos, cleaned up keys, restructured everything!
1 parent 9368bc6 commit 50fcffb

File tree

514 files changed

+176863
-71
lines changed

Some content is hidden

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

514 files changed

+176863
-71
lines changed

src/ApiAuth/Net/ApiAuth.sln renamed to src/ApiAuth/Net/JwtApiAuth.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.29503.13
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiAuth", "ApiAuth\ApiAuth.csproj", "{BDF10019-A0B8-43AB-A20C-477D77725207}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JwtApiAuth", "JwtApiAuth\JwtApiAuth.csproj", "{BDF10019-A0B8-43AB-A20C-477D77725207}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
### IdP variables
2+
@authority =
3+
@clientId =
4+
@clientSecret =
5+
@grantType = client_credentials
6+
@scope =
7+
8+
# @name IdP
9+
POST {{authority}}
10+
CONTENT-TYPE: application/x-www-form-urlencoded
11+
12+
client_id={{clientId}}
13+
&client_secret={{clientSecret}}
14+
&grant_type={{grantType}}
15+
&scope={{scope}}
16+
17+
### API Call
18+
@accessToken = {{IdP.response.body.access_token}}
19+
@apiHost = localhost:44343
20+
21+
# @name UserAPI
22+
GET https://{{apiHost}}/user
23+
Content-Type: application/json
24+
Authorization: Bearer {{accessToken}}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Diagnostics;
2+
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Extensions.Logging;
5+
6+
using JwtApiAuth.Models;
7+
8+
namespace JwtApiAuth.Controllers
9+
{
10+
public class HomeController : Controller
11+
{
12+
private readonly ILogger<HomeController> _logger;
13+
14+
public HomeController(ILogger<HomeController> logger)
15+
{
16+
_logger = logger;
17+
}
18+
19+
public IActionResult Index()
20+
{
21+
return View();
22+
}
23+
24+
public IActionResult Privacy()
25+
{
26+
return View();
27+
}
28+
29+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
30+
public IActionResult Error()
31+
{
32+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
33+
}
34+
}
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Linq;
2+
3+
using Microsoft.AspNetCore.Authorization;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace JwtApiAuth.Controllers
7+
{
8+
[Authorize, ApiController, Route("user")]
9+
public class UserController : Controller
10+
{
11+
[HttpGet, Route("")]
12+
public dynamic GetAll()
13+
{
14+
var user = User;
15+
var claims = user.Claims.ToDictionary(c => c.Type, c => c.Value);
16+
17+
return claims;
18+
}
19+
}
20+
}
File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace JwtApiAuth.Models
4+
{
5+
public class ErrorViewModel
6+
{
7+
public string RequestId { get; set; }
8+
9+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace JwtApiAuth
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
CreateHostBuilder(args).Build().Run();
11+
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
19+
}
20+
}
File renamed without changes.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using Microsoft.AspNetCore.Authentication.JwtBearer;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Hosting;
4+
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
8+
9+
using Microsoft.IdentityModel.Tokens;
10+
11+
using System.Threading.Tasks;
12+
13+
namespace JwtApiAuth
14+
{
15+
public class Startup
16+
{
17+
public Startup(IConfiguration configuration)
18+
{
19+
Configuration = configuration;
20+
}
21+
22+
public IConfiguration Configuration { get; }
23+
24+
// This method gets called by the runtime. Use this method to add services to the container.
25+
public void ConfigureServices(IServiceCollection services)
26+
{
27+
services.AddControllersWithViews();
28+
29+
services
30+
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
31+
.AddJwtBearer(x =>
32+
{
33+
// Disabled only for local dev
34+
x.RequireHttpsMetadata = false;
35+
x.Authority = Configuration["API:Authority"];
36+
37+
x.TokenValidationParameters = new TokenValidationParameters
38+
{
39+
ValidAudience = Configuration["API:Audience"]
40+
};
41+
42+
x.Events = new JwtBearerEvents();
43+
x.Events.OnAuthenticationFailed += AuthenticationFailed;
44+
x.Events.OnChallenge += Challenge;
45+
x.Events.OnTokenValidated += TokenValidated;
46+
});
47+
}
48+
49+
private Task TokenValidated(TokenValidatedContext arg)
50+
{
51+
return Task.FromResult(0);
52+
}
53+
54+
private Task Challenge(JwtBearerChallengeContext arg)
55+
{
56+
return Task.FromResult(0);
57+
}
58+
59+
private Task AuthenticationFailed(AuthenticationFailedContext arg)
60+
{
61+
return Task.FromResult(0);
62+
}
63+
64+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
65+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
66+
{
67+
if (env.IsDevelopment())
68+
{
69+
app.UseDeveloperExceptionPage();
70+
}
71+
else
72+
{
73+
app.UseExceptionHandler("/Home/Error");
74+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
75+
app.UseHsts();
76+
}
77+
app.UseHttpsRedirection();
78+
app.UseStaticFiles();
79+
80+
app.UseRouting();
81+
82+
app.UseAuthentication();
83+
app.UseAuthorization();
84+
85+
app.UseEndpoints(endpoints =>
86+
{
87+
endpoints.MapControllerRoute(
88+
name: "default",
89+
pattern: "{controller=Home}/{action=Index}/{id?}");
90+
});
91+
}
92+
}
93+
}
File renamed without changes.

0 commit comments

Comments
 (0)