【问题标题】:OWIN OAuth 2.0 - Bearer Token Never ExpireOWIN OAuth 2.0 - 承载令牌永不过期
【发布时间】:2014-05-30 10:00:31
【问题描述】:

我正在使用以下 OAuth 提供程序和选项:

    UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(new ApplicationDbContext()));
    OAuthOptions = new OAuthAuthorizationServerOptions
    {
        TokenEndpointPath = new PathString("/Token"),
        Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
        AuthorizeEndpointPath = new PathString("/api/AccountOwin/ExternalLogin"),
        AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(2),
        AllowInsecureHttp = true
    };
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Enable the application to use bearer tokens to authenticate users
            app.UseOAuthBearerTokens(OAuthOptions);

Oauth Provider 类来自以下链接: https://github.com/gustavo-armenta/BearerTokenAuthenticationSample/blob/master/BearerTokenAuthenticationSample/Providers/ApplicationOAuthProvider.cs

我想实现刷新令牌提供程序,因此我将过期时间设置为 2 分钟。但我注意到 WEB API 即使在 2 分钟后也允许访问资源。

提前致谢!

【问题讨论】:

  • 您是使用完整示例还是将其应用到您自己的代码中?您是否将 [Authorize] 属性应用于您正在访问的端点?
  • 是的,我确定我使用的是 Authorize 属性。如果没有有效的令牌,我无法访问我的资源,但我将过期时间设置为 2 分钟,但之后我仍然可以使用该令牌访问资源。这就是为什么我对此感到好奇。在所有示例中,我看到每个人都将到期日设置为超过一天。
  • @HaveThunk 也许我用错了这个例子?这很奇怪,我仍然有问题。

标签: .net oauth owin asp.net-web-api2


【解决方案1】:

我遇到了这个问题,因为我忘记正确配置 WebAPI。将以下代码添加到我的 WebApiConfig Register() 方法中即可解决。

// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

我在the sample I used 中找到了这个,在this post 中也提到了它。

【讨论】:

  • 我在我的配置文件中使用了相同的设置。但结果是一样的。
【解决方案2】:

我们遇到了同样的问题。在我们的例子中,验证服务器是使用 web api 2.0 构建的,而资源服务器是 web api 2.2。我们首先构建了身份验证服务器。然后构建资源服务器。当我们构建资源服务器并添加 Nuget 包时,我们得到了 web api 2.2。将包升级到身份验证服务器上的新版本并重新部署解决了我们的问题。

【讨论】: