예제 #1
0
        public Worker(ILogger <Worker> logger,
                      IOptionsMonitor <BaseConfig> optionsAccessor,
                      AppState appState,
                      UserAuthService userAuthService,
                      MediatR.IMediator mediator,
                      IWorkingHoursService workingHoursService)
        {
            Config = optionsAccessor.CurrentValue;
            _workingHoursService = workingHoursService;
            _mediator            = mediator;
            _userAuthService     = userAuthService;
            _logger   = logger;
            _appState = appState;

            _graphClient = new GraphServiceClient(userAuthService);
        }
예제 #2
0
        public Worker(IHueService hueService,
                      ILogger <Worker> logger,
                      IOptionsMonitor <ConfigWrapper> optionsAccessor,
                      AppState appState,
                      LIFXService lifxService,
                      UserAuthService userAuthService)
        {
            Config           = optionsAccessor.CurrentValue;
            _hueService      = hueService;
            _lifxService     = lifxService;
            _logger          = logger;
            _appState        = appState;
            _userAuthService = userAuthService;

            _graphClient = new GraphServiceClient(userAuthService);
        }
예제 #3
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
            .AddAzureAD(options => Configuration.Bind(options));

            services.AddHttpContextAccessor();
            services.Configure <ConfigWrapper>(Configuration);
            var userAuthService = new UserAuthService(Configuration);

            services.AddSingleton(userAuthService);

            services.Configure <OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
            {
                options.ResponseType = "id_token code";
                options.Authority    = $"{Configuration["Instance"]}common/v2.0";
                options.Scope.Add("offline_access");
                options.Scope.Add("User.Read");

                options.TokenValidationParameters = new TokenValidationParameters
                {
                    // Azure ID tokens give name in "name"
                    NameClaimType  = "name",
                    ValidateIssuer = false
                };

                // Hook into the OpenID events to wire up MSAL
                options.Events = new OpenIdConnectEvents
                {
                    OnRedirectToIdentityProviderForSignOut = async(context) =>
                    {
                        await userAuthService.SignOut();
                    },
                    OnAuthenticationFailed = context =>
                    {
                        context.Response.Redirect("/Error");
                        context.HandleResponse();
                        return(Task.FromResult(0));
                    },
                    OnAuthorizationCodeReceived = async(context) =>
                    {
                        // Prevent ASP.NET Core from handling the code redemption itself
                        context.HandleCodeRedemption();

                        var idToken = await userAuthService
                                      .AddUserToTokenCache(context.ProtocolMessage.Code);

                        // Pass the ID token on to the middleware, but
                        // leave access token management to MSAL
                        context.HandleCodeRedemption(null, idToken);
                    }
                };
            });

            services.AddHttpClient();

            services.AddControllersWithViews(options =>
            {
                var policy = new AuthorizationPolicyBuilder()
                             .RequireAuthenticatedUser()
                             .Build();
                options.Filters.Add(new AuthorizeFilter(policy));
            });

            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddOptions();

            services.AddSingleton <IGraphService, GraphService>();
            services.AddSingleton <LIFXService, LIFXService>();
            services.AddSingleton <IHueService, HueService>();
            services.AddSingleton <AppState, AppState>();
            services.AddBlazoredModal();
            services.AddHostedService <Worker>();
        }