Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
d224fad
Initial structure
damienbod Oct 22, 2024
09fe109
Link menu
damienbod Oct 22, 2024
6884d25
Add sample
damienbod Oct 23, 2024
557e270
Update new authentication for OIDC
damienbod Oct 23, 2024
a5ea417
samples
damienbod Oct 23, 2024
f6f462b
Add links from the used standards
damienbod Oct 23, 2024
b0692db
Add a reference link
damienbod Oct 23, 2024
e9d4040
Update sections
damienbod Oct 23, 2024
0f34cb5
Add a link
damienbod Oct 23, 2024
22b5ec4
Add some definitions
damienbod Oct 23, 2024
149fd91
Add image
damienbod Oct 23, 2024
f850f78
Update image
damienbod Oct 23, 2024
9535563
Add an Overview
damienbod Oct 23, 2024
31dec59
Update docs
damienbod Oct 23, 2024
88fd2f2
Update doc
damienbod Oct 23, 2024
6f699ef
Add customizations
damienbod Oct 23, 2024
b117a14
Update sections
damienbod Oct 23, 2024
a4559ba
Add more data
damienbod Oct 23, 2024
0db2826
Add third party information
damienbod Oct 23, 2024
4e80b45
Update texts
damienbod Oct 23, 2024
185cb8a
add background to image
damienbod Oct 23, 2024
b140eb9
clean up
damienbod Oct 23, 2024
8c3b3c2
remove file
damienbod Oct 23, 2024
8bb3e27
Update text
damienbod Oct 23, 2024
66c08f9
Update text
damienbod Oct 23, 2024
2caea0d
Update text
damienbod Oct 23, 2024
798b75c
text
damienbod Oct 23, 2024
98ea9d8
fix line 80
damienbod Oct 24, 2024
7e5d4c4
fix link
damienbod Oct 24, 2024
5f34f37
link change due to build line 15
damienbod Oct 24, 2024
002e14d
build test
damienbod Oct 24, 2024
81d26a4
Remove toc link due to build in dev branch
damienbod Oct 24, 2024
8a80c23
reset link, no change to build
damienbod Oct 24, 2024
b29c522
fix links
damienbod Oct 24, 2024
a0c986c
Test add toc menu
damienbod Oct 24, 2024
e29e969
Connect not connect
damienbod Oct 24, 2024
3ae9d22
Improve image
damienbod Oct 24, 2024
d88cffe
fix small typos
damienbod Oct 24, 2024
f761a91
typo
damienbod Oct 24, 2024
c4a3261
Update configuration
damienbod Nov 9, 2024
19e667c
Update programs
damienbod Nov 9, 2024
8199ed2
Use FallbackPolicy instead of MVC options
damienbod Nov 9, 2024
5a7e807
UseAuthorization() comment
damienbod Nov 9, 2024
b342f94
Clean up sample using feedback
damienbod Nov 9, 2024
4a9e039
code clean up
damienbod Nov 9, 2024
f0f5789
Update text based on PR feedback
damienbod Nov 9, 2024
c6355bc
2 spaces
damienbod Nov 9, 2024
bc7afe1
Update text
damienbod Nov 9, 2024
1a974a5
Update text
damienbod Nov 9, 2024
708bd1b
Improve text
damienbod Nov 9, 2024
1a2143a
text
damienbod Nov 9, 2024
a802049
Update sample
damienbod Nov 9, 2024
72dcd60
Update sample
damienbod Nov 9, 2024
d4714b0
Fix code example
damienbod Nov 9, 2024
f64b6a6
spaces
damienbod Nov 9, 2024
2114f6a
spaces
damienbod Nov 9, 2024
5e558c4
spaces
damienbod Nov 9, 2024
5846b59
PR feedback
damienbod Nov 12, 2024
7d29cd4
Improve text
damienbod Nov 12, 2024
d250ac7
Update schemes doc
damienbod Nov 12, 2024
8bf7c68
logout
damienbod Nov 12, 2024
e3af7a1
rename claim mappings
damienbod Nov 12, 2024
9e75b67
Update code
damienbod Nov 12, 2024
52d26d5
Update link
damienbod Nov 12, 2024
4197943
Update link
damienbod Nov 12, 2024
c3017ff
fix link
damienbod Nov 12, 2024
8ee2ea4
logging
damienbod Nov 12, 2024
a888f3c
Update links
damienbod Nov 12, 2024
42aa032
fix spacing
damienbod Nov 12, 2024
22c93e3
grammer
damienbod Nov 12, 2024
ef96027
grammer
damienbod Nov 12, 2024
f05ca90
Add Login page
damienbod Nov 12, 2024
ebce90d
Add a challenge
damienbod Nov 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@page
@model ErrorModel
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RazorPageOidc.Pages;

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RazorPageOidc.Pages;

