-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathAccountController.cs
More file actions
134 lines (117 loc) · 5.34 KB
/
Copy pathAccountController.cs
File metadata and controls
134 lines (117 loc) · 5.34 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Auth0.AspNetCore.Authentication.IntegrationTests.Controllers
{
public class AccountController : Controller
{
public async Task Login(
string returnUrl = "/",
string scope = null,
[FromQuery(Name = "extraParameters")] Dictionary<string, string> extraParameters = null,
string organization = null,
string invitation = null,
string audience = null,
string scheme = null)
{
var authenticationPropertiesBuilder = new LoginAuthenticationPropertiesBuilder().WithRedirectUri(returnUrl);
if (!string.IsNullOrWhiteSpace(scope))
{
authenticationPropertiesBuilder = authenticationPropertiesBuilder.WithScope(scope);
}
if (extraParameters != null)
{
foreach (KeyValuePair<string, string> entry in extraParameters)
{
authenticationPropertiesBuilder = authenticationPropertiesBuilder.WithParameter(entry.Key, entry.Value);
}
}
if (!string.IsNullOrWhiteSpace(organization))
{
authenticationPropertiesBuilder = authenticationPropertiesBuilder.WithOrganization(organization);
}
if (!string.IsNullOrWhiteSpace(invitation))
{
authenticationPropertiesBuilder = authenticationPropertiesBuilder.WithInvitation(invitation);
}
if (!string.IsNullOrWhiteSpace(audience))
{
authenticationPropertiesBuilder = authenticationPropertiesBuilder.WithAudience(audience);
}
var authenticationProperties = authenticationPropertiesBuilder.Build();
await HttpContext.ChallengeAsync(scheme ?? Auth0Constants.AuthenticationScheme, authenticationProperties);
}
[Authorize]
public async Task Logout([FromQuery(Name = "extraParameters")] Dictionary<string, string> extraParameters = null, [FromQuery(Name = "cookieAuthenticationScheme")] string cookieAuthenticationScheme = null)
{
// Indicate here where Auth0 should redirect the user after a logout.
// Note that the resulting absolute Uri must be whitelisted in the
// **Allowed Logout URLs** settings for the client.
var authenticationPropertiesBuilder = new LogoutAuthenticationPropertiesBuilder().WithRedirectUri(Url.Action("Index", "Home"));
if (extraParameters != null)
{
foreach (KeyValuePair<string, string> entry in extraParameters)
{
authenticationPropertiesBuilder = authenticationPropertiesBuilder.WithParameter(entry.Key, entry.Value);
}
}
await HttpContext.SignOutAsync(Auth0Constants.AuthenticationScheme, authenticationPropertiesBuilder.Build());
await HttpContext.SignOutAsync(cookieAuthenticationScheme ?? CookieAuthenticationDefaults.AuthenticationScheme);
}
[Authorize]
public IActionResult Profile()
{
return View(new
{
Name = User.Identity.Name,
EmailAddress = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value,
ProfileImage = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value
});
}
/// <summary>
/// This is just a helper action to enable you to easily see all claims related to a user. It helps when debugging your
/// application to see the in claims populated from the Auth0 ID Token
/// </summary>
/// <returns></returns>
[Authorize]
public IActionResult Claims()
{
return Content("Hello from claims");
}
public IActionResult AccessDenied()
{
return View();
}
public IActionResult Tokens()
{
var authItems = HttpContext.Features.Get<IAuthenticateResultFeature>()?.AuthenticateResult?.Properties?.Items;
if (authItems == null) return BadRequest("Error with authentication result object.");
if (authItems.ContainsKey(".Token.access_token")
&& authItems.ContainsKey(".Token.refresh_token")
&& authItems.ContainsKey(".Token.id_token"))
return Ok($"TokensExist=True");
else
return Ok($"TokensExist=False");
}
private Dictionary<string, string> ObjectToDictionary(object values)
{
if (values is Dictionary<string, string> dictionary)
return dictionary;
dictionary = new Dictionary<string, string>();
if (values != null)
foreach (var prop in values.GetType().GetRuntimeProperties())
{
var value = prop.GetValue(values) as string;
if (!string.IsNullOrEmpty(value))
dictionary.Add(prop.Name, value);
}
return dictionary;
}
}
}