// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Authentication.Cookies
{
///
/// This default implementation of the ICookieAuthenticationEvents may be used if the
/// application only needs to override a few of the interface methods. This may be used as a base class
/// or may be instantiated directly.
///
public class CookieAuthenticationEvents : ICookieAuthenticationEvents
{
///
/// A delegate assigned to this property will be invoked when the related method is called.
///
public Func OnValidatePrincipal { get; set; } = context => Task.FromResult(0);
///
/// A delegate assigned to this property will be invoked when the related method is called.
///
public Func OnSigningIn { get; set; } = context => Task.FromResult(0);
///
/// A delegate assigned to this property will be invoked when the related method is called.
///
public Func OnSignedIn { get; set; } = context => Task.FromResult(0);
///
/// A delegate assigned to this property will be invoked when the related method is called.
///
public Func OnSigningOut { get; set; } = context => Task.FromResult(0);
///
/// A delegate assigned to this property will be invoked when the related method is called.
///
public Func OnRedirectToLogin { get; set; } = context =>
{
if (IsAjaxRequest(context.Request))
{
context.Response.Headers["Location"] = context.RedirectUri;
context.Response.StatusCode = 401;
}
else
{
context.Response.Redirect(context.RedirectUri);
}
return Task.FromResult(0);
};
///
/// A delegate assigned to this property will be invoked when the related method is called.
///
public Func OnRedirectToAccessDenied { get; set; } = context =>
{
if (IsAjaxRequest(context.Request))
{
context.Response.Headers["Location"] = context.RedirectUri;
context.Response.StatusCode = 403;
}
else
{
context.Response.Redirect(context.RedirectUri);
}
return Task.FromResult(0);
};
///
/// A delegate assigned to this property will be invoked when the related method is called.
///
public Func OnRedirectToLogout { get; set; } = context =>
{
if (IsAjaxRequest(context.Request))
{
context.Response.Headers["Location"] = context.RedirectUri;
}
else
{
context.Response.Redirect(context.RedirectUri);
}
return Task.FromResult(0);
};
///
/// A delegate assigned to this property will be invoked when the related method is called.
///
public Func OnRedirectToReturnUrl { get; set; } = context =>
{
if (IsAjaxRequest(context.Request))
{
context.Response.Headers["Location"] = context.RedirectUri;
}
else
{
context.Response.Redirect(context.RedirectUri);
}
return Task.FromResult(0);
};
private static bool IsAjaxRequest(HttpRequest request)
{
return string.Equals(request.Query["X-Requested-With"], "XMLHttpRequest", StringComparison.Ordinal) ||
string.Equals(request.Headers["X-Requested-With"], "XMLHttpRequest", StringComparison.Ordinal);
}
///
/// Implements the interface method by invoking the related delegate method.
///
///
///
public virtual Task ValidatePrincipal(CookieValidatePrincipalContext context) => OnValidatePrincipal(context);
///
/// Implements the interface method by invoking the related delegate method.
///
///
public virtual Task SigningIn(CookieSigningInContext context) => OnSigningIn(context);
///
/// Implements the interface method by invoking the related delegate method.
///
///
public virtual Task SignedIn(CookieSignedInContext context) => OnSignedIn(context);
///
/// Implements the interface method by invoking the related delegate method.
///
///
public virtual Task SigningOut(CookieSigningOutContext context) => OnSigningOut(context);
///
/// Implements the interface method by invoking the related delegate method.
///
/// Contains information about the event
public virtual Task RedirectToLogout(CookieRedirectContext context) => OnRedirectToLogout(context);
///
/// Implements the interface method by invoking the related delegate method.
///
/// Contains information about the event
public virtual Task RedirectToLogin(CookieRedirectContext context) => OnRedirectToLogin(context);
///
/// Implements the interface method by invoking the related delegate method.
///
/// Contains information about the event
public virtual Task RedirectToReturnUrl(CookieRedirectContext context) => OnRedirectToReturnUrl(context);
///
/// Implements the interface method by invoking the related delegate method.
///
/// Contains information about the event
public virtual Task RedirectToAccessDenied(CookieRedirectContext context) => OnRedirectToAccessDenied(context);
}
}