小编Cri*_*les的帖子

使用 Asp.Net Core Cookie 身份验证的注销操作

我在 Asp.Net Core 2.2 中实现了身份验证,如下所示:

public async Task<IActionResult> LoginAsync(string user, string password)
    {
        if (user == "admin" && password == "admin")
        {
            var claims = new[] { new Claim(ClaimTypes.Name, user),
            new Claim(ClaimTypes.Role, "Admin") };

            var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(identity));

            return RedirectToAction("Index", "Home");
        {
        else
        {
            return RedirectToAction("Login", "Users");
        }
Run Code Online (Sandbox Code Playgroud)

我现在需要执行注销操作。我曾经使用 FormsAuthentication.SignOut() 在 Asp.Net MVC 中实现此目的...我需要知道在 Asp.Net Core 2.2 中执行此操作的正确方法

我尝试过执行如下注销操作:

    public async Task<IActionResult> Logout()
    {
        await HttpContext.SignOutAsync();
        return RedirectToAction("Index","Home");
    }
Run Code Online (Sandbox Code Playgroud)

并在我的导航栏中使用了以下代码:

@if (User.Identity.IsAuthenticated)
            { …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-core

9
推荐指数
1
解决办法
3万
查看次数

标签 统计

asp.net ×1

asp.net-core ×1

c# ×1