Skip to content

Commit bab237b

Browse files
committed
Added example ASP.NET Core app
1 parent 0360a88 commit bab237b

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.14" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\AspNetSaml\AspNetSaml.csproj" />
16+
</ItemGroup>
17+
18+
</Project>

AspNetSaml.WebExample/Program.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
// TODO: specify the certificate that your SAML provider gave you, and your app's entity ID
4+
const string SAML_CERTIFICATE = """
5+
-----BEGIN CERTIFICATE-----
6+
BLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAH123543==
7+
-----END CERTIFICATE-----
8+
""";
9+
const string ENTITY_ID = "[YOUR_ENTITY_ID]";
10+
11+
var builder = WebApplication.CreateBuilder(args);
12+
var app = builder.Build();
13+
app.UseHttpsRedirection();
14+
15+
16+
//homepage
17+
app.MapGet("/", () =>
18+
{
19+
//TODO: specify the SAML provider url here, aka "Endpoint"
20+
var samlEndpoint = "http://saml-provider-that-we-use.com/login/";
21+
22+
var request = new Saml.AuthRequest(
23+
ENTITY_ID,
24+
"http://localhost:5000/SamlConsume"
25+
);
26+
27+
//now send the user to the SAML provider
28+
var url = request.GetRedirectUrl(samlEndpoint);
29+
30+
return Results.Content("Click <a href=\"" + url + "\">here</a> to log in", "text/html");
31+
});
32+
33+
34+
//IsP will send logged in user here
35+
app.MapPost("/SamlConsume", ([FromForm] string samlResponse) =>
36+
{
37+
var saml = new Saml.Response(SAML_CERTIFICATE, samlResponse);
38+
39+
if (saml.IsValid()) //all good?
40+
{
41+
return Results.Content("Success! Logged in as user " + saml.GetNameID(), "text/html");
42+
}
43+
44+
return Results.Unauthorized();
45+
});
46+
47+
48+
//IdP will send logout requests here
49+
app.MapPost("/SamlLogout", ([FromForm] string samlResponse) =>
50+
{
51+
var saml = new Saml.IdpLogoutRequest(SAML_CERTIFICATE, samlResponse);
52+
53+
if (saml.IsValid()) //all good?
54+
{
55+
var username = saml.GetNameID();
56+
//pseudo-code-logout-user-from-your-system(username);
57+
}
58+
59+
return Results.Ok();
60+
});
61+
62+
app.Run();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"https": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "https://localhost:7009;http://localhost:5266",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
}
13+
}
14+
}

AspNetSaml.WebExample/README

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a simple example of how to use the AspNetSaml library.
2+
3+
A very basic "minimal API" style app ASP.NET 8
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

AspNetSaml.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetSaml", "AspNetSaml\As
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetSaml.Tests", "AspNetSaml.Tests\AspNetSaml.Tests.csproj", "{6633016C-09D8-4B86-876A-6A6E995D5734}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetSaml.WebExample", "AspNetSaml.WebExample\AspNetSaml.WebExample.csproj", "{96C75360-DCD9-4EF3-80A4-BE91032B32DE}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{6633016C-09D8-4B86-876A-6A6E995D5734}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{6633016C-09D8-4B86-876A-6A6E995D5734}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{6633016C-09D8-4B86-876A-6A6E995D5734}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{96C75360-DCD9-4EF3-80A4-BE91032B32DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{96C75360-DCD9-4EF3-80A4-BE91032B32DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{96C75360-DCD9-4EF3-80A4-BE91032B32DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{96C75360-DCD9-4EF3-80A4-BE91032B32DE}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)