Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,86 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace Auth0.AspNetCore.Authentication.Playground.Controllers
namespace Auth0.AspNetCore.Authentication.Playground.Controllers;

public class AccountController : Controller
{

public class AccountController : Controller
public async Task Login(string returnUrl = "/")
{
public async Task Login(string returnUrl = "/")
{
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(returnUrl)
.Build();
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(returnUrl)
.Build();

await HttpContext.ChallengeAsync(PlaygroundConstants.AuthenticationScheme, authenticationProperties);
}
await HttpContext.ChallengeAsync(PlaygroundConstants.AuthenticationScheme, authenticationProperties);
}

public async Task Login2(string returnUrl = "/")
{
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(returnUrl)
.Build();
public async Task Login2(string returnUrl = "/")
{
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(returnUrl)
.Build();

await HttpContext.ChallengeAsync(PlaygroundConstants.AuthenticationScheme2, authenticationProperties);
}
await HttpContext.ChallengeAsync(PlaygroundConstants.AuthenticationScheme2, authenticationProperties);
}

[Authorize]
public async Task Logout()
{
// 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 authenticationProperties = new LogoutAuthenticationPropertiesBuilder()
.WithRedirectUri(Url.Action("Index", "Home"))
.Build();
[Authorize]
public async Task Logout()
{
// 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 authenticationProperties = new LogoutAuthenticationPropertiesBuilder()
.WithRedirectUri(Url.Action("Index", "Home"))
.Build();

await HttpContext.SignOutAsync(PlaygroundConstants.AuthenticationScheme, authenticationProperties);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
await HttpContext.SignOutAsync(PlaygroundConstants.AuthenticationScheme, authenticationProperties);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}

[Authorize]
public async Task Logout2()
{
// 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 authenticationProperties = new LogoutAuthenticationPropertiesBuilder()
.WithRedirectUri(Url.Action("Index", "Home"))
.Build();
[Authorize]
public async Task Logout2()
{
// 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 authenticationProperties = new LogoutAuthenticationPropertiesBuilder()
.WithRedirectUri(Url.Action("Index", "Home"))
.Build();

await HttpContext.SignOutAsync(PlaygroundConstants.AuthenticationScheme2, authenticationProperties);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
await HttpContext.SignOutAsync(PlaygroundConstants.AuthenticationScheme2, authenticationProperties);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}

[Authorize]
public async Task<IActionResult> Profile()
[Authorize]
public async Task<IActionResult> Profile()
{
var accessToken = await HttpContext.GetTokenAsync("access_token");
var idToken = await HttpContext.GetTokenAsync("id_token");
var refreshToken = await HttpContext.GetTokenAsync("refresh_token");
return View(new UserProfileViewModel()
{
var accessToken = await HttpContext.GetTokenAsync("access_token");
var idToken = await HttpContext.GetTokenAsync("id_token");
var refreshToken = await HttpContext.GetTokenAsync("refresh_token");
return View(new UserProfileViewModel()
{
Name = User.Identity.Name,
EmailAddress = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value,
ProfileImage = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value
});
}
Name = User.Identity.Name,
EmailAddress = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value,
ProfileImage = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value,
AccessToken = accessToken,
IdToken = idToken,
RefreshToken = refreshToken
});
}

/// <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 View();
}
/// <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 View();
}

