예제 #1
0
        /// <summary>
        /// Gets the refresh token from the specified content token.
        /// </summary>
        /// <param name="contextToken">SharePoint App Token as context token</param>
        /// <returns>
        /// Refresh Token as string
        /// </returns>
        public static string GetRefreshToken(string contextToken)
        {
            string refreshToken = string.Empty;

            if (!string.IsNullOrWhiteSpace(contextToken))
            {
                SharePointContextToken contextTokenData = TokenHelper.ReadAndValidateContextToken(contextToken);
                refreshToken = null != contextTokenData?EncryptionDecryption.Encrypt(contextTokenData.RefreshToken) : string.Empty;
            }
            return(refreshToken);
        }
예제 #2
0
        /// <summary>
        /// Generates Client Context in case of online and onpremise deployments
        /// </summary>
        /// <param name="contextToken">SharePoint Application Token</param>
        /// <param name="siteUrl">SharePoint site URL</param>
        /// <param name="refreshToken">The refresh token</param>
        /// <param name="currentIdentity">Current Identity object</param>
        /// <returns>Client Context</returns>
        private static ClientContext GetCommonContext(string contextToken, Uri siteUrl, string refreshToken, WindowsIdentity currentIdentity)
        {
            bool          environment = Convert.ToBoolean(ConstantStrings.IsDeployedOnAzure, CultureInfo.InvariantCulture);
            ClientContext result      = null;

            if (environment)
            {
                try
                {
                    string decryptedRefreshToken = string.Empty;
                    string key = ConfigurationManager.AppSettings["Encryption_Key"];

                    if (!string.IsNullOrEmpty(refreshToken) && !string.IsNullOrEmpty(key))
                    {
                        decryptedRefreshToken = EncryptionDecryption.Decrypt(refreshToken, key);
                        if (!string.IsNullOrEmpty(decryptedRefreshToken))
                        {
                            result = FetchClientContext(contextToken, siteUrl, decryptedRefreshToken);
                        }
                        else
                        {
                            key = ConfigurationManager.AppSettings["Old_Encryption_Key"];
                            if (!string.IsNullOrEmpty(refreshToken) && !string.IsNullOrEmpty(key))
                            {
                                decryptedRefreshToken = EncryptionDecryption.Decrypt(refreshToken, key);
                                if (!string.IsNullOrEmpty(decryptedRefreshToken))
                                {
                                    result = FetchClientContext(contextToken, siteUrl, decryptedRefreshToken);
                                }
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ConstantStrings.LogTableName);
                    throw;
                }
            }
            else
            {
                if (null != siteUrl)
                {
                    result = TokenHelper.GetS2SClientContextWithWindowsIdentity(siteUrl, currentIdentity);
                }
            }
            return(result);
        }