-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a login page as a demo for the challenge
- Loading branch information
Showing
3 changed files
with
60 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@page | ||
@model WebCodeFlowPkceClient.Pages.LoginModel | ||
@{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 WebCodeFlowPkceClient.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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters