Skip to content

Commit ebce90d

Browse files
committed
Add a challenge
1 parent f05ca90 commit ebce90d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,56 @@ public class SignedOutModel : PageModel
179179
}
180180
```
181181

182+
### Implement Login Page
183+
184+
A Login Razor Page can also be implemented to call the **ChallengeAsync** directly with the required AuthProperties. This is not required if the whole web application requires authentication and the default Challenge is used.
185+
186+
The `login.cshtml` requires the AllowAnonymous attribute.
187+
188+
```
189+
using Microsoft.AspNetCore.Authentication;
190+
using Microsoft.AspNetCore.Authorization;
191+
using Microsoft.AspNetCore.Mvc;
192+
using Microsoft.AspNetCore.Mvc.RazorPages;
193+
194+
namespace RazorPageOidc.Pages;
195+
196+
[AllowAnonymous]
197+
public class LoginModel : PageModel
198+
{
199+
[BindProperty(SupportsGet = true)]
200+
public string? ReturnUrl { get; set; }
201+
202+
public async Task OnGetAsync()
203+
{
204+
var properties = GetAuthProperties(ReturnUrl);
205+
await HttpContext.ChallengeAsync(properties);
206+
}
207+
208+
private static AuthenticationProperties GetAuthProperties(string? returnUrl)
209+
{
210+
const string pathBase = "/";
211+
212+
// Prevent open redirects.
213+
if (string.IsNullOrEmpty(returnUrl))
214+
{
215+
returnUrl = pathBase;
216+
}
217+
else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative))
218+
{
219+
returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery;
220+
}
221+
else if (returnUrl[0] != '/')
222+
{
223+
returnUrl = $"{pathBase}{returnUrl}";
224+
}
225+
226+
return new AuthenticationProperties { RedirectUri = returnUrl };
227+
}
228+
}
229+
230+
```
231+
182232
### Add a login, logout button for the user.
183233

184234
```

0 commit comments

Comments
 (0)