【问题标题】:OWIN authentication, expire current token and remove cookieOWIN 身份验证,使当前令牌过期并删除 cookie
【发布时间】:2014-12-02 14:55:50
【问题描述】:

我有一个用于身份验证的 OWIN 中间件。我们有两种类型的身份验证。 第一种是使用以下配置的不记名令牌

var OAuthOptions =  new OAuthAuthorizationServerOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ExternalBearer,
        TokenEndpointPath = new PathString("/Token"),
        Provider = new ApplicationOAuthProvider(PublicClientId),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
        AllowInsecureHttp = true,
        AccessTokenFormat = new SecureTokenFormatter(GetMachineKey())
    };

第二种使用身份验证cookie进行外部登录

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
    AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive,
    CookieHttpOnly = true,
    CookieSecure = CookieSecureOption.SameAsRequest,
    CookieName = ".AspNet." + DefaultAuthenticationTypes.ExternalCookie,
    ExpireTimeSpan = TimeSpan.FromMinutes(5),
    TicketDataFormat = new SecureTokenFormatter(GetMachineKey())
});

当用户注销时,我们实际上发出了两次注销

Request.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

Request.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ExternalBearer);

对于第一个,我希望看到 .AspNet.ExternalCookie Cookie 从浏览器中删除,但事实并非如此。 对于第二个,我希望我的 Token 无效并且 User.Current.Identity = null,但事实并非如此。

所以我怎么能 1)物理注销当前会话的当前身份? 2) 从浏览器中移除外部 Cookie?

【问题讨论】:

  • 我通过 :Request.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie); 解决了同样的问题; FederatedAuthentication.SessionAuthenticationModule.SignOut();

标签: c# authentication owin


【解决方案1】:

我遇到了同样的问题,经过 3 天的搜索,我找到了 asnwer(有点……)。

在您的注销中尝试这些代码行中的一个(并且仅一个)。 (它们都对我有用,但我使用的是第一个,但例子越多越好,对吧??)

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

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

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

这篇文章很好地描述了这个问题,但它没有提供有效的修复(至少对我来说没有) http://coding.abel.nu/2014/11/catching-the-system-webowin-cookie-monster/

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2015-07-02
    • 2018-09-29
    • 2021-01-10
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多