public IActionResult AccessDenied()
{
return View();
}
public IActionResult AccessDenied()
{
return View();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace Auth0.AspNetCore.Authentication.Playground.Controllers
namespace Auth0.AspNetCore.Authentication.Playground.Controllers;

public class HomeController : Controller
{
public class HomeController : Controller
public IActionResult Index()
{
public IActionResult Index()
{
return View();
}
return View();
}

[Authorize(Roles = "Admin")]
public IActionResult Admin()
{
return View();
}
[Authorize(Roles = "Admin")]
public IActionResult Admin()
{
return View();
}

public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
using System.Threading.Tasks;
using Auth0.AspNetCore.Authentication.BackchannelLogout;

namespace Auth0.AspNetCore.Authentication.Playground
namespace Auth0.AspNetCore.Authentication.Playground;

public class CustomClearSessionLogoutTokenHandler : ILogoutTokenHandler
{
public class CustomClearSessionLogoutTokenHandler : ILogoutTokenHandler
{
private readonly ITicketStore store;
private readonly ITicketStore store;

public CustomClearSessionLogoutTokenHandler(ITicketStore store)
{
this.store = store;
}
public CustomClearSessionLogoutTokenHandler(ITicketStore store)
{
this.store = store;
}

public async Task OnTokenReceivedAsync(string issuer, string sid, string logoutToken, TimeSpan expiration)
{
await store.RemoveAsync(sid);
}
public async Task OnTokenReceivedAsync(string issuer, string sid, string logoutToken, TimeSpan expiration)
{
await store.RemoveAsync(sid);
}

public Task<bool> IsLoggedOutAsync(string issuer, string sid)
{
return Task.FromResult(false);
}
public Task<bool> IsLoggedOutAsync(string issuer, string sid)
{
return Task.FromResult(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@
using System.Threading.Tasks;
using Auth0.AspNetCore.Authentication.BackchannelLogout;

namespace Auth0.AspNetCore.Authentication.Playground
{
public class CustomDistributedLogoutTokenHandler : ILogoutTokenHandler
{
private readonly IDistributedCache _cache;
namespace Auth0.AspNetCore.Authentication.Playground;

public CustomDistributedLogoutTokenHandler(IDistributedCache cache)
{
_cache = cache;
}
public class CustomDistributedLogoutTokenHandler : ILogoutTokenHandler
{
private readonly IDistributedCache _cache;

public async Task OnTokenReceivedAsync(string issuer, string sid, string logoutToken, TimeSpan expiration)
{
await _cache.SetAsync($"{issuer}|{sid}", Encoding.ASCII.GetBytes(logoutToken), new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = expiration
});
}
public CustomDistributedLogoutTokenHandler(IDistributedCache cache)
{
_cache = cache;
}

public async Task<bool> IsLoggedOutAsync(string issuer, string sid)
public async Task OnTokenReceivedAsync(string issuer, string sid, string logoutToken, TimeSpan expiration)
{
await _cache.SetAsync($"{issuer}|{sid}", Encoding.ASCII.GetBytes(logoutToken), new DistributedCacheEntryOptions
{
var token = await _cache.GetAsync($"{issuer}|{sid}");
return token != null;
}
AbsoluteExpirationRelativeToNow = expiration
});
}

}
public async Task<bool> IsLoggedOutAsync(string issuer, string sid)
{
var token = await _cache.GetAsync($"{issuer}|{sid}");
return token != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,45 @@
using System.Linq;
using System.Threading.Tasks;

namespace Auth0.AspNetCore.Authentication.Playground
namespace Auth0.AspNetCore.Authentication.Playground;

public class CustomInMemoryTicketStore : ITicketStore
{
public class CustomInMemoryTicketStore : ITicketStore
{
private readonly IMemoryCache _cache;
private readonly IMemoryCache _cache;

public CustomInMemoryTicketStore(IMemoryCache cache)
{
_cache = cache;
}
public CustomInMemoryTicketStore(IMemoryCache cache)
{
_cache = cache;
}

public Task RemoveAsync(string key)
{
_cache.Remove(key);
public Task RemoveAsync(string key)
{
_cache.Remove(key);

return Task.CompletedTask;
}
return Task.CompletedTask;
}

public Task<AuthenticationTicket> RetrieveAsync(string key)
{
var ticket = _cache.Get<AuthenticationTicket>(key);
public Task<AuthenticationTicket> RetrieveAsync(string key)
{
var ticket = _cache.Get<AuthenticationTicket>(key);

return Task.FromResult(ticket);
}
return Task.FromResult(ticket);
}

public Task RenewAsync(string key, AuthenticationTicket ticket)
{
_cache.Set(key, ticket);
public Task RenewAsync(string key, AuthenticationTicket ticket)
{
_cache.Set(key, ticket);

return Task.CompletedTask;
}
return Task.CompletedTask;
}

public Task<string> StoreAsync(AuthenticationTicket ticket)
{
var key = ticket.Principal.Claims
.First(c => c.Type == "sid").Value;
public Task<string> StoreAsync(AuthenticationTicket ticket)
{
var key = ticket.Principal.Claims
.First(c => c.Type == "sid").Value;

_cache.Set(key, ticket);
_cache.Set(key, ticket);

return Task.FromResult(key);
}
return Task.FromResult(key);
}
}
}
Loading
Loading