Feature/admin (#831)
* fix redirect to login for admin page fix logout * add admin part url Co-authored-by: cedri <cedri@BAS>
This commit is contained in:
@@ -63,7 +63,7 @@ public class CustomAuthStateProvider : AuthenticationStateProvider
|
||||
|
||||
if (user == null || !user.IsAuthenticated)
|
||||
{
|
||||
return null;
|
||||
return new ClaimsPrincipal(new ClaimsIdentity());
|
||||
}
|
||||
|
||||
var identity = new ClaimsIdentity(
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await HttpClient.PostAsync("Identity/Account/Logout", null);
|
||||
await HttpClient.PostAsync("User/Logout", null);
|
||||
await new Route(JSRuntime).RouteOutside("/Identity/Account/Login");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
@inject NavigationManager Navigation
|
||||
@using System.Web;
|
||||
|
||||
@inject NavigationManager Navigation
|
||||
@inject IJSRuntime JsRuntime
|
||||
|
||||
@code {
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
Navigation.NavigateTo($"Identity/Account/Login?returnUrl=" +
|
||||
$"/{Uri.EscapeDataString(Navigation.ToBaseRelativePath(Navigation.Uri))}");
|
||||
{
|
||||
var returnUrl = HttpUtility.UrlEncode($"/{Uri.EscapeDataString(Navigation.ToBaseRelativePath(Navigation.Uri))}");
|
||||
JsRuntime.InvokeVoidAsync("location.replace", $"Identity/Account/Login?returnUrl={returnUrl}");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
@@ -10,7 +7,6 @@ using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.eShopWeb.Infrastructure.Identity;
|
||||
using Microsoft.eShopWeb.Web.Configuration;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Microsoft.eShopWeb.Web.Areas.Identity.Pages.Account;
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Claims;
|
||||
using BlazorShared.Authorization;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.Infrastructure.Identity;
|
||||
using Microsoft.eShopWeb.Web.Configuration;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace Microsoft.eShopWeb.Web.Controllers;
|
||||
|
||||
@@ -14,10 +17,19 @@ namespace Microsoft.eShopWeb.Web.Controllers;
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private readonly ITokenClaimsService _tokenClaimsService;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly ILogger<UserController> _logger;
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
public UserController(ITokenClaimsService tokenClaimsService)
|
||||
public UserController(ITokenClaimsService tokenClaimsService,
|
||||
SignInManager<ApplicationUser> signInManager,
|
||||
ILogger<UserController> logger,
|
||||
IMemoryCache cache)
|
||||
{
|
||||
_tokenClaimsService = tokenClaimsService;
|
||||
_signInManager = signInManager;
|
||||
_logger = logger;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -26,6 +38,25 @@ public class UserController : ControllerBase
|
||||
public async Task<IActionResult> GetCurrentUser() =>
|
||||
Ok(await CreateUserInfo(User));
|
||||
|
||||
[Route("Logout")]
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> Logout()
|
||||
{
|
||||
await _signInManager.SignOutAsync();
|
||||
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
var userId = _signInManager.Context.User.Claims.First(c => c.Type == ClaimTypes.Name);
|
||||
var identityKey = _signInManager.Context.Request.Cookies[ConfigureCookieSettings.IdentifierCookieName];
|
||||
_cache.Set($"{userId.Value}:{identityKey}", identityKey, new MemoryCacheEntryOptions
|
||||
{
|
||||
AbsoluteExpiration = DateTime.Now.AddMinutes(ConfigureCookieSettings.ValidityMinutesPeriod)
|
||||
});
|
||||
|
||||
_logger.LogInformation("User logged out.");
|
||||
return Ok();
|
||||
}
|
||||
|
||||
private async Task<UserInfo> CreateUserInfo(ClaimsPrincipal claimsPrincipal)
|
||||
{
|
||||
if (claimsPrincipal.Identity == null || claimsPrincipal.Identity.Name == null || !claimsPrincipal.Identity.IsAuthenticated)
|
||||
|
||||
Reference in New Issue
Block a user