// 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.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Authentication { /// /// Extension methods to expose Authentication on HttpContext. /// public static class AuthenticationHttpContextExtensions { /// /// Extension method for authenticate using the scheme. /// /// The context. /// The . public static Task AuthenticateAsync(this HttpContext context) => context.AuthenticateAsync(scheme: null); /// /// Extension method for authenticate. /// /// The context. /// The name of the authentication scheme. /// The . public static Task AuthenticateAsync(this HttpContext context, string scheme) => context.RequestServices.GetRequiredService().AuthenticateAsync(context, scheme); /// /// Extension method for Challenge. /// /// The context. /// The name of the authentication scheme. /// The result. public static Task ChallengeAsync(this HttpContext context, string scheme) => context.ChallengeAsync(scheme, properties: null); /// /// Extension method for authenticate using the scheme. /// /// The context. /// The task. public static Task ChallengeAsync(this HttpContext context) => context.ChallengeAsync(scheme: null, properties: null); /// /// Extension method for authenticate using the scheme. /// /// The context. /// The properties. /// The task. public static Task ChallengeAsync(this HttpContext context, AuthenticationProperties properties) => context.ChallengeAsync(scheme: null, properties: properties); /// /// Extension method for Challenge. /// /// The context. /// The name of the authentication scheme. /// The properties. /// The task. public static Task ChallengeAsync(this HttpContext context, string scheme, AuthenticationProperties properties) => context.ChallengeAsync(scheme, properties: properties, behavior: ChallengeBehavior.Automatic); /// /// Extension method for Challenge. /// /// The context. /// The name of the authentication scheme. /// The properties. /// The behavior. /// The task. public static Task ChallengeAsync(this HttpContext context, string scheme, AuthenticationProperties properties, ChallengeBehavior behavior) => context.RequestServices.GetRequiredService().ChallengeAsync(context, scheme, properties, behavior); /// /// Extension method for Forbid. /// /// The context. /// The name of the authentication scheme. /// The task. public static Task ForbidAsync(this HttpContext context, string scheme) => context.ForbidAsync(scheme, properties: null); /// /// Extension method for Forbid. /// /// The context. /// The task. public static Task ForbidAsync(this HttpContext context) => context.ForbidAsync(scheme: null, properties: null); /// /// Extension method for Forbid. /// /// The context. /// The properties. /// The task. public static Task ForbidAsync(this HttpContext context, AuthenticationProperties properties) => context.ForbidAsync(scheme: null, properties: properties); /// /// Extension method for Forbid. /// /// The context. /// The name of the authentication scheme. /// The properties. /// The task. public static Task ForbidAsync(this HttpContext context, string scheme, AuthenticationProperties properties) => context.RequestServices.GetRequiredService().ChallengeAsync(context, scheme, properties, ChallengeBehavior.Forbidden); /// /// Extension method for SignIn. /// /// The context. /// The name of the authentication scheme. /// The user. /// The task. public static Task SignInAsync(this HttpContext context, string scheme, ClaimsPrincipal principal) => context.SignInAsync(scheme, principal, properties: null); /// /// Extension method for SignIn using the . /// /// The context. /// The user. /// The task. public static Task SignInAsync(this HttpContext context, ClaimsPrincipal principal) => context.SignInAsync(scheme: null, principal: principal, properties: null); /// /// Extension method for SignIn using the . /// /// The context. /// The user. /// The properties. /// The task. public static Task SignInAsync(this HttpContext context, ClaimsPrincipal principal, AuthenticationProperties properties) => context.SignInAsync(scheme: null, principal: principal, properties: properties); /// /// Extension method for SignIn. /// /// The context. /// The name of the authentication scheme. /// The user. /// The properties. /// The task. public static Task SignInAsync(this HttpContext context, string scheme, ClaimsPrincipal principal, AuthenticationProperties properties) => context.RequestServices.GetRequiredService().SignInAsync(context, scheme, principal, properties); /// /// Extension method for SignOut. /// /// The context. /// The name of the authentication scheme. /// The task. public static Task SignOutAsync(this HttpContext context, string scheme) => context.SignOutAsync(scheme, properties: null); /// /// Extension method for SignOut. /// /// The context. /// The name of the authentication scheme. /// The properties. /// public static Task SignOutAsync(this HttpContext context, string scheme, AuthenticationProperties properties) => context.RequestServices.GetRequiredService().SignOutAsync(context, scheme, properties); /// /// Extension method for getting the value of an authentication token. /// /// The context. /// The name of the authentication scheme. /// The name of the token. /// The value of the token. public static Task GetTokenAsync(this HttpContext context, string scheme, string tokenName) => context.RequestServices.GetRequiredService().GetTokenAsync(context, scheme, tokenName); } }