Skip to content

Commit

Permalink
Added sample applications for a service api and a blazor based client
Browse files Browse the repository at this point in the history
  • Loading branch information
Nfactor26 committed Apr 10, 2022
1 parent 148dd8b commit e641f85
Show file tree
Hide file tree
Showing 52 changed files with 1,898 additions and 1 deletion.
51 changes: 50 additions & 1 deletion Pixel.Identity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Store.Sql.Sh
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Store.SqlServer", "src\Pixel.Identity.Store.SqlServer\Pixel.Identity.Store.SqlServer.csproj", "{E67F20E6-B228-467A-9C40-1B38EAD35367}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pixel.Identity.Messenger.Console", "src\Pixel.Identity.Messenger.Console\Pixel.Identity.Messenger.Console.csproj", "{137631D2-6F14-43A2-885D-F61C63B7058C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Messenger.Console", "src\Pixel.Identity.Messenger.Console\Pixel.Identity.Messenger.Console.csproj", "{137631D2-6F14-43A2-885D-F61C63B7058C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{D1F9C105-5E1A-42B0-A47F-3DD3B223AD8C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Service.Api", "samples\Sample.Service.Api\Sample.Service.Api.csproj", "{18ADE8F9-D7F2-400A-A2EB-78439F82A468}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.Blazor.App", "samples\Samples.Blazor.App\Samples.Blazor.App.csproj", "{7D75919B-B900-4370-A9AB-913104AEA2F0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.Blazor.App.Host", "samples\Samples.Blazor.App.Host\Samples.Blazor.App.Host.csproj", "{535B958A-7CEF-43EC-9545-3CE999D74889}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -167,10 +175,51 @@ Global
{137631D2-6F14-43A2-885D-F61C63B7058C}.Release|x64.Build.0 = Release|Any CPU
{137631D2-6F14-43A2-885D-F61C63B7058C}.Release|x86.ActiveCfg = Release|Any CPU
{137631D2-6F14-43A2-885D-F61C63B7058C}.Release|x86.Build.0 = Release|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Debug|x64.ActiveCfg = Debug|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Debug|x64.Build.0 = Debug|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Debug|x86.ActiveCfg = Debug|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Debug|x86.Build.0 = Debug|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Release|Any CPU.Build.0 = Release|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Release|x64.ActiveCfg = Release|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Release|x64.Build.0 = Release|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Release|x86.ActiveCfg = Release|Any CPU
{18ADE8F9-D7F2-400A-A2EB-78439F82A468}.Release|x86.Build.0 = Release|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Debug|x64.ActiveCfg = Debug|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Debug|x64.Build.0 = Debug|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Debug|x86.ActiveCfg = Debug|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Debug|x86.Build.0 = Debug|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Release|Any CPU.Build.0 = Release|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Release|x64.ActiveCfg = Release|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Release|x64.Build.0 = Release|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Release|x86.ActiveCfg = Release|Any CPU
{7D75919B-B900-4370-A9AB-913104AEA2F0}.Release|x86.Build.0 = Release|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Debug|Any CPU.Build.0 = Debug|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Debug|x64.ActiveCfg = Debug|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Debug|x64.Build.0 = Debug|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Debug|x86.ActiveCfg = Debug|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Debug|x86.Build.0 = Debug|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Release|Any CPU.ActiveCfg = Release|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Release|Any CPU.Build.0 = Release|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Release|x64.ActiveCfg = Release|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Release|x64.Build.0 = Release|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Release|x86.ActiveCfg = Release|Any CPU
{535B958A-7CEF-43EC-9545-3CE999D74889}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{18ADE8F9-D7F2-400A-A2EB-78439F82A468} = {D1F9C105-5E1A-42B0-A47F-3DD3B223AD8C}
{7D75919B-B900-4370-A9AB-913104AEA2F0} = {D1F9C105-5E1A-42B0-A47F-3DD3B223AD8C}
{535B958A-7CEF-43EC-9545-3CE999D74889} = {D1F9C105-5E1A-42B0-A47F-3DD3B223AD8C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D2D4C886-7F44-4E77-9010-D9AB3C1B559C}
EndGlobalSection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Sample.Service.Api.Controllers;

/// <summary>
/// We require authorized access with policy ReadWeatherDataPolicy.
/// This policy is configured during startup.
/// </summary>
[Authorize(Policy = Policies.ReadWeatherDataPolicy)]
[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

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

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
6 changes: 6 additions & 0 deletions samples/Sample.Service.Api/Policies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Sample.Service.Api;

public class Policies
{
public const string ReadWeatherDataPolicy = nameof(ReadWeatherDataPolicy);
}
17 changes: 17 additions & 0 deletions samples/Sample.Service.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Samples.Service.Api;

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls("http://localhost:5216");
});
}
31 changes: 31 additions & 0 deletions samples/Sample.Service.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:47477",
"sslPort": 0
}
},
"profiles": {
"Sample.Service.Api": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5216",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
15 changes: 15 additions & 0 deletions samples/Sample.Service.Api/Sample.Service.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.0" />
</ItemGroup>

</Project>
83 changes: 83 additions & 0 deletions samples/Sample.Service.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using IdentityModel.AspNetCore.OAuth2Introspection;
using Sample.Service.Api;

namespace Samples.Service.Api;

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//configure cors to allow requests from http://localhost:5239 which is our client application
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins("http://localhost:5239");
builder.AllowAnyHeader();
builder.AllowAnyMethod();
builder.AllowCredentials();
});
});

//Configure Authentication to use introspection i.e. API will check with OAuth2 introspection endpoint to validate
//if request is authenticated.
services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
.AddOAuth2Introspection(options =>
{
options.Authority = "http://localhost:44382/pauth";
options.ClientId = "sample-service-api";
options.ClientSecret = "service-secret";
});

//Configure authorization policy that requires read-weather = true claim to read weather data
services.AddAuthorizationCore(options =>
{
//Add a policy to require read-weather claim
options.AddPolicy(Policies.ReadWeatherDataPolicy, policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim("read-weather", "true");
});
});
services.AddControllers();
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Weather Forecast v1");
});

app.UseRouting();
app.UseCors();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}
}
12 changes: 12 additions & 0 deletions samples/Sample.Service.Api/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Sample.Service.Api;

public class WeatherForecast
{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
10 changes: 10 additions & 0 deletions samples/Sample.Service.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ASPNETCORE_URLS": "http://localhost:5216"
}

9 changes: 9 additions & 0 deletions samples/Sample.Service.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
17 changes: 17 additions & 0 deletions samples/Samples.Blazor.App.Host/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Samples.Blazor.App.Host;

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls("http://localhost:5239");
});
}
28 changes: 28 additions & 0 deletions samples/Samples.Blazor.App.Host/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61849",
"sslPort": 0
}
},
"profiles": {
"Samples.Blazor.App.Host": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5282",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
17 changes: 17 additions & 0 deletions samples/Samples.Blazor.App.Host/Samples.Blazor.App.Host.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Samples.Blazor.App\Samples.Blazor.App.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.1" />
</ItemGroup>

</Project>
Loading

0 comments on commit e641f85

Please sign in to comment.