C# 使用asp.net核心cookie身份验证注销后防止自动windows身份验证登录

C# 使用asp.net核心cookie身份验证注销后防止自动windows身份验证登录,c#,asp.net-core,windows-authentication,cookie-authentication,C#,Asp.net Core,Windows Authentication,Cookie Authentication,我正在尝试使用cookie身份验证作为默认方案来计算和缓存windows用户的声明。下面是我为此目的使用的帐户控制器 [Route("account"), AllowAnonymous] public class AccountController : Controller { [HttpGet, Route("login")] public async Task<IActionResult> Login(string returnUrl) {

我正在尝试使用cookie身份验证作为默认方案来计算和缓存windows用户的声明。下面是我为此目的使用的帐户控制器

[Route("account"), AllowAnonymous]
public class AccountController : Controller
{

    [HttpGet, Route("login")]
    public async Task<IActionResult> Login(string returnUrl)
    {
        var windowsAuthenticationScheme = Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme;

        var result = await HttpContext.AuthenticateAsync(windowsAuthenticationScheme);
        if (result?.Principal is WindowsPrincipal wp)
        {
            var id = new ClaimsIdentity(windowsAuthenticationScheme);
            // add claims

            await HttpContext.SignInAsync(new ClaimsPrincipal(id));
            return Redirect(returnUrl);
        }

        return Challenge(windowsAuthenticationScheme);
    }

    [HttpPost, Route("logout")]
    public async Task Logout()
    {
        await HttpContext.SignOutAsync();
        // both should have similar effect as cookie authentication is the default scheme
        // await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    }
}
[路由(“帐户”),不可任意使用]
公共类AccountController:控制器
{
[HttpGet,路由(“登录”)]
公共异步任务登录(字符串返回URL)
{
var windowsAuthenticationScheme=Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme;
var result=wait HttpContext.authenticateSync(windowsAuthenticationScheme);
如果(结果?.Principal为WindowsPrincipal wp)
{
var id=新的索赔实体(windowsAuthenticationScheme);
//添加索赔
等待HttpContext.SignInAsync(新的ClaimsPrincipal(id));
返回重定向(returnUrl);
}
返回质询(windowsAuthenticationScheme);
}
[HttpPost,路由(“注销”)]
公共异步任务注销()
{
等待HttpContext.SignOutAsync();
//两者都应该具有类似的效果,因为cookie身份验证是默认方案
//等待HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
}
这在登录时非常有效。然而,签出过程似乎并不十分有效。在我从浏览器为路由
/account/logout
生成HTTP POST后,浏览器中的cookie被正确清除


但是,在刷新时,它会自动对用户进行身份验证,而不会命中登录端点。使用
HttpContext.Response.Cookies.Delete(“.AspNetCore.Cookies”)显式删除cookie也没有帮助。我做错了什么

Windows身份验证的工作方式不同:它是在服务器中很早就实施的,应用程序实际上无法控制它(这就是为什么您在IIS中启用它,并且它可以正常工作的原因)。使用Windows身份验证,每次请求都会将凭据传递给应用程序。您无法真正注销它。遗憾的是,它在应用程序级别仍然不可能。“仍然不可能”-它无法在应用程序级别工作,因为它必须在网络堆栈中更早地发生。当请求到达应用程序时,身份验证必须已经发生。Windows身份验证的工作方式不同:它是在服务器的早期强制执行的,应用程序实际上无法控制它(这就是为什么您在IIS中启用它并且它可以正常工作的原因)。使用Windows身份验证,每次请求都会将凭据传递给应用程序。您无法真正注销它。遗憾的是,它在应用程序级别仍然不可能。“仍然不可能”-它无法在应用程序级别工作,因为它必须在网络堆栈中更早地发生。当请求到达应用程序时,身份验证必须已经发生。