|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Security.Claims; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Authentication |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Extension methods to expose Authentication on HttpContext. |
| 13 | + /// </summary> |
| 14 | + public static class AuthenticationHttpContextExtensions |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Extension method for authenticate using the <see cref="AuthenticationOptions.DefaultAuthenticationScheme"/> scheme. |
| 18 | + /// </summary> |
| 19 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 20 | + /// <returns>The <see cref="AuthenticateResult"/>.</returns> |
| 21 | + public static Task<AuthenticateResult> AuthenticateAsync(this HttpContext context) => |
| 22 | + context.AuthenticateAsync(scheme: null); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Extension method for authenticate. |
| 26 | + /// </summary> |
| 27 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 28 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 29 | + /// <returns>The <see cref="AuthenticateResult"/>.</returns> |
| 30 | + public static Task<AuthenticateResult> AuthenticateAsync(this HttpContext context, string scheme) => |
| 31 | + context.RequestServices.GetRequiredService<IAuthenticationService>().AuthenticateAsync(context, scheme); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Extension method for Challenge. |
| 35 | + /// </summary> |
| 36 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 37 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 38 | + /// <returns>The result.</returns> |
| 39 | + public static Task ChallengeAsync(this HttpContext context, string scheme) => |
| 40 | + context.ChallengeAsync(scheme, properties: null); |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Extension method for authenticate using the <see cref="AuthenticationOptions.DefaultChallengeScheme"/> scheme. |
| 44 | + /// </summary> |
| 45 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 46 | + /// <returns>The task.</returns> |
| 47 | + public static Task ChallengeAsync(this HttpContext context) => |
| 48 | + context.ChallengeAsync(scheme: null, properties: null); |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Extension method for Challenge. |
| 52 | + /// </summary> |
| 53 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 54 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 55 | + /// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param> |
| 56 | + /// <returns>The task.</returns> |
| 57 | + public static Task ChallengeAsync(this HttpContext context, string scheme, AuthenticationProperties properties) => |
| 58 | + context.ChallengeAsync(scheme, properties: properties, behavior: ChallengeBehavior.Automatic); |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Extension method for Challenge. |
| 62 | + /// </summary> |
| 63 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 64 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 65 | + /// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param> |
| 66 | + /// <param name="behavior">The <see cref="ChallengeBehavior"/> behavior.</param> |
| 67 | + /// <returns>The task.</returns> |
| 68 | + public static Task ChallengeAsync(this HttpContext context, string scheme, AuthenticationProperties properties, ChallengeBehavior behavior) => |
| 69 | + context.RequestServices.GetRequiredService<IAuthenticationService>().ChallengeAsync(context, scheme, properties, behavior); |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Extension method for Forbid. |
| 73 | + /// </summary> |
| 74 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 75 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 76 | + /// <returns>The task.</returns> |
| 77 | + public static Task ForbidAsync(this HttpContext context, string scheme) => |
| 78 | + context.ForbidAsync(scheme, properties: null); |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Extension method for Forbid. |
| 82 | + /// </summary> |
| 83 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 84 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 85 | + /// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param> |
| 86 | + /// <returns>The task.</returns> |
| 87 | + public static Task ForbidAsync(this HttpContext context, string scheme, AuthenticationProperties properties) => |
| 88 | + context.RequestServices.GetRequiredService<IAuthenticationService>().ChallengeAsync(context, scheme, properties, ChallengeBehavior.Forbidden); |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Extension method for SignIn. |
| 92 | + /// </summary> |
| 93 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 94 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 95 | + /// <param name="principal">The user.</param> |
| 96 | + /// <returns>The task.</returns> |
| 97 | + public static Task SignInAsync(this HttpContext context, string scheme, ClaimsPrincipal principal) => |
| 98 | + context.SignInAsync(scheme, principal, properties: null); |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Extension method for SignIn using the <see cref="AuthenticationOptions.DefaultSignInScheme"/>. |
| 102 | + /// </summary> |
| 103 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 104 | + /// <param name="principal">The user.</param> |
| 105 | + /// <returns>The task.</returns> |
| 106 | + public static Task SignInAsync(this HttpContext context, ClaimsPrincipal principal) => |
| 107 | + context.SignInAsync(scheme: null, principal: principal, properties: null); |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Extension method for SignIn using the <see cref="AuthenticationOptions.DefaultSignInScheme"/>. |
| 111 | + /// </summary> |
| 112 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 113 | + /// <param name="principal">The user.</param> |
| 114 | + /// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param> |
| 115 | + /// <returns>The task.</returns> |
| 116 | + public static Task SignInAsync(this HttpContext context, ClaimsPrincipal principal, AuthenticationProperties properties) => |
| 117 | + context.SignInAsync(scheme: null, principal: principal, properties: properties); |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Extension method for SignIn. |
| 121 | + /// </summary> |
| 122 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 123 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 124 | + /// <param name="principal">The user.</param> |
| 125 | + /// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param> |
| 126 | + /// <returns>The task.</returns> |
| 127 | + public static Task SignInAsync(this HttpContext context, string scheme, ClaimsPrincipal principal, AuthenticationProperties properties) => |
| 128 | + context.RequestServices.GetRequiredService<IAuthenticationService>().SignInAsync(context, scheme, principal, properties); |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Extension method for SignOut. |
| 132 | + /// </summary> |
| 133 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 134 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 135 | + /// <returns>The task.</returns> |
| 136 | + public static Task SignOutAsync(this HttpContext context, string scheme) => context.SignOutAsync(scheme, properties: null); |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Extension method for SignOut. |
| 140 | + /// </summary> |
| 141 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 142 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 143 | + /// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param> |
| 144 | + /// <returns></returns> |
| 145 | + public static Task SignOutAsync(this HttpContext context, string scheme, AuthenticationProperties properties) => |
| 146 | + context.RequestServices.GetRequiredService<IAuthenticationService>().SignOutAsync(context, scheme, properties); |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Extension method for getting the value of an authentication token. |
| 150 | + /// </summary> |
| 151 | + /// <param name="context">The <see cref="HttpContext"/> context.</param> |
| 152 | + /// <param name="scheme">The name of the authentication scheme.</param> |
| 153 | + /// <param name="tokenName">The name of the token.</param> |
| 154 | + /// <returns>The value of the token.</returns> |
| 155 | + public static Task<string> GetTokenAsync(this HttpContext context, string scheme, string tokenName) => |
| 156 | + context.RequestServices.GetRequiredService<IAuthenticationService>().GetTokenAsync(context, scheme, tokenName); |
| 157 | + } |
| 158 | +} |
0 commit comments