【问题标题】:Microsoft Graph API multi-tenant token lifetimeMicrosoft Graph API 多租户令牌生命周期
【发布时间】:2017-04-29 17:53:29
【问题描述】:

我目前正在使用this example 在 C# 中登录 Microsoft graph。我不确定这在内部使用什么样的令牌,所以我的主要问题是:我是否需要担心刷新令牌超时? 我发现 this page 关于配置刷新令牌生命周期 - 我需要更改这些吗? 如果我在应用程序租户中更改它们,它们是否也会在我注册应用程序的租户中设置

初始身份验证是这样工作的(完整代码here):

 AuthorizationCodeReceived = async (context) =>
                        {
                            // We received an authorization code => get token.
                            var code = context.Code;

                            ClientCredential credential = new ClientCredential(clientId, appKey);
                            string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
                            string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;

                            Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(string.Format("https://login.microsoftonline.com/{0}", tenantID), new EFADALTokenCache(signedInUserID));
                            AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
                                code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path).TrimEnd('/')), credential, graphResourceID);
                        }

我稍微修改了授权过程,所以我可以使用https://github.com/Azure-Samples/active-directory-dotnet-graphapi-web中的方法:

internal class AuthenticationHelper
    {

        /// <summary>
        ///     Async task to acquire token for Application.
        /// </summary>
        /// <returns>Async Token for application.</returns>
        public static async Task<string> AcquireTokenAsync()
        {
            string clientId = ConfigurationManager.AppSettings["ida:ClientID"];
            string appKey = ConfigurationManager.AppSettings["ida:AppKey"];
            string graphResourceID = Constants.ResourceUrl;
            string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
            string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
            string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

            // get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
            ClientCredential clientcred = new ClientCredential(clientId, appKey);
            // initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's EF DB
            AuthenticationContext authContext = new AuthenticationContext(string.Format("https://login.microsoftonline.com/{0}", tenantID), new EFADALTokenCache(signedInUserID));
            AuthenticationResult result = await authContext.AcquireTokenSilentAsync(graphResourceID, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
            return result.AccessToken;
        }

        /// <summary>
        ///     Get Active Directory Client for Application.
        /// </summary>
        /// <returns>ActiveDirectoryClient for Application.</returns>
        public static ActiveDirectoryClient GetActiveDirectoryClient()
        {
            string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
            Uri baseServiceUri = new Uri(Constants.ResourceUrl);
            ActiveDirectoryClient activeDirectoryClient =
                new ActiveDirectoryClient(new Uri(baseServiceUri, tenantID),
                    async () => await AcquireTokenAsync());
            return activeDirectoryClient;
        }
    }

我从上面的代码中得到的(Bearer)AccessToken 的 ExpirationDate 等于请求的时间并且很长 - 所以在身份验证过程中真正发生的事情对我来说似乎是一个黑匣子,我很想了解它。

【问题讨论】:

    标签: c# azure microsoft-graph-api


    【解决方案1】:

    简短回答:是的。

    长答案:您正在使用的库 (ADAL) 将尽其所能使您的刷新令牌(以及因此您的用户会话)尽可能长时间有效。每次您拨打AcquireToken* 时,它都会为您处理令牌刷新。但是,最终刷新令牌将过期。它们何时到期是您无法控制的(作为开发人员)。您可以使用您引用的链接设置默认值,但这些默认值始终可以被客户覆盖。刷新令牌也可能因其他原因而失效,例如用户撤销您的应用或您的应用被识别为潜在恶意。

    因此,您需要处理 ADAL 无法为您获取令牌的情况。在 ADAL 中,这将导致一个异常,您可以优雅地处理该异常并将用户重定向以再次登录,在那里他们可以纠正导致刷新令牌过期的任何情况。

    【讨论】:

    • 谢谢,这让事情变得更清楚了——我没有遇到客户撤销令牌的问题。但我不确定我的 AD 中令牌生命周期的设置是否可以被客户 AD 覆盖,我的应用程序在哪里获得授权?因为我可能需要做一些后台任务,比如归档电子邮件,所以这可能会成为一个严重的问题。
    猜你喜欢
    • 1970-01-01
    • 2020-12-19
    • 2021-03-24
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 2017-07-25
    相关资源
    最近更新 更多