[Authorize]
public class IndexModel : PageModel
{
public void OnGet()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@page
@model RazorPageOidc.Pages.LoginModel
@{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RazorPageOidc.Pages;

[AllowAnonymous]
public class LoginModel : PageModel
{
[BindProperty(SupportsGet = true)]
public string? ReturnUrl { get; set; }

public async Task OnGetAsync()
{
var properties = GetAuthProperties(ReturnUrl);
await HttpContext.ChallengeAsync(properties);
}

private static AuthenticationProperties GetAuthProperties(string? returnUrl)
{
const string pathBase = "/";

// Prevent open redirects.
if (string.IsNullOrEmpty(returnUrl))
{
returnUrl = pathBase;
}
else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative))
{
returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery;
}
else if (returnUrl[0] != '/')
{
returnUrl = $"{pathBase}{returnUrl}";
}

return new AuthenticationProperties { RedirectUri = returnUrl };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page
@model RazorPageOidc.Pages.LogoutModel
@{
ViewData["Title"] = "Logout";
}

<h1>Logout</h1>

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RazorPageOidc.Pages;

[Authorize]
public class LogoutModel : PageModel
{
public IActionResult OnGetAsync()
{
return SignOut(new AuthenticationProperties
{
RedirectUri = "/SignedOut"
},
// Clear auth cookie
CookieAuthenticationDefaults.AuthenticationScheme,
// Redirect to OIDC provider signout endpoint
OpenIdConnectDefaults.AuthenticationScheme);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
@using Microsoft.AspNetCore.Authorization
@inject IAuthorizationService AuthorizationService
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - RazorPageOidcClient</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-page="/Index">RazorPageOidcClient</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
@if (Context.User.Identity!.IsAuthenticated)
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Logout">Logout</a>
</li>

<span class="nav-link text-dark">Hi @Context.User.Identity.Name</span>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Login</a>
</li>
}

</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>

<footer class="border-top footer text-muted">
<div class="container">
&copy; 2024 - RazorPageOidcClient
</div>
</footer>

<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>

@RenderSection("Scripts", required: false)
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page
@model RazorPageOidc.Pages.SignedOutModel
@{
ViewData["Title"] = "SignedOut";
}

<h1>Signed out</h1>

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RazorPageOidc.Pages;

[AllowAnonymous]
public class SignedOutModel : PageModel
{
public void OnGet()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using RazorPageOidc
@namespace RazorPageOidc.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
// ........................................................................
// The OIDC handler must use a sign-in scheme capable of persisting
// user credentials across requests.

options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
// ........................................................................

// ........................................................................
// The "openid" and "profile" scopes are required for the OIDC handler
// and included by default.

//options.Scope.Add("some-scope");
// ........................................................................

// ........................................................................
// The following paths must match the redirect and post logout redirect
// paths configured when registering the application with the OIDC provider.
// Both the signin and signout paths must be registered as Redirect URIs.
// The default values are "/signin-oidc" and "/signout-callback-oidc".

//options.CallbackPath = new PathString("/signin-oidc");
//options.SignedOutCallbackPath = new PathString("/signout-callback-oidc");
// ........................................................................

// ........................................................................
// The RemoteSignOutPath is the "Front-channel logout URL" for remote single
// sign-out. The default value is "/signout-oidc".

//options.RemoteSignOutPath = new PathString("/signout-oidc");
// ........................................................................

var oidcConfig = builder.Configuration.GetSection("OpenIDConnectSettings");
// ........................................................................
// Authority is the OIDC provider's base URL. Set the application settings

options.Authority = oidcConfig["Authority"];
// ........................................................................

// ........................................................................
// Set the Client ID for the app. Set the application settings to
// the Client ID.

options.ClientId = oidcConfig["ClientId"];
// ........................................................................


options.ClientSecret = oidcConfig["ClientSecret"];

// ........................................................................
// Setting ResponseType to "code" configures the OIDC handler to use
// authorization code flow. The OIDC handler automatically requests the
// appropriate tokens using the code returned from the
// authorization endpoint.

options.ResponseType = OpenIdConnectResponseType.Code;
// ........................................................................

// ........................................................................
// Set MapInboundClaims to "false" to obtain the original claim types from
// the token. Many OIDC servers use "name" and "role"/"roles" rather than
// the SOAP/WS-Fed defaults in ClaimTypes. Adjust these values if your
// identity provider uses different claim types.

options.MapInboundClaims = false;
options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.RoleClaimType = "role";
// ........................................................................

options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
});

var requireAuthPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();

builder.Services.AddAuthorizationBuilder()
.SetFallbackPolicy(requireAuthPolicy);

builder.Services.AddRazorPages();

var app = builder.Build();

//IdentityModelEventSource.ShowPII = true;
JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
// Authorization is applied for middleware after the UseAuthorization method
app.UseAuthorization();
app.MapRazorPages();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.10" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Loading
Loading