public override async Task RedirectToLogin(CookieRedirectContext context) { if(context.Request.Path.Value.ToLower().StartsWith("/api")) //return the correct stuff for api results { context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; } else { await base.RedirectToLogin(context); } }
private async Task ApplyHeaders(bool shouldRedirectToReturnUrl) { Response.Headers[HeaderNames.CacheControl] = HeaderValueNoCache; Response.Headers[HeaderNames.Pragma] = HeaderValueNoCache; Response.Headers[HeaderNames.Expires] = HeaderValueMinusOne; if (shouldRedirectToReturnUrl && Response.StatusCode == 200) { var query = Request.Query; var redirectUri = query[Options.ReturnUrlParameter]; if (!StringValues.IsNullOrEmpty(redirectUri) && IsHostRelative(redirectUri)) { var redirectContext = new CookieRedirectContext(Context, Options, redirectUri); await Options.Events.RedirectToReturnUrl(redirectContext); } } }
protected override async Task <bool> HandleUnauthorizedAsync(ChallengeContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var redirectUri = new AuthenticationProperties(context.Properties).RedirectUri; if (string.IsNullOrEmpty(redirectUri)) { redirectUri = OriginalPathBase + Request.Path + Request.QueryString; } var loginUri = Options.LoginPath + QueryString.Create(Options.ReturnUrlParameter, redirectUri); var redirectContext = new CookieRedirectContext(Context, Options, BuildRedirectUri(loginUri)); await Options.Events.RedirectToLogin(redirectContext); return(true); }
public Task OnCustomRedirectToLogin(CookieRedirectContext context) { var actionContext = context.HttpContext.RequestServices.GetRequiredService<IActionContextAccessor>(); if (actionContext.ActionContext == null) return _old(context); if (actionContext.ActionContext.ActionDescriptor.FilterDescriptors.Any(x => x.Filter is AjaxAttribute)) { // this is an ajax request, return custom JSON telling user that they must be authenticated. var serializerSettings = context .HttpContext .RequestServices .GetRequiredService<IOptions<MvcJsonOptions>>() .Value .SerializerSettings; context.Response.ContentType = "application/json"; using (var writer = new HttpResponseStreamWriter(context.Response.Body, Encoding.UTF8)) { using (var jsonWriter = new JsonTextWriter(writer)) { jsonWriter.CloseOutput = false; var jsonSerializer = JsonSerializer.Create(serializerSettings); jsonSerializer.Serialize(jsonWriter, new { success = false, error = "You must be signed in." }); } } return Task.FromResult(0); } else { // this is a normal request to an endpoint that is secured. // do what ASP.NET used to do. return _old(context); } }
/// <summary> /// Implements the interface method by invoking the related delegate method. /// </summary> /// <param name="context">Contains information about the event</param> public virtual Task RedirectToAccessDenied(CookieRedirectContext context) => OnRedirectToAccessDenied(context);
/// <summary> /// Implements the interface method by invoking the related delegate method. /// </summary> /// <param name="context">Contains information about the event</param> public virtual Task RedirectToReturnUrl(CookieRedirectContext context) => OnRedirectToReturnUrl(context);
/// <summary> /// Implements the interface method by invoking the related delegate method. /// </summary> /// <param name="context">Contains information about the event</param> public virtual Task RedirectToLogin(CookieRedirectContext context) => OnRedirectToLogin(context);
/// <summary> /// Implements the interface method by invoking the related delegate method. /// </summary> /// <param name="context">Contains information about the event</param> public virtual Task RedirectToLogin(CookieRedirectContext context) => OnRedirect(context);
/// <summary> /// Implements the interface method by invoking the related delegate method. /// </summary> /// <param name="context">Contains information about the event</param> public virtual Task RedirectToAccessDenied(CookieRedirectContext context) => OnRedirect(context);
/// <summary> /// Implements the interface method by invoking the related delegate method. /// </summary> /// <param name="context">Contains information about the event</param> public virtual Task RedirectToReturnUrl(CookieRedirectContext context) => OnRedirect(context);