public virtual async Task <ExternalEventSyncResult> RunSynchronizationAsync(ExternalAccount account, Registration registration)
        {
            if (account == null)
            {
                throw new ArgumentException(nameof(account));
            }

            if (registration == null)
            {
                throw new ArgumentException(nameof(registration));
            }

            if (registration.EventInfo == null)
            {
                throw new ArgumentException(nameof(registration));
            }

            var externalEvent = await EnsureExternalEventAsync(registration.EventInfo);

            if (externalEvent == null)
            {
                return(ExternalEventSyncResult.NotSynced);
            }

            if (await _context.ExternalRegistrations
                .AnyAsync(e => e.ExternalEventId == externalEvent.LocalId &&
                          e.ExternalAccountId == account.LocalId))
            {
                return(ExternalEventSyncResult.AlreadySynced);
            }

            await RegisterUserToExternalEventAsync(account, externalEvent);

            var externalRegistration = new ExternalRegistration
            {
                ExternalEvent   = externalEvent,
                ExternalAccount = account,
                Registration    = registration
            };

            try
            {
                await _context.ExternalRegistrations.AddAsync(externalRegistration);

                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException e) when(e.IsUniqueKeyViolation())
            {
                _logger.LogWarning(e, e.Message);
                _context.ExternalRegistrations.Remove(externalRegistration);
                return(ExternalEventSyncResult.AlreadySynced);
            }

            return(ExternalEventSyncResult.Synced);
        }
Beispiel #2
0
        internal static AuthenticationBuilder AddAerieHub(this AuthenticationBuilder builder, IConfiguration config)
        {
            const string TenantName = "AerieHub.com";

            return(builder.AddOpenIdConnect("Microsoft", TenantName, options =>
            {
                options.SignInScheme = IdentityConstants.ExternalScheme;

                var tenant = TenantName;
                options.Authority = $"https://login.microsoftonline.com/{tenant}/v2.0";
                options.ClientId = config["AzureAd:ClientId"];
                options.ClientSecret = config["AzureAd:ClientSecret"];

                options.CallbackPath = new PathString("/signin-oidc");

                options.Scope.Clear();
                options.Scope.Add("openid");
                options.Scope.Add("profile");
                options.Scope.Add("email");
                options.ResponseType = "code";

                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;

                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = false,                     // set to true and populate ValidIssuers to only allow login from registered directories
                    NameClaimType = "name"
                };

                options.Events = new OpenIdConnectEvents
                {
                    OnTicketReceived = async(context) =>
                    {
                        var data = new DataAccess(config);
                        using (var cn = data.GetConnection())
                        {
                            await ExternalRegistration.CreateAsync(cn, context.Principal, TenantName);
                        }
                    }
                };
            }));
        }