|
|
@ -2,26 +2,23 @@ |
|
|
|
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
|
|
|
|
|
|
|
|
|
|
|
using Identity.API.Models; |
|
|
|
using Identity.API.Models.AccountViewModels; |
|
|
|
using Identity.API.Services; |
|
|
|
using IdentityModel; |
|
|
|
using IdentityServer4.Quickstart.UI.Models; |
|
|
|
using IdentityServer4.Models; |
|
|
|
using IdentityServer4.Services; |
|
|
|
using Microsoft.AspNetCore.Http.Authentication; |
|
|
|
using IdentityServer4.Stores; |
|
|
|
using Microsoft.AspNetCore.Authentication; |
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
using Microsoft.AspNetCore.Identity; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Security.Claims; |
|
|
|
using System.Text.Encodings.Web; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using IdentityServer4.Models; |
|
|
|
using IdentityServer4.Stores; |
|
|
|
using Identity.API.Services; |
|
|
|
using Identity.API.Models; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
using Identity.API.Models.AccountViewModels; |
|
|
|
using Microsoft.AspNetCore.Identity; |
|
|
|
using Microsoft.AspNetCore.Authentication; |
|
|
|
|
|
|
|
namespace IdentityServer4.Quickstart.UI.Controllers |
|
|
|
{ |
|
|
@ -36,7 +33,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers |
|
|
|
private readonly ILoginService<ApplicationUser> _loginService; |
|
|
|
private readonly IIdentityServerInteractionService _interaction; |
|
|
|
private readonly IClientStore _clientStore; |
|
|
|
private readonly ILogger _logger; |
|
|
|
private readonly ILogger<AccountController> _logger; |
|
|
|
private readonly UserManager<ApplicationUser> _userManager; |
|
|
|
|
|
|
|
public AccountController( |
|
|
@ -45,13 +42,13 @@ namespace IdentityServer4.Quickstart.UI.Controllers |
|
|
|
ILoginService<ApplicationUser> loginService, |
|
|
|
IIdentityServerInteractionService interaction, |
|
|
|
IClientStore clientStore, |
|
|
|
ILoggerFactory loggerFactory, |
|
|
|
ILogger<AccountController> logger, |
|
|
|
UserManager<ApplicationUser> userManager) |
|
|
|
{ |
|
|
|
_loginService = loginService; |
|
|
|
_interaction = interaction; |
|
|
|
_clientStore = clientStore; |
|
|
|
_logger = loggerFactory.CreateLogger<AccountController>(); |
|
|
|
_logger = logger; |
|
|
|
_userManager = userManager; |
|
|
|
} |
|
|
|
|
|
|
@ -69,6 +66,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers |
|
|
|
} |
|
|
|
|
|
|
|
var vm = await BuildLoginViewModelAsync(returnUrl, context); |
|
|
|
|
|
|
|
ViewData["ReturnUrl"] = returnUrl; |
|
|
|
|
|
|
|
return View(vm); |
|
|
@ -97,6 +95,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers |
|
|
|
}; |
|
|
|
|
|
|
|
await _loginService.SignIn(user); |
|
|
|
|
|
|
|
// make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
|
|
|
|
if (_interaction.IsValidReturnUrl(model.ReturnUrl)) |
|
|
|
{ |
|
|
@ -111,7 +110,9 @@ namespace IdentityServer4.Quickstart.UI.Controllers |
|
|
|
|
|
|
|
// something went wrong, show form with error
|
|
|
|
var vm = await BuildLoginViewModelAsync(model); |
|
|
|
|
|
|
|
ViewData["ReturnUrl"] = model.ReturnUrl; |
|
|
|
|
|
|
|
return View(vm); |
|
|
|
} |
|
|
|
|
|
|
@ -180,6 +181,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers |
|
|
|
public async Task<IActionResult> Logout(LogoutViewModel model) |
|
|
|
{ |
|
|
|
var idp = User?.FindFirst(JwtClaimTypes.IdentityProvider)?.Value; |
|
|
|
|
|
|
|
if (idp != null && idp != IdentityServerConstants.LocalIdentityProvider) |
|
|
|
{ |
|
|
|
if (model.LogoutId == null) |
|
|
@ -191,10 +193,15 @@ namespace IdentityServer4.Quickstart.UI.Controllers |
|
|
|
} |
|
|
|
|
|
|
|
string url = "/Account/Logout?logoutId=" + model.LogoutId; |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
|
|
|
|
// hack: try/catch to handle social providers that throw
|
|
|
|
await HttpContext.Authentication.SignOutAsync(idp, new AuthenticationProperties { RedirectUri = url }); |
|
|
|
await HttpContext.SignOutAsync(idp, new AuthenticationProperties |
|
|
|
{ |
|
|
|
RedirectUri = url |
|
|
|
}); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
@ -203,7 +210,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers |
|
|
|
} |
|
|
|
|
|
|
|
// delete authentication cookie
|
|
|
|
await HttpContext.Authentication.SignOutAsync(); |
|
|
|
await HttpContext.SignOutAsync(); |
|
|
|
|
|
|
|
// set this so UI rendering sees an anonymous user
|
|
|
|
HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity()); |
|
|
@ -217,7 +224,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers |
|
|
|
public async Task<IActionResult> DeviceLogOut(string redirectUrl) |
|
|
|
{ |
|
|
|
// delete authentication cookie
|
|
|
|
await HttpContext.Authentication.SignOutAsync(); |
|
|
|
await HttpContext.SignOutAsync(); |
|
|
|
|
|
|
|
// set this so UI rendering sees an anonymous user
|
|
|
|
HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity()); |
|
|
|