-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIndex.cshtml.cs
58 lines (47 loc) · 2.06 KB
/
Index.cshtml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using Logto.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Text.Json;
namespace sample.Pages;
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public async Task OnGetAsync()
{
var logtoOptions = HttpContext.GetLogtoOptions();
ViewData["Resource"] = logtoOptions.Resource;
ViewData["AccessTokenForResource"] = await HttpContext.GetTokenAsync(LogtoParameters.Tokens.AccessTokenForResource);
}
public async Task OnPostSignInAsync()
{
var authProperties = new AuthenticationProperties
{
RedirectUri = "/"
};
/// <see href="https://docs.logto.io/docs/references/openid-connect/authentication-parameters/#first-screen"/>
/// <see cref="LogtoParameters.Authentication.FirstScreen"/>
authProperties.SetParameter("first_screen", LogtoParameters.Authentication.FirstScreen.Register);
// This parameter MUST be used together with `first_screen`
authProperties.SetParameter("identifiers", string.Join(",", new[]
{
LogtoParameters.Authentication.Identifiers.Username,
}));
var directSignIn = new LogtoParameters.Authentication.DirectSignIn
{
Target = "github",
Method = LogtoParameters.Authentication.DirectSignIn.Methods.Social
};
/// <see href="https://docs.logto.io/docs/references/openid-connect/authentication-parameters/#direct-sign-in"/>
/// <see cref="LogtoParameters.Authentication.DirectSignIn"/>
authProperties.SetParameter("direct_sign_in", JsonSerializer.Serialize(directSignIn));
await HttpContext.ChallengeAsync(authProperties);
}
public async Task OnPostSignOutAsync()
{
await HttpContext.SignOutAsync(new AuthenticationProperties { RedirectUri = "/" });
}
}