Example #1
0
        private void SetCookieOptions(CookieAuthenticationOptions options)
        {
            options.Cookie.Name         = "auth-token";
            options.Cookie.SecurePolicy = CookieSecurePolicy.Always;

            options.LoginPath        = Configuration["Basic:LoginPath"];
            options.AccessDeniedPath = Configuration["Basic:AccessDeniedPath"];
            options.LogoutPath       = Configuration["Basic:LogoutPath"];
            options.ClaimsIssuer     = Configuration["Basic:ClaimIssuer"];
            options.Events           = new CookieAuthenticationEvents
            {
                OnValidatePrincipal = async context =>
                {
                    if (context.Principal.Identity.IsAuthenticated)
                    {
                        var props = context.Properties;
                        // check for out of date web token or missing token
                        if (!await RefreshWebToken(props))
                        {
                            context.RejectPrincipal();

                            await context.HttpContext.SignOutAsync(
                                CookieAuthenticationDefaults.AuthenticationScheme);

                            return;
                        }
                        else
                        {
                            if (props.IssuedUtc < DateTime.UtcNow.AddHours(-2) ||
                                string.IsNullOrEmpty(props.GetTokenValue("api_token")))
                            {
                                if (await RefreshApiToken(props))
                                {
                                    context.ShouldRenew = true;
                                }
                            }
                        }
                        if (HttpClientHelper.ClientUser().LoginState != Discord.LoginState.LoggedIn)
                        {
                            //await HttpClientHelper.DiscordClientUserLoginAsync(context.Properties.GetTokenValue("access_token"));
                        }
                        await HttpClientHelper.DiscordClientBotLoginAsync();
                    }
                }
            };
            options.Validate();
        }