/// <summary>
        /// Checks if the used ClientContext is app-only
        /// </summary>
        /// <param name="clientContext">The ClientContext to inspect</param>
        /// <returns>True if app-only, false otherwise</returns>
        public static bool IsAppOnly(this ClientRuntimeContext clientContext)
        {
            // Set initial result to false
            var result = false;

            // Try to get an access token from the current context
            var accessToken = clientContext.GetAccessToken();

            // If any
            if (!String.IsNullOrEmpty(accessToken))
            {
                // Try to decode the access token
                var token = new JwtSecurityToken(accessToken);

                // Search for the UPN claim, to see if we have user's delegation
                var upn = token.Claims.FirstOrDefault(claim => claim.Type == "upn")?.Value;
                if (String.IsNullOrEmpty(upn))
                {
                    result = true;
                }
            }
            else if (clientContext.Credentials == null)
            {
                result = true;
            }
            // As a final check, do we have the auth cookies?
            if (clientContext.HasAuthCookies())
            {
                result = false;
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks if the used ClientContext is app-only
        /// </summary>
        /// <param name="clientContext">The ClientContext to inspect</param>
        /// <returns>True if app-only, false otherwise</returns>
        public static bool IsAppOnlyWithDelegation(this ClientRuntimeContext clientContext)
        {
            // Set initial result to false
            var result = false;

            // Try to get an access token from the current context
            var accessToken = clientContext.GetAccessToken();

            // If any
            if (!String.IsNullOrEmpty(accessToken))
            {
                // Try to decode the access token
                try
                {
                    var token = new JwtSecurityToken(accessToken);

                    if (token.Audiences.Any(x => x.StartsWith(TokenHelper.SharePointPrincipal)))
                    {
                    }

                    // Search for the UPN claim, to see if we have user's delegation
                    var upn = token.Claims.FirstOrDefault(claim => claim.Type == "upn")?.Value;
                    if (!String.IsNullOrEmpty(upn))
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Maybe Newtonsoft.Json assembly is not loaded?", ex);
                }
            }
            else if (clientContext.Credentials == null)
            {
                result = false;
            }

            // As a final check, do we have the auth cookies?
            if (clientContext.HasAuthCookies())
            {
                result = false;
            }

            return(result);
        }