1. 添加Controller:
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace TestClients.BlazorApplication.Controllers
{
[Authorize]
[Route("/Logout")]
public class LogoutController : Controller
{
public AuthorizeHelper AuthHelper { get; }
public LogoutController(AuthorizeHelper AuthHelper)
{
this.AuthHelper = AuthHelper;
}
[HttpGet]
public IActionResult Index()
{
if(AuthHelper.Config != null && AuthHelper.Config.CookiesName != null && AuthHelper.Config.OidcName != null)
{
return new SignOutResult(new List<string> { AuthHelper.Config.CookiesName, AuthHelper.Config.OidcName });
}
return new SignOutResult(new List<string> { CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme});
}
}
}
2. razor 页面:
<a href="Logout" >Logout</a>