C# OWIN注销不';不要删除cookie

C# OWIN注销不';不要删除cookie,c#,authentication,cookies,asp.net-identity,owin,C#,Authentication,Cookies,Asp.net Identity,Owin,我在一个外部身份验证服务器中使用OWIN中间件,我的应用程序使用OAuth授权代码授权流进行身份验证 我可以重定向到身份验证服务器,针对外部提供商(Google)进行身份验证,并重定向回我的客户端应用程序,登录的用户和应用程序Cookie设置得很好,但是当我尝试注销时,调用AuthenticationManager.SignOut方法后Cookie仍然存在 Startup.Auth.cs中的My cookie选项包括: var cookieOptions = new CookieAuthenti

我在一个外部身份验证服务器中使用OWIN中间件,我的应用程序使用OAuth授权代码授权流进行身份验证

我可以重定向到身份验证服务器,针对外部提供商(Google)进行身份验证,并重定向回我的客户端应用程序,登录的用户和应用程序Cookie设置得很好,但是当我尝试注销时,调用
AuthenticationManager.SignOut
方法后Cookie仍然存在

Startup.Auth.cs
中的My cookie选项包括:

var cookieOptions = new CookieAuthenticationOptions
                    {
                        Provider = cookieProvider,
                        AuthenticationType = "Application",
                        AuthenticationMode = AuthenticationMode.Passive,
                        LoginPath = new PathString("/Account/Index"),
                        LogoutPath = new PathString("/Account/Logout"),
                        SlidingExpiration = true,
                        ExpireTimeSpan = TimeSpan.FromMinutes(30),
                    };
app.UseCookieAuthentication(cookieOptions);
app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ExternalCookie);
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
我的登录方式:

var loginInfo = await AuthManager.GetExternalLoginInfoAsync();
SignInManager.ExternalSignInAsync(loginInfo, true);
var identity = AuthManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie).Result.Identity;

if (identity != null)
{
    AuthManager.SignIn(
                  new AuthenticationProperties {IsPersistent = true},
                  new ClaimsIdentity(identity.Claims, "Application", identity.NameClaimType, identity.RoleClaimType));

        var ticket = AuthManager.AuthenticateAsync("Application").Result;
        var identity = ticket != null ? ticket.Identity : null;
        if (identity == null)
        {
            AuthManager.Challenge("Application");
            return new HttpUnauthorizedResult();
        }

        identity = new ClaimsIdentity(identity.Claims, "Bearer", identity.NameClaimType, identity.RoleClaimType);
        AuthManager.SignIn(identity);
}

return Redirect(Request.QueryString["ReturnUrl"]);
注销方法:

var authTypeNames = new List<string>();
authTypeNames.Add("Google");
authTypeNames.Add("Application");
authTypeNames.Add("Bearer");
authTypeNames.Add(DefaultAuthenticationTypes.ExternalCookie);

Request.GetOwinContext().Authentication.SignOut(authTypeNames.ToArray());
var authTypeNames=new List();
authTypeNames.Add(“谷歌”);
authTypeNames.Add(“应用程序”);
authTypeNames.Add(“持票人”);
添加(DefaultAuthenticationTypes.ExternalCookie);
Request.GetOwinContext().Authentication.SignOut(authTypeNames.ToArray());
我还研究了其他问题,如: 和

没有运气。我知道我可以通过设置一个负的到期日期来手动删除cookie,但如果可能的话,我更喜欢使用内置方法

我如何在注销时删除应用程序Cookie

如果您有任何母版页,请添加以下标签。也许这会有帮助


来自另一个对我有用的StackOverFlow答案:

仅使用以下其中一种:

Request.GetOwinContext().Authentication.SignOut();
Request.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.Current.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

我想第二个对你有用,但看起来你就是这么做的。你能自己测试一下吗?注释掉你的数组并确认它是否有效


老实说,我对OWIN的了解还不够,无法了解被动身份验证模式。

为了让注销方法标记身份验证票证(cookie)以便从客户端删除,传递到注销方法的AuthenticationType参数和cookie上的值必须完全匹配。如果要从客户端删除多个身份验证票证,则必须匹配所有这些身份验证类型,并将它们作为字符串[]传递给SignOut方法

身份验证票证的AuthenticationType通常以主机web容器的名称(即“.AspNet.”之类的名称)作为前缀,后跟您引导OWIN Cookie身份验证设置时使用的名称

看起来您在
Startup.Auth.cs
中将AuthenticationType字符串值设置为“Application”。试着简单地呼叫:

