51 lines
2.0 KiB
C#
51 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Microsoft.eShopOnContainers.WebMVC.Controllers
|
|
{
|
|
[Authorize(AuthenticationSchemes = "OpenIdConnect")]
|
|
public class AccountController : Controller
|
|
{
|
|
private readonly ILogger<AccountController> _logger;
|
|
|
|
public AccountController(ILogger<AccountController> logger)
|
|
{
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
[Authorize(AuthenticationSchemes = "OpenIdConnect")] public async Task<IActionResult> SignIn(string returnUrl)
|
|
{
|
|
var user = User as ClaimsPrincipal;
|
|
var token = await HttpContext.GetTokenAsync("access_token");
|
|
|
|
if (token != null)
|
|
{
|
|
ViewData["access_token"] = token;
|
|
}
|
|
|
|
// "Catalog" because UrlHelper doesn't support nameof() for controllers
|
|
// https://github.com/aspnet/Mvc/issues/5853
|
|
return RedirectToAction(nameof(CatalogController.Index), "Catalog");
|
|
}
|
|
|
|
public async Task<IActionResult> Signout()
|
|
{
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
|
|
|
|
// "Catalog" because UrlHelper doesn't support nameof() for controllers
|
|
// https://github.com/aspnet/Mvc/issues/5853
|
|
var homeUrl = Url.Action(nameof(CatalogController.Index), "Catalog");
|
|
return new SignOutResult(OpenIdConnectDefaults.AuthenticationScheme,
|
|
new AspNetCore.Authentication.AuthenticationProperties { RedirectUri = homeUrl });
|
|
}
|
|
}
|
|
}
|