OWIN OAuth 2.0 - 承载令牌永不过期

时间:2014-05-30 10:00:31

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

我正在使用以下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分钟后也可以访问资源。

提前致谢!

2 个答案:

答案 0 :(得分:2)

我遇到了这个问题,因为我忘了正确配置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中也提到了这一点。

答案 1 :(得分:1)

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

相关问题