Request.GetOwinContext().Authentication.SignOut("Application");

如果这对您不起作用,我将调试您的应用程序,并查看您的应用程序允许的每种类型的身份验证用户的身份上的特定AuthenticationType,注意每种类型的AuthenticationType的值,并尝试在您的注销呼叫中将它们全部包含在字符串[]中。

我为此工作了几天。这就是最终对我有用的东西。我要做的第一件事是清除令牌缓存。接下来,我创建一个Auth应用程序类型数组。我加了4个。如果您正在使用它们,您可以添加更多。据我所知,我只使用Cookies和OpenIdConnect,但为了安全起见,我添加了载体和应用程序。最后一步是清除所有剩余Cookie(如果有)和任何剩余会话(如果有)。同样,我为此工作了好几天。这太令人沮丧了。我目前正在使用这些软件包中的4.0.1

 AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
 FormsAuthentication.SignOut();
 Session.Abandon();
Install-Package Microsoft.Owin.Security.OpenIdConnect
Install-Package Microsoft.Owin.Security.Cookies
Install-Package Microsoft.Owin.Host.SystemWeb

public ActionResult SignOut()
        {
            
            if (Request.IsAuthenticated)
            {
                string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;

                if (!string.IsNullOrEmpty(userId))
                {
                    // Get the user's token cache and clear it
                    SessionTokenCache tokenCache = new SessionTokenCache(userId, HttpContext);

                    string sessionID = HttpContext.Session.SessionID;

                    tokenCache.Clear(sessionID);
                }
            }

            var authTypeNames = new List<string>();
            authTypeNames.Add("Cookies");
            authTypeNames.Add("Application");
            authTypeNames.Add("Bearer");
            authTypeNames.Add("OpenIdConnect");

            // Send a sign-out request. 
            HttpContext.GetOwinContext().Authentication.SignOut(authTypeNames.ToArray());

            Request.Cookies.Clear();
            Session.RemoveAll();

            return RedirectToAction("Index", "Home");

        }
安装软件包Microsoft.Owin.Security.OpenIdConnect
安装软件包Microsoft.Owin.Security.Cookies
安装软件包Microsoft.Owin.Host.SystemWeb
公共行动结果签出()
{
如果(请求已验证)
{
字符串userId=ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
如果(!string.IsNullOrEmpty(userId))
{
//获取用户的令牌缓存并清除它
SessionTokenCache tokenCache=新的SessionTokenCache(用户ID,HttpContext);
字符串sessionID=HttpContext.Session.sessionID;
tokenCache.Clear(sessionID);
}
}
var authTypeNames=新列表();
authTypeNames.Add(“Cookies”);
authTypeNames.Add(“应用程序”);
authTypeNames.Add(“持票人”);
添加(“OpenIdConnect”);
//发送注销请求。
HttpContext.GetOwinContext().Authentication.SignOut(authTypeNames.ToArray());
Request.Cookies.Clear();
Session.RemoveAll();
返回重定向到操作(“索引”、“主页”);
}

我不明白这是怎么回事,因为我想缓存,而这在Firefox、Chrome甚至是IE的HTTPS上都不起作用。。这已经不是第一次了@ymz,这是一个不同的问题。我问的是关于使用从另一个应用程序调用的外部身份验证服务器注销的问题。这可能比已经存在的其他答案有所改进,但您没有解释。虽然这个代码块可能会回答这个问题,但如果您能提供一些解释来解释为什么它会这样做,那将是最好的。
Install-Package Microsoft.Owin.Security.OpenIdConnect
Install-Package Microsoft.Owin.Security.Cookies
Install-Package Microsoft.Owin.Host.SystemWeb

public ActionResult SignOut()
        {
            
            if (Request.IsAuthenticated)
            {
                string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;

                if (!string.IsNullOrEmpty(userId))
                {
                    // Get the user's token cache and clear it
                    SessionTokenCache tokenCache = new SessionTokenCache(userId, HttpContext);

                    string sessionID = HttpContext.Session.SessionID;

                    tokenCache.Clear(sessionID);
                }
            }

            var authTypeNames = new List<string>();
            authTypeNames.Add("Cookies");
            authTypeNames.Add("Application");
            authTypeNames.Add("Bearer");
            authTypeNames.Add("OpenIdConnect");

            // Send a sign-out request. 
            HttpContext.GetOwinContext().Authentication.SignOut(authTypeNames.ToArray());

            Request.Cookies.Clear();
            Session.RemoveAll();

            return RedirectToAction("Index", "Home");

        }