Skip to content

Commit f05ca90

Browse files
committed
Add Login page
1 parent ef96027 commit f05ca90

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
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+
}

0 commit comments

Comments
 (0)