-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHomeController.cs
More file actions
67 lines (55 loc) · 2.38 KB
/
HomeController.cs
File metadata and controls
67 lines (55 loc) · 2.38 KB
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
59
60
61
62
63
64
65
66
67
using Logto.AspNetCore.Authentication;
using System.Diagnostics;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using sample_mvc.Models;
using System.Text.Json;
namespace sample_mvc.Controllers;
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public async Task<IActionResult> Index()
{
var logtoOptions = HttpContext.GetLogtoOptions();
ViewData["Resource"] = logtoOptions.Resource;
ViewData["AccessTokenForResource"] = await HttpContext.GetTokenAsync(LogtoParameters.Tokens.AccessTokenForResource);
return View();
}
public IActionResult SignIn()
{
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);
authProperties.SetParameter("identifiers", JsonSerializer.Serialize(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));
return Challenge(authProperties);
}
// Use the `new` keyword to avoid conflict with the `ControllerBase.SignOut` method
new public IActionResult SignOut()
{
return SignOut(new AuthenticationProperties { RedirectUri = "/" });
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}