Azure Oauth - 如何更改令牌过期时间?
Azure Oauth - how to change token expiration time?
我们在 Azure 中使用 Oauth2。并且默认情况下服务器 returns 令牌以一小时为间隔过期。有什么办法可以改变有效期?
现在可以配置令牌生命周期。您可以在这里阅读更多内容:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-configurable-token-lifetimes.
备注:此功能处于预览阶段,不会以这种方式投入生产。下面的header也放在我上面提到的文档link
After hearing from customers during the preview, we're planning to replace this functionality with a new feature in Azure Active Directory Conditional Access. Once the new feature is complete, this functionality will eventually be deprecated after a notification period. If you use the Configurable Token Lifetime policy, be prepared to switch to the new Conditional Access feature once it's available.
原回答:
目前无法更改到期间隔。这些是当前的到期时间。
访问令牌持续 1 小时
刷新令牌持续 14 天,但是
- 如果您在这 14 天内使用刷新令牌,您将收到一个新有效期 window 提前 14 天的新令牌。您可以在长达 90 天的总有效期内重复此技巧,然后您必须重新验证
- 刷新令牌可以随时失效,原因与您的应用无关(例如,用户更改密码)。因此,您不应在代码中依赖上述内容——您的逻辑应始终假设刷新令牌随时可能失败
- 访客 MSA 帐户的刷新令牌问题仅持续 12 小时
来源:http://www.cloudidentity.com/blog/2015/03/20/azure-ad-token-lifetime/还有我自己的经历。
假设您在谈论 Azure AD,据我所知这是不可能的。
但是,在响应以及令牌中,您还会返回一个 refresh token
,它可用于获取新令牌。您可以做的是缓存 refresh token
和 expiry time
并且在发出请求之前您可以检查令牌是否已过期(或即将过期)。在这种情况下,您可以使用此刷新令牌来获取新令牌,然后发出您的请求。
您必须使用电源 shell 来执行以下 2 个步骤:
创建新策略。此策略设置超时 2 小时
New-AzureADPolicy -Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"02:00:00","MaxAgeSessionSingleFactor":"02:00:00"}}') -DisplayName "MyWebPolicyScenario" -IsOrganizationDefault $false -Type "TokenLifetimePolicy"
将此政策应用于您的网站
Add-AzureADServicePrincipalPolicy -Id <ObjectId of the ServicePrincipal> -RefObjectId <ObjectId of the Policy>
注意:
为了获得 ServicePrincipal 的 ObjectId,运行 这个命令:Get-AzureADServicePrincipal
要获取策略的 ObjectId,运行 此命令:Get-AzureADPolicy
更多细节你可以参考这个文档:https://docs.microsoft.com/en-us/azure/active-directory/active-directory-configurable-token-lifetimes
我们在 Azure 中使用 Oauth2。并且默认情况下服务器 returns 令牌以一小时为间隔过期。有什么办法可以改变有效期?
现在可以配置令牌生命周期。您可以在这里阅读更多内容:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-configurable-token-lifetimes.
备注:此功能处于预览阶段,不会以这种方式投入生产。下面的header也放在我上面提到的文档link
After hearing from customers during the preview, we're planning to replace this functionality with a new feature in Azure Active Directory Conditional Access. Once the new feature is complete, this functionality will eventually be deprecated after a notification period. If you use the Configurable Token Lifetime policy, be prepared to switch to the new Conditional Access feature once it's available.
原回答:
目前无法更改到期间隔。这些是当前的到期时间。
访问令牌持续 1 小时
刷新令牌持续 14 天,但是
- 如果您在这 14 天内使用刷新令牌,您将收到一个新有效期 window 提前 14 天的新令牌。您可以在长达 90 天的总有效期内重复此技巧,然后您必须重新验证
- 刷新令牌可以随时失效,原因与您的应用无关(例如,用户更改密码)。因此,您不应在代码中依赖上述内容——您的逻辑应始终假设刷新令牌随时可能失败
- 访客 MSA 帐户的刷新令牌问题仅持续 12 小时
来源:http://www.cloudidentity.com/blog/2015/03/20/azure-ad-token-lifetime/还有我自己的经历。
假设您在谈论 Azure AD,据我所知这是不可能的。
但是,在响应以及令牌中,您还会返回一个 refresh token
,它可用于获取新令牌。您可以做的是缓存 refresh token
和 expiry time
并且在发出请求之前您可以检查令牌是否已过期(或即将过期)。在这种情况下,您可以使用此刷新令牌来获取新令牌,然后发出您的请求。
您必须使用电源 shell 来执行以下 2 个步骤:
创建新策略。此策略设置超时 2 小时
New-AzureADPolicy -Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"02:00:00","MaxAgeSessionSingleFactor":"02:00:00"}}') -DisplayName "MyWebPolicyScenario" -IsOrganizationDefault $false -Type "TokenLifetimePolicy"
将此政策应用于您的网站
Add-AzureADServicePrincipalPolicy -Id <ObjectId of the ServicePrincipal> -RefObjectId <ObjectId of the Policy>
注意:
为了获得 ServicePrincipal 的 ObjectId,运行 这个命令:Get-AzureADServicePrincipal
要获取策略的 ObjectId,运行 此命令:Get-AzureADPolicy
更多细节你可以参考这个文档:https://docs.microsoft.com/en-us/azure/active-directory/active-directory-configurable-token-lifetimes