Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update UserInteraction samples to .NET 8 #173

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
58 changes: 58 additions & 0 deletions IdentityServer/v7/UserInteraction/Ciba/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"version": "0.2.0",
"compounds": [
{
"name": "Run All",
"configurations": ["Api","Client","IdentityServerHost"],
"presentation": {
"group": "10-compounds",
"order": 1
}
}
],
"configurations": [
{
"name": "IdentityServerHost",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-identityserver",
"program": "${workspaceFolder}/IdentityServerHost/bin/Debug/net8.0/IdentityServerHost.dll",
"args": [],
"cwd": "${workspaceFolder}/IdentityServerHost",
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"console": "externalTerminal"
},
{
"name": "Client",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-client",
"program": "${workspaceFolder}/Client/bin/Debug/net8.0/Client.dll",
"args": [],
"cwd": "${workspaceFolder}/Client",
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"console": "externalTerminal"
},
{
"name": "Api",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-api",
"program": "${workspaceFolder}/Api/bin/Debug/net8.0/Api.dll",
"args": [],
"cwd": "${workspaceFolder}/Api",
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"console": "externalTerminal"
}
]
}
41 changes: 41 additions & 0 deletions IdentityServer/v7/UserInteraction/Ciba/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build-identityserver",
"type": "process",
"command": "dotnet",
"args": [
"build",
"${workspaceFolder}/IdentityServerHost/IdentityServerHost.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build-client",
"type": "process",
"command": "dotnet",
"args": [
"build",
"${workspaceFolder}/Client/Client.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build-api",
"type": "process",
"command": "dotnet",
"args": [
"build",
"${workspaceFolder}/Api/Api.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.1" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions IdentityServer/v7/UserInteraction/Ciba/Api/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Api;

public class Constants
{
public const string Authority = "https://localhost:5001";
public const string SampleApi = "https://localhost:5005/";
}
27 changes: 27 additions & 0 deletions IdentityServer/v7/UserInteraction/Ciba/Api/IdentityController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Linq;

namespace Api;

[Route("identity")]
public class IdentityController : ControllerBase
{
private readonly ILogger<IdentityController> _logger;

public IdentityController(ILogger<IdentityController> logger)
{
_logger = logger;
}

// this action simply echoes the claims back to the client
[HttpGet]
public ActionResult Get()
{
var claims = User.Claims.Select(c => new { c.Type, c.Value });
_logger.LogInformation("claims: {claims}", claims);

return new JsonResult(claims);
}
}
34 changes: 34 additions & 0 deletions IdentityServer/v7/UserInteraction/Ciba/Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Sinks.SystemConsole.Themes;

namespace Api;

public class Program
{
public static void Main(string[] args)
{
Console.Title = "API";

BuildWebHost(args).Run();
}

public static IHost BuildWebHost(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code)
.CreateLogger();

return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseSerilog()
.Build();
}
}
File renamed without changes.
41 changes: 41 additions & 0 deletions IdentityServer/v7/UserInteraction/Ciba/Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;


namespace Api;

public class Startup
{
public Startup()
{
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();

// this API will accept any access token from the authority
services.AddAuthentication("token")
.AddJwtBearer("token", options =>
{
options.Authority = Constants.Authority;
options.TokenValidationParameters.ValidateAudience = false;

options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" };
});
}

public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers().RequireAuthorization();
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>

Expand Down
50 changes: 50 additions & 0 deletions IdentityServer/v7/UserInteraction/Ciba/Client/ConsoleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Diagnostics;

namespace Clients;

public static class ConsoleExtensions
{
/// <summary>
/// Writes green text to the console.
/// </summary>
/// <param name="text">The text.</param>
[DebuggerStepThrough]
public static void ConsoleGreen(this string text)
{
text.ColoredWriteLine(ConsoleColor.Green);
}

/// <summary>
/// Writes red text to the console.
/// </summary>
/// <param name="text">The text.</param>
[DebuggerStepThrough]
public static void ConsoleRed(this string text)
{
text.ColoredWriteLine(ConsoleColor.Red);
}

/// <summary>
/// Writes yellow text to the console.
/// </summary>
/// <param name="text">The text.</param>
[DebuggerStepThrough]
public static void ConsoleYellow(this string text)
{
text.ColoredWriteLine(ConsoleColor.Yellow);
}

/// <summary>
/// Writes out text with the specified ConsoleColor.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="color">The color.</param>
[DebuggerStepThrough]
public static void ColoredWriteLine(this string text, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(text);
Console.ResetColor();
}
}
7 changes: 7 additions & 0 deletions IdentityServer/v7/UserInteraction/Ciba/Client/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Clients;

public class Constants
{
public const string Authority = "https://localhost:5001";
public const string SampleApi = "https://localhost:5002/";
}
Loading
Loading