【问题标题】:Can't sign out Web Api using Owin无法使用 Owin 退出 Web Api
【发布时间】:2016-11-25 21:15:39
【问题描述】:

我已使用 owin 登录,但无法退出。
在开始:

公共无效 ConfigureOAuth(IAppBuilder 应用程序) { OAuthAuthorizationServerOptions OAuthServerOptions = 新 OAuthAuthorizationServerOptions() { AllowInsecureHttp = true, TokenEndpointPath = new PathString("/token"), AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(20), 提供者 = 新 AuthorizationServerProvider(), AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie }; app.UseOAuthBearerTokens(OAuthServerOptions); app.UseCookieAuthentication(new CookieAuthenticationOptions()); }

在 AuthorizationServerProvider 中:

公共覆盖任务 ValidateClientAuthentication(OAuthValidateClientAuthenticationContext 上下文) { context.Validated(); 返回 Task.FromResult(null); } 公共覆盖异步任务 GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext 上下文) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*"}); 使用 (demoEntities _repo = new demoEntities()) { if (!_repo.users.Where(x => x.username == context.UserName && x.pass == context.Password).Any()) { context.SetError("invalid_grant", "wrong."); //context.Rejected(); 返回; } } //上下文.请求。 var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim("sub", context.UserName)); identity.AddClaim(new Claim("role", "user")); identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName)); if (context.Request.Path.Value != "/api/apidemo/logout") { context.Request.Context.Authentication.SignIn(identity); } 别的 { context.Request.Context.Authentication.SignOut(); } context.Validated(身份); }


在 ApiController 中:

  [HttpGet]
    [ActionName("logout")]
    public IHttpActionResult logout()
    {
        Request.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        this.Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        return Ok();
    }

我调用注销然后使用旧令牌但它仍然可以使用。所以注销不起作用? 感谢收看。

【问题讨论】:

    标签: asp.net-web-api owin


    【解决方案1】:

    这不是 Owin 的工作方式。没有注销。您获得一个令牌,并且该令牌在一定时间内有效。令牌在过期之前一直有效。

    您可以自己添加一个额外的层,基本上是在生成令牌时,将其与到期数据和有效状态一起存储在某个地方。当您调用注销时,您将令牌更新为无效,然后当它被使用时,在它通过 owin 检查后,您然后运行您自己的检查并将其无效。

    老实说,我不会为此烦恼。如果你沿着这条路线走,这意味着你没有使用 Owin 来完成它的目的,即应用程序级别的身份验证,而不是用户身份验证。两者差别很大。

    因此,我的建议是使用会员系统进行用户身份验证,并将 owin 的东西分开。如果您这样做,那么您实际上可以注销某人。

    所以底线是:owin 令牌在过期之前都是有效的。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-24
    • 2015-02-27
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2013-09-26
    • 2015-03-29
    • 2015-02-03
    相关资源
    最近更新 更多