Пример #1
0
        private void Backup()
        {
            var source = new DirectoryInfo(CommonHelper.MapPath("~/"));

            var    tempPath      = CommonHelper.MapPath("~/App_Data/_Backup/App/SmartStore");
            string localTempPath = null;

            for (int i = 0; i < 50; i++)
            {
                localTempPath = tempPath + (i == 0 ? "" : "." + i.ToString());
                if (!Directory.Exists(localTempPath))
                {
                    Directory.CreateDirectory(localTempPath);
                    break;
                }
                localTempPath = null;
            }

            if (localTempPath == null)
            {
                var exception = new SmartException("Too many backups in '{0}'.".FormatInvariant(tempPath));
                _logger.Error(exception.Message, exception);
                throw exception;
            }

            var backupFolder  = new DirectoryInfo(localTempPath);
            var folderUpdater = new FolderUpdater(_logger);

            folderUpdater.Backup(source, backupFolder, "App_Data", "Media");

            _logger.Information("Backup successfully created in folder '{0}'.".FormatInvariant(localTempPath));
        }
Пример #2
0
        private IPackage FindPackage(bool createLogger, out string path)
        {
            path = null;
            var dir = CommonHelper.MapPath(UpdatePackagePath, false);

            if (!Directory.Exists(dir))
            {
                return(null);
            }

            var files = Directory.GetFiles(dir, "SmartStore.*.nupkg", SearchOption.TopDirectoryOnly);

            // TODO: allow more than one package in folder and return newest
            if (files.Length == 0 || files.Length > 1)
            {
                return(null);
            }

            try
            {
                path = files[0];
                IPackage package = new ZipPackage(files[0]);
                if (createLogger)
                {
                    _logger = CreateLogger(package);
                    _logger.Information("Found update package '{0}'".FormatInvariant(package.GetFullName()));
                }
                return(package);
            }
            catch { }

            return(null);
        }
        /// <summary>
        /// Acquire Token for user
        /// </summary>
        public async Task <AuthenticationResult> GetAuthenticationAsync()
        {
            AuthenticationResult authResult = null;
            var application = _clientApplication as PublicClientApplication;

            var accounts = await application.GetAccountsAsync();

            if (accounts.Any())
            {
                try
                {
                    authResult = await application.AcquireTokenSilent(_scopes, accounts.FirstOrDefault())
                                 .ExecuteAsync();
                }
                catch (MsalUiRequiredException ex)
                {
                    TraceLogger.Error($"MSAL Error {ex}");
                }
            }

            if (authResult == null)
            {
                try
                {
                    authResult = application.AcquireTokenWithDeviceCode(_scopes,
                                                                        (deviceCodeCallback) =>
                    {
                        Console.WriteLine(deviceCodeCallback.Message);
                        return(Task.FromResult(0));
                    }).ExecuteAsync().GetAwaiter().GetResult();
                    TraceLogger.Information($"Username from Device Code Flow {authResult?.Account?.Username}");
                }
                catch (MsalServiceException aex)
                {
                    TraceLogger.Error($"MsalServiceException AcquireTokenWithDeviceCode {aex}");
                    throw;
                }
                catch (OperationCanceledException ex)
                {
                    // If you use a CancellationToken, and call the Cancel() method on it, then this *may* be triggered
                    // to indicate that the operation was cancelled.
                    // See https://docs.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads
                    // for more detailed information on how C# supports cancellation in managed threads.
                    TraceLogger.Error($"OperationCanceledException AcquireTokenWithDeviceCode {ex}");
                }
                catch (MsalClientException ex)
                {
                    // Possible cause - verification code expired before contacting the server
                    // This exception will occur if the user does not manage to sign-in before a time out (15 mins) and the
                    // call to `AcquireTokenWithDeviceCode` is not cancelled in between
                    TraceLogger.Error($"MsalClientException AcquireTokenWithDeviceCode {ex}");
                }
            }

            return(authResult);
        }
Пример #4
0
        protected internal virtual void CallEndpoint(string url)
        {
            if (_shuttingDown)
            {
                return;
            }

            var req = (HttpWebRequest)WebRequest.Create(url);

            req.UserAgent     = "SmartStore.NET";
            req.Method        = "POST";
            req.ContentType   = "text/plain";
            req.ContentLength = 0;

            string authToken = Guid.NewGuid().ToString();

            _authTokens.TryAdd(authToken, true);
            req.Headers.Add("X-AUTH-TOKEN", authToken);

            req.GetResponseAsync().ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    HandleException(t.Exception, url);
                    _errCount++;
                    if (_errCount >= 10)
                    {
                        // 10 failed attempts in succession. Stop the timer!
                        this.Stop();
                        using (var logger = new TraceLogger())
                        {
                            logger.Information("Stopping TaskScheduler sweep timer. Too many failed requests in succession.");
                        }
                    }
                }
                else
                {
                    _errCount    = 0;
                    var response = t.Result;

                    //using (var logger = new TraceLogger())
                    //{
                    //	logger.Debug("TaskScheduler Sweep called successfully: {0}".FormatCurrent(response.GetResponseStream().AsString()));
                    //}

                    response.Dispose();
                }
            });
        }
Пример #5
0
        protected internal virtual void CallEndpoint(Uri uri)
        {
            if (_shuttingDown)
            {
                return;
            }

            var req = WebHelper.CreateHttpRequestForSafeLocalCall(uri);

            req.Method        = "POST";
            req.ContentType   = "text/plain";
            req.ContentLength = 0;
            req.Timeout       = 10000;       // 10 sec.

            string authToken = CreateAuthToken();

            req.Headers.Add("X-AUTH-TOKEN", authToken);

            req.GetResponseAsync().ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    HandleException(t.Exception, uri);
                    _errCount++;
                    if (_errCount >= 10)
                    {
                        // 10 failed attempts in succession. Stop the timer!
                        this.Stop();
                        using (var logger = new TraceLogger())
                        {
                            logger.Information("Stopping TaskScheduler sweep timer. Too many failed requests in succession.");
                        }
                    }
                }
                else
                {
                    _errCount    = 0;
                    var response = t.Result;

                    //using (var logger = new TraceLogger())
                    //{
                    //	logger.Debug("TaskScheduler Sweep called successfully: {0}".FormatCurrent(response.GetResponseStream().AsString()));
                    //}

                    response.Dispose();
                }
            });
        }