Skip to content

Commit b64caf5

Browse files
authored
ASP.NET Core OpenID Connect: Configure OIDC web authentication (#33908)
* Initial structure * Link menu * Add sample * Update new authentication for OIDC * samples * Add links from the used standards * Add a reference link * Update sections * Add a link * Add some definitions * Add image * Update image * Add an Overview * Update docs * Update doc * Add customizations * Update sections * Add more data * Add third party information * Update texts * add background to image * clean up * remove file * Update text * Update text * Update text * text * fix line 80 * fix link * link change due to build line 15 * build test * Remove toc link due to build in dev branch * reset link, no change to build * fix links * Test add toc menu * Connect not connect * Improve image * fix small typos * typo * Update configuration * Update programs * Use FallbackPolicy instead of MVC options * UseAuthorization() comment * Clean up sample using feedback * code clean up * Update text based on PR feedback * 2 spaces * Update text * Update text * Improve text * text * Update sample * Update sample * Fix code example * spaces * spaces * spaces * PR feedback * Improve text * Update schemes doc * logout * rename claim mappings * Update code * Update link * Update link * fix link * logging * Update links * fix spacing * grammer * grammer * Add Login page * Add a challenge
1 parent 0ddfbb8 commit b64caf5

37 files changed

+40027
-0
lines changed

aspnetcore/security/authentication/configure-oidc-web-authentication.md

Lines changed: 377 additions & 0 deletions
Large diffs are not rendered by default.
61.2 KB
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page
2+
@model ErrorModel
3+
@{
4+
ViewData["Title"] = "Error";
5+
}
6+
7+
<h1 class="text-danger">Error.</h1>
8+
<h2 class="text-danger">An error occurred while processing your request.</h2>
9+
10+
@if (Model.ShowRequestId)
11+
{
12+
<p>
13+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
14+
</p>
15+
}
16+
17+
<h3>Development Mode</h3>
18+
<p>
19+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
20+
</p>
21+
<p>
22+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
23+
It can result in displaying sensitive information from exceptions to end users.
24+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
25+
and restarting the app.
26+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Diagnostics;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
5+
namespace RazorPageOidc.Pages;
6+
7+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8+
public class ErrorModel : PageModel
9+
{
10+
public string? RequestId { get; set; }
11+
12+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
13+
14+
public void OnGet()
15+
{
16+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@page
2+
@model IndexModel
3+
@{
4+
ViewData["Title"] = "Home page";
5+
}
6+
7+
<div class="text-center">
8+
<h1 class="display-4">Welcome</h1>
9+
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
10+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Mvc.RazorPages;
3+
4+
namespace RazorPageOidc.Pages;
5+
6+
[Authorize]
7+
public class IndexModel : PageModel
8+
{
9+
public void OnGet()
10+
{
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@page
2+
@model RazorPageOidc.Pages.LoginModel
3+
@{
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.AspNetCore.Mvc.RazorPages;
5+
6+
namespace RazorPageOidc.Pages;
7+
8+
[AllowAnonymous]
9+
public class LoginModel : PageModel
10+
{
11+
[BindProperty(SupportsGet = true)]
12+
public string? ReturnUrl { get; set; }
13+
14+
public async Task OnGetAsync()
15+
{
16+
var properties = GetAuthProperties(ReturnUrl);
17+
await HttpContext.ChallengeAsync(properties);
18+
}
19+
20+
private static AuthenticationProperties GetAuthProperties(string? returnUrl)
21+
{
22+
const string pathBase = "/";
23+
24+
// Prevent open redirects.
25+
if (string.IsNullOrEmpty(returnUrl))
26+
{
27+
returnUrl = pathBase;
28+
}
29+
else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative))
30+
{
31+
returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery;
32+
}
33+
else if (returnUrl[0] != '/')
34+
{
35+
returnUrl = $"{pathBase}{returnUrl}";
36+
}
37+
38+
return new AuthenticationProperties { RedirectUri = returnUrl };
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page
2+
@model RazorPageOidc.Pages.LogoutModel
3+
@{
4+
ViewData["Title"] = "Logout";
5+
}
6+
7+
<h1>Logout</h1>
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Authentication.Cookies;
3+
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Mvc.RazorPages;
7+
8+
namespace RazorPageOidc.Pages;
9+
10+
[Authorize]
11+
public class LogoutModel : PageModel
12+
{
13+
public IActionResult OnGetAsync()
14+
{
15+
return SignOut(new AuthenticationProperties
16+
{
17+
RedirectUri = "/SignedOut"
18+
},
19+
// Clear auth cookie
20+
CookieAuthenticationDefaults.AuthenticationScheme,
21+
// Redirect to OIDC provider signout endpoint
22+
OpenIdConnectDefaults.AuthenticationScheme);
23+
}
24+
}

0 commit comments

Comments
 (0)