Web api 2 oauth2 过期滑动
Web api 2 oauth2 expiration sliding
我正在使用 Angular JS 和 Web API2 构建 SPA,使用 Oauth2 进行身份验证。我的问题,token'expiration是固定的,比如20分钟。那么如果用户在 20 分钟内没有任何请求,我们如何重定向到登录页面呢?
刷新令牌不起作用,因为系统将自动刷新令牌,尽管用户在有效时间内没有任何操作。
干杯,
您不需要在客户端应用程序中控制超时。
当客户端向资源服务器发出请求时,资源服务器会验证访问令牌,如果它已过期 returns 则返回 401 - 未经授权的响应。
当客户端从资源服务器获取 401 时,需要使用资源所有者凭据或刷新令牌从授权服务器获取新的访问令牌。
这是 OAuth 2.0 protocol 指定的行为。
如果您需要更深入的解释,请告诉我。
我使用 AuthorizeAttribute
并覆盖 OnAuthorization
public override void OnAuthorization(HttpActionContext actionContext)
{
string token = string.Empty;
AuthenticationTicket ticket;
//retrieve the token the client sent in the request...
token = (actionContext.Request.Headers.Any(x => x.Key == "Authorization")) ? actionContext.Request.Headers.Where(x => x.Key == "Authorization").FirstOrDefault().Value.SingleOrDefault().Replace("Bearer ", "") : "";
//Your OAuth Startup class may be called differently...
ticket = Startup.OAuthBearerOptions.AccessTokenFormat.Unprotect(token);
//verification using the ticket's properties. When it was set to expire (ExpiresUtc) or whatever other properties you may have appended to it's dictionnary.
//if verification fails..
//actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "Verification failed.");
//return;
//Otherwise, send a new token with an extended expiration date...
AuthenticationProperties refreshTokenProperties = new AuthenticationProperties(ticket.Properties.Dictionary)
{
IssuedUtc = ticket.Properties.IssuedUtc,
ExpiresUtc = DateTime.UtcNow.AddMinutes(20)
};
AuthenticationTicket newToken = new AuthenticationTicket(ticket.Identity, refreshTokenProperties);
string newTokenHash = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(newToken);
//add the new token to request properties. Can't add it to the header here, because creating response/response headers here will prevent process from proceeding to called controller method.
actionContext.Request.Properties.Add(new KeyValuePair<string, object>("Token", newTokenHash));
}
然后用 ActionFilterAttribute
过滤器链接它:
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response == null)
return;
var objectContent = actionExecutedContext.Response.Content as ObjectContent;
//the token we put in the filter above...
string tokenHash = (actionExecutedContext.Request.Properties.Any(x => x.Key == "Token")) ? (string)actionExecutedContext.Request.Properties.Where(x => x.Key == "Token").FirstOrDefault().Value : "";
}
您可以将新的 header 附加到响应中,放入 JSON 负载响应或将其添加为响应 cookie。然后你让你的客户在请求任何其他资源时使用这个新的哈希值,这样每次过期都会额外增加 20 分钟。
您可以在 App_Start/WebApiConfig.cs
中全局注册这些过滤器属性
config.Filters.Add(new ClassExtendingAuthorizeAttribute());
config.Filters.Add(new ClassExtendingActionFilterAttribute());
但是正如 jumuro 所提到的,您可以让您的客户端简单地使用刷新令牌。取决于您是否希望 back-end 或 front-end 完成大部分腿部工作。
希望对您有所帮助。
我正在使用 Angular JS 和 Web API2 构建 SPA,使用 Oauth2 进行身份验证。我的问题,token'expiration是固定的,比如20分钟。那么如果用户在 20 分钟内没有任何请求,我们如何重定向到登录页面呢?
刷新令牌不起作用,因为系统将自动刷新令牌,尽管用户在有效时间内没有任何操作。
干杯,
您不需要在客户端应用程序中控制超时。 当客户端向资源服务器发出请求时,资源服务器会验证访问令牌,如果它已过期 returns 则返回 401 - 未经授权的响应。 当客户端从资源服务器获取 401 时,需要使用资源所有者凭据或刷新令牌从授权服务器获取新的访问令牌。 这是 OAuth 2.0 protocol 指定的行为。 如果您需要更深入的解释,请告诉我。
我使用 AuthorizeAttribute
并覆盖 OnAuthorization
public override void OnAuthorization(HttpActionContext actionContext)
{
string token = string.Empty;
AuthenticationTicket ticket;
//retrieve the token the client sent in the request...
token = (actionContext.Request.Headers.Any(x => x.Key == "Authorization")) ? actionContext.Request.Headers.Where(x => x.Key == "Authorization").FirstOrDefault().Value.SingleOrDefault().Replace("Bearer ", "") : "";
//Your OAuth Startup class may be called differently...
ticket = Startup.OAuthBearerOptions.AccessTokenFormat.Unprotect(token);
//verification using the ticket's properties. When it was set to expire (ExpiresUtc) or whatever other properties you may have appended to it's dictionnary.
//if verification fails..
//actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "Verification failed.");
//return;
//Otherwise, send a new token with an extended expiration date...
AuthenticationProperties refreshTokenProperties = new AuthenticationProperties(ticket.Properties.Dictionary)
{
IssuedUtc = ticket.Properties.IssuedUtc,
ExpiresUtc = DateTime.UtcNow.AddMinutes(20)
};
AuthenticationTicket newToken = new AuthenticationTicket(ticket.Identity, refreshTokenProperties);
string newTokenHash = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(newToken);
//add the new token to request properties. Can't add it to the header here, because creating response/response headers here will prevent process from proceeding to called controller method.
actionContext.Request.Properties.Add(new KeyValuePair<string, object>("Token", newTokenHash));
}
然后用 ActionFilterAttribute
过滤器链接它:
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response == null)
return;
var objectContent = actionExecutedContext.Response.Content as ObjectContent;
//the token we put in the filter above...
string tokenHash = (actionExecutedContext.Request.Properties.Any(x => x.Key == "Token")) ? (string)actionExecutedContext.Request.Properties.Where(x => x.Key == "Token").FirstOrDefault().Value : "";
}
您可以将新的 header 附加到响应中,放入 JSON 负载响应或将其添加为响应 cookie。然后你让你的客户在请求任何其他资源时使用这个新的哈希值,这样每次过期都会额外增加 20 分钟。
您可以在 App_Start/WebApiConfig.cs
config.Filters.Add(new ClassExtendingAuthorizeAttribute());
config.Filters.Add(new ClassExtendingActionFilterAttribute());
但是正如 jumuro 所提到的,您可以让您的客户端简单地使用刷新令牌。取决于您是否希望 back-end 或 front-end 完成大部分腿部工作。
希望对您有所帮助。