我正在使用以下 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
我想实现刷新 token 提供程序,因此我将过期时间设置为 2 分钟。但我注意到 WEB API 即使在 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 中也提到了它.
https://stackoverflow.com/questions/23951859/