public override void OnActionExecuted(ActionExecutedContext context) { _cryptoService = context.HttpContext.RequestServices.GetRequiredService <IAppCryptoService>(); _dummyAuthSpecsOptionsMonitor = context.HttpContext.RequestServices.GetRequiredService <IOptionsMonitor <AppDummyAuthSpecs> >(); try { var response = context.HttpContext.Response; if (response.StatusCode != (int)HttpStatusCode.OK && response.StatusCode != (int)HttpStatusCode.Redirect) { return; } var cookieLifespan = _dummyAuthSpecsOptionsMonitor .CurrentValue .CookiesSettings .SecondStageEnablingCookieLifespanInMins; var nowUtc = DateTimeOffset.UtcNow; var secondStageEnablingCookieValueJson = JsonSerializer.Serialize( new DummyAuthSecondStageEnablingCookieSpecs { ExpiresAt = nowUtc.AddMinutes(cookieLifespan), } ); var encryptedValue = _cryptoService.EncryptToBase64String(secondStageEnablingCookieValueJson); response.Cookies.Append( _dummyAuthSpecsOptionsMonitor.CurrentValue.CookiesSettings.CookieNameForEnableAccessToSecondStage, encryptedValue, new CookieOptions { Secure = true, //0 vital MaxAge = TimeSpan.FromMinutes(cookieLifespan), Expires = nowUtc.AddHours(cookieLifespan), //backwards compatibility ie8 IsEssential = false, } ); } finally { base.OnActionExecuted(context); } //0 using site=none mandates setting secure=true otherwise the cookie will get rejected by chrome }
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { _cryptoService = context.HttpContext.RequestServices.GetRequiredService <IAppCryptoService>(); _dummyAuthSpecsOptionsMonitor = context.HttpContext.RequestServices.GetRequiredService <IOptionsMonitor <AppDummyAuthSpecs> >(); var controller = context.Controller as IDummyTwoFactorAuthController; if (controller == null) { throw new ArgumentException("This filter is specific to [I]LoginController - no other controller should be using it", nameof(context)); } try { var cookieFound = context .HttpContext .Request .Cookies .TryGetValue( key: _dummyAuthSpecsOptionsMonitor.CurrentValue.CookiesSettings.CookieNameForEnableAccessToSecondStage, value: out var cookie ); if (!cookieFound) { SetRedirectionToLoginFirstStage(context, controller); return; } var isValid = ValidateCookieValue(cookie); if (!isValid) { SetRedirectionToLoginFirstStage(context, controller); } } finally { await base.OnActionExecutionAsync(context, next); } }