Ejemplo n.º 1
0
 public OutlookCalendarProvider(ICalendarUserSettingsService settingsService)
 {
     _settingsService = settingsService;
     _authSettings    = OutlookAuthSettings.GetAuthSettings();
     if (_authSettings == null)
     {
         throw new Exception("No client settings present");
     }
 }
        /// <summary>
        /// Register outlook provider
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <param name="outlookOptions"></param>
        /// <returns></returns>
        public static CalendarServiceCollection RegisterOutlookCalendarProvider(this CalendarServiceCollection serviceCollection, Action <MsAuthorizationSettings> outlookOptions)
        {
            Arg.NotNull(outlookOptions, nameof(RegisterOutlookCalendarProvider));
            serviceCollection.RegisterExternalCalendarProvider(options =>
            {
                options.ProviderName    = nameof(OutlookCalendarProvider);
                options.ProviderType    = typeof(OutlookCalendarProvider);
                options.DisplayName     = "Outlook";
                options.FontAwesomeIcon = "microsoft";
            });

            var authSettings = new MsAuthorizationSettings();

            outlookOptions(authSettings);
            OutlookAuthSettings.SetAuthSettings(authSettings);

            serviceCollection.Services.AddAuthentication()
            .AddMicrosoftAccount(microsoftOptions =>
            {
                microsoftOptions.ClientId                = authSettings.ClientId;
                microsoftOptions.ClientSecret            = authSettings.ClientSecretId;
                microsoftOptions.SaveTokens              = true;
                microsoftOptions.Events.OnCreatingTicket = ctx =>
                {
                    var tokens = ctx.Properties.GetTokens().ToList();

                    tokens.Add(new AuthenticationToken()
                    {
                        Name  = "TicketCreated",
                        Value = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)
                    });

                    ctx.Properties.StoreTokens(tokens);

                    return(Task.CompletedTask);
                };

                foreach (var scope in authSettings.Scopes)
                {
                    microsoftOptions.Scope.Add(scope);
                }
            });
            return(serviceCollection);
        }