-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Roland Guijt <[email protected]>
- Loading branch information
1 parent
bd492b2
commit ad16b63
Showing
24 changed files
with
992 additions
and
1,217 deletions.
There are no files selected for viewing
82 changes: 53 additions & 29 deletions
82
IdentityServer/v7/Basics/Apis/ResourceBasedApi/Program.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,59 @@ | ||
using System; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Client; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ResourceBasedApi; | ||
using Serilog; | ||
using Serilog.Events; | ||
using Serilog.Sinks.SystemConsole.Themes; | ||
|
||
namespace ResourceBasedApi | ||
{ | ||
public class Program | ||
Console.Title = "Resource based API"; | ||
Log.Logger = new LoggerConfiguration() | ||
.MinimumLevel.Verbose() | ||
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning) | ||
.MinimumLevel.Override("System", LogEventLevel.Warning) | ||
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information) | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code) | ||
.CreateLogger(); | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddSerilog(); | ||
builder.Services.AddControllers(); | ||
|
||
builder.Services.AddCors(); | ||
builder.Services.AddDistributedMemoryCache(); | ||
|
||
builder.Services.AddAuthentication("token") | ||
// JWT tokens | ||
.AddJwtBearer("token", options => | ||
{ | ||
options.Authority = Urls.IdentityServer; | ||
options.Audience = "resource2"; | ||
options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" }; | ||
// if token does not contain a dot, it is a reference token | ||
options.ForwardDefaultSelector = Selector.ForwardReferenceToken("introspection"); | ||
}) | ||
|
||
// reference tokens | ||
.AddOAuth2Introspection("introspection", options => | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
Console.Title = "Simple API with Resources"; | ||
|
||
BuildWebHost(args).Run(); | ||
} | ||
|
||
public static IWebHost BuildWebHost(string[] args) | ||
{ | ||
Log.Logger = new LoggerConfiguration() | ||
.MinimumLevel.Verbose() | ||
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning) | ||
.MinimumLevel.Override("System", LogEventLevel.Warning) | ||
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information) | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code) | ||
.CreateLogger(); | ||
|
||
return WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>() | ||
.Build(); | ||
} | ||
} | ||
} | ||
options.Authority = Urls.IdentityServer; | ||
options.ClientId = "resource1"; | ||
options.ClientSecret = "secret"; | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseRouting(); | ||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
app.MapControllers().RequireAuthorization(); | ||
|
||
app.Run(); | ||
|
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 file was deleted.
Oops, something went wrong.
88 changes: 40 additions & 48 deletions
88
IdentityServer/v7/Basics/ClientCredentials/src/Program.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,48 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Client; | ||
using IdentityModel.Client; | ||
|
||
namespace Client | ||
Console.Title = "Console Client Credentials Flow"; | ||
|
||
var response = await RequestTokenAsync(); | ||
response.Show(); | ||
|
||
Console.ReadLine(); | ||
await CallServiceAsync(response.AccessToken); | ||
|
||
static async Task<TokenResponse> RequestTokenAsync() | ||
{ | ||
class Program | ||
var client = new HttpClient(); | ||
|
||
var disco = await client.GetDiscoveryDocumentAsync(Urls.IdentityServer); | ||
if (disco.IsError) throw new Exception(disco.Error); | ||
|
||
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest | ||
{ | ||
public static async Task Main() | ||
{ | ||
Console.Title = "Console Client Credentials Flow"; | ||
|
||
var response = await RequestTokenAsync(); | ||
response.Show(); | ||
|
||
Console.ReadLine(); | ||
await CallServiceAsync(response.AccessToken); | ||
} | ||
|
||
static async Task<TokenResponse> RequestTokenAsync() | ||
{ | ||
var client = new HttpClient(); | ||
|
||
var disco = await client.GetDiscoveryDocumentAsync(Urls.IdentityServer); | ||
if (disco.IsError) throw new Exception(disco.Error); | ||
|
||
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest | ||
{ | ||
Address = disco.TokenEndpoint, | ||
|
||
ClientId = "client.credentials.sample", | ||
ClientSecret = "secret", | ||
|
||
Scope = "scope1" | ||
}); | ||
|
||
if (response.IsError) throw new Exception(response.Error); | ||
return response; | ||
} | ||
|
||
static async Task CallServiceAsync(string token) | ||
{ | ||
var client = new HttpClient | ||
{ | ||
BaseAddress = new Uri(Urls.SampleApi) | ||
}; | ||
|
||
client.SetBearerToken(token); | ||
var response = await client.GetStringAsync("identity"); | ||
|
||
"\n\nService claims:".ConsoleGreen(); | ||
Console.WriteLine(response.PrettyPrintJson()); | ||
} | ||
} | ||
Address = disco.TokenEndpoint, | ||
|
||
ClientId = "client.credentials.sample", | ||
ClientSecret = "secret", | ||
|
||
Scope = "scope1" | ||
}); | ||
|
||
if (response.IsError) throw new Exception(response.Error); | ||
return response; | ||
} | ||
|
||
static async Task CallServiceAsync(string token) | ||
{ | ||
var client = new HttpClient | ||
{ | ||
BaseAddress = new Uri(Urls.SampleApi) | ||
}; | ||
|
||
client.SetBearerToken(token); | ||
var response = await client.GetStringAsync("identity"); | ||
|
||
"\n\nService claims:".ConsoleGreen(); | ||
Console.WriteLine(response.PrettyPrintJson()); | ||
} |
Oops, something went wrong.