From ce0ed3d237e04dc23fe9618c74b0b2139b2e73a0 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 8 Sep 2016 10:01:53 -0700 Subject: [PATCH] Use TaskCache class from Microsoft.Extensions.TaskCache.Sources (#968) Instead of Task.FromResult(0) --- .../Events/CookieAuthenticationEvents.cs | 17 +++++++++-------- .../project.json | 4 ++++ .../Events/JwtBearerEvents.cs | 9 +++++---- .../project.json | 4 ++++ .../Events/OAuthEvents.cs | 5 +++-- .../project.json | 4 ++++ .../Events/OpenIdConnectEvents.cs | 19 ++++++++++--------- .../project.json | 4 ++++ .../Events/TwitterEvents.cs | 5 +++-- .../project.json | 4 ++++ .../AuthenticationHandler.cs | 6 +++--- .../ClaimsTransformationHandler.cs | 7 ++++--- .../Events/RemoteAuthenticationEvents.cs | 5 +++-- .../project.json | 4 ++++ .../ClaimsAuthorizationRequirement.cs | 3 ++- .../DenyAnonymousAuthorizationRequirement.cs | 3 ++- .../NameAuthorizationRequirement.cs | 3 ++- .../RolesAuthorizationRequirement.cs | 3 ++- .../project.json | 6 +++++- 19 files changed, 77 insertions(+), 38 deletions(-) diff --git a/src/Microsoft.AspNetCore.Authentication.Cookies/Events/CookieAuthenticationEvents.cs b/src/Microsoft.AspNetCore.Authentication.Cookies/Events/CookieAuthenticationEvents.cs index ffe687e5f2..4364a2e546 100644 --- a/src/Microsoft.AspNetCore.Authentication.Cookies/Events/CookieAuthenticationEvents.cs +++ b/src/Microsoft.AspNetCore.Authentication.Cookies/Events/CookieAuthenticationEvents.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authentication.Cookies { @@ -17,22 +18,22 @@ namespace Microsoft.AspNetCore.Authentication.Cookies /// /// A delegate assigned to this property will be invoked when the related method is called. /// - public Func OnValidatePrincipal { get; set; } = context => Task.FromResult(0); + public Func OnValidatePrincipal { get; set; } = context => TaskCache.CompletedTask; /// /// A delegate assigned to this property will be invoked when the related method is called. /// - public Func OnSigningIn { get; set; } = context => Task.FromResult(0); + public Func OnSigningIn { get; set; } = context => TaskCache.CompletedTask; /// /// A delegate assigned to this property will be invoked when the related method is called. /// - public Func OnSignedIn { get; set; } = context => Task.FromResult(0); + public Func OnSignedIn { get; set; } = context => TaskCache.CompletedTask; /// /// A delegate assigned to this property will be invoked when the related method is called. /// - public Func OnSigningOut { get; set; } = context => Task.FromResult(0); + public Func OnSigningOut { get; set; } = context => TaskCache.CompletedTask; /// /// A delegate assigned to this property will be invoked when the related method is called. @@ -48,7 +49,7 @@ namespace Microsoft.AspNetCore.Authentication.Cookies { context.Response.Redirect(context.RedirectUri); } - return Task.FromResult(0); + return TaskCache.CompletedTask; }; /// @@ -65,7 +66,7 @@ namespace Microsoft.AspNetCore.Authentication.Cookies { context.Response.Redirect(context.RedirectUri); } - return Task.FromResult(0); + return TaskCache.CompletedTask; }; /// @@ -81,7 +82,7 @@ namespace Microsoft.AspNetCore.Authentication.Cookies { context.Response.Redirect(context.RedirectUri); } - return Task.FromResult(0); + return TaskCache.CompletedTask; }; /// @@ -97,7 +98,7 @@ namespace Microsoft.AspNetCore.Authentication.Cookies { context.Response.Redirect(context.RedirectUri); } - return Task.FromResult(0); + return TaskCache.CompletedTask; }; private static bool IsAjaxRequest(HttpRequest request) diff --git a/src/Microsoft.AspNetCore.Authentication.Cookies/project.json b/src/Microsoft.AspNetCore.Authentication.Cookies/project.json index 7ddff7ce8c..1317797b6a 100644 --- a/src/Microsoft.AspNetCore.Authentication.Cookies/project.json +++ b/src/Microsoft.AspNetCore.Authentication.Cookies/project.json @@ -23,6 +23,10 @@ "dependencies": { "Microsoft.AspNetCore.Authentication": "1.1.0-*", "Microsoft.Extensions.Options": "1.1.0-*", + "Microsoft.Extensions.TaskCache.Sources": { + "version": "1.1.0-*", + "type": "build" + }, "Microsoft.Extensions.WebEncoders": "1.1.0-*" }, "frameworks": { diff --git a/src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/JwtBearerEvents.cs b/src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/JwtBearerEvents.cs index 38a877f668..8ac1c3631e 100644 --- a/src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/JwtBearerEvents.cs +++ b/src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/JwtBearerEvents.cs @@ -3,6 +3,7 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authentication.JwtBearer { @@ -14,22 +15,22 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer /// /// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. /// - public Func OnAuthenticationFailed { get; set; } = context => Task.FromResult(0); + public Func OnAuthenticationFailed { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked when a protocol message is first received. /// - public Func OnMessageReceived { get; set; } = context => Task.FromResult(0); + public Func OnMessageReceived { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. /// - public Func OnTokenValidated { get; set; } = context => Task.FromResult(0); + public Func OnTokenValidated { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked before a challenge is sent back to the caller. /// - public Func OnChallenge { get; set; } = context => Task.FromResult(0); + public Func OnChallenge { get; set; } = context => TaskCache.CompletedTask; public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context); diff --git a/src/Microsoft.AspNetCore.Authentication.JwtBearer/project.json b/src/Microsoft.AspNetCore.Authentication.JwtBearer/project.json index 2f8ea6072a..7697e9b691 100644 --- a/src/Microsoft.AspNetCore.Authentication.JwtBearer/project.json +++ b/src/Microsoft.AspNetCore.Authentication.JwtBearer/project.json @@ -22,6 +22,10 @@ }, "dependencies": { "Microsoft.AspNetCore.Authentication": "1.1.0-*", + "Microsoft.Extensions.TaskCache.Sources": { + "version": "1.1.0-*", + "type": "build" + }, "Microsoft.IdentityModel.Protocols.OpenIdConnect": "2.0.0-*" }, "frameworks": { diff --git a/src/Microsoft.AspNetCore.Authentication.OAuth/Events/OAuthEvents.cs b/src/Microsoft.AspNetCore.Authentication.OAuth/Events/OAuthEvents.cs index 44c4260516..066b324b75 100644 --- a/src/Microsoft.AspNetCore.Authentication.OAuth/Events/OAuthEvents.cs +++ b/src/Microsoft.AspNetCore.Authentication.OAuth/Events/OAuthEvents.cs @@ -3,6 +3,7 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authentication.OAuth { @@ -14,7 +15,7 @@ namespace Microsoft.AspNetCore.Authentication.OAuth /// /// Gets or sets the function that is invoked when the CreatingTicket method is invoked. /// - public Func OnCreatingTicket { get; set; } = context => Task.FromResult(0); + public Func OnCreatingTicket { get; set; } = context => TaskCache.CompletedTask; /// /// Gets or sets the delegate that is invoked when the RedirectToAuthorizationEndpoint method is invoked. @@ -22,7 +23,7 @@ namespace Microsoft.AspNetCore.Authentication.OAuth public Func OnRedirectToAuthorizationEndpoint { get; set; } = context => { context.Response.Redirect(context.RedirectUri); - return Task.FromResult(0); + return TaskCache.CompletedTask; }; /// diff --git a/src/Microsoft.AspNetCore.Authentication.OAuth/project.json b/src/Microsoft.AspNetCore.Authentication.OAuth/project.json index 192801ced6..0365178576 100644 --- a/src/Microsoft.AspNetCore.Authentication.OAuth/project.json +++ b/src/Microsoft.AspNetCore.Authentication.OAuth/project.json @@ -22,6 +22,10 @@ }, "dependencies": { "Microsoft.AspNetCore.Authentication": "1.1.0-*", + "Microsoft.Extensions.TaskCache.Sources": { + "version": "1.1.0-*", + "type": "build" + }, "Newtonsoft.Json": "9.0.1" }, "frameworks": { diff --git a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/Events/OpenIdConnectEvents.cs b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/Events/OpenIdConnectEvents.cs index 42d35b7982..f39b554ece 100644 --- a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/Events/OpenIdConnectEvents.cs +++ b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/Events/OpenIdConnectEvents.cs @@ -3,6 +3,7 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authentication.OpenIdConnect { @@ -14,47 +15,47 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect /// /// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. /// - public Func OnAuthenticationFailed { get; set; } = context => Task.FromResult(0); + public Func OnAuthenticationFailed { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked after security token validation if an authorization code is present in the protocol message. /// - public Func OnAuthorizationCodeReceived { get; set; } = context => Task.FromResult(0); + public Func OnAuthorizationCodeReceived { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked when a protocol message is first received. /// - public Func OnMessageReceived { get; set; } = context => Task.FromResult(0); + public Func OnMessageReceived { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked before redirecting to the identity provider to authenticate. /// - public Func OnRedirectToIdentityProvider { get; set; } = context => Task.FromResult(0); + public Func OnRedirectToIdentityProvider { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked before redirecting to the identity provider to sign out. /// - public Func OnRedirectToIdentityProviderForSignOut { get; set; } = context => Task.FromResult(0); + public Func OnRedirectToIdentityProviderForSignOut { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked when a request is received on the RemoteSignOutPath. /// - public Func OnRemoteSignOut { get; set; } = context => Task.FromResult(0); + public Func OnRemoteSignOut { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked after "authorization code" is redeemed for tokens at the token endpoint. /// - public Func OnTokenResponseReceived { get; set; } = context => Task.FromResult(0); + public Func OnTokenResponseReceived { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked when an IdToken has been validated and produced an AuthenticationTicket. /// - public Func OnTokenValidated { get; set; } = context => Task.FromResult(0); + public Func OnTokenValidated { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked when user information is retrieved from the UserInfoEndpoint. /// - public Func OnUserInformationReceived { get; set; } = context => Task.FromResult(0); + public Func OnUserInformationReceived { get; set; } = context => TaskCache.CompletedTask; public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context); diff --git a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/project.json b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/project.json index 5243a96a73..53379f96d5 100644 --- a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/project.json +++ b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/project.json @@ -22,6 +22,10 @@ }, "dependencies": { "Microsoft.AspNetCore.Authentication": "1.1.0-*", + "Microsoft.Extensions.TaskCache.Sources": { + "version": "1.1.0-*", + "type": "build" + }, "Microsoft.IdentityModel.Protocols.OpenIdConnect": "2.0.0-*" }, "frameworks": { diff --git a/src/Microsoft.AspNetCore.Authentication.Twitter/Events/TwitterEvents.cs b/src/Microsoft.AspNetCore.Authentication.Twitter/Events/TwitterEvents.cs index 21c5b57a7f..033227542a 100644 --- a/src/Microsoft.AspNetCore.Authentication.Twitter/Events/TwitterEvents.cs +++ b/src/Microsoft.AspNetCore.Authentication.Twitter/Events/TwitterEvents.cs @@ -3,6 +3,7 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authentication.Twitter { @@ -14,7 +15,7 @@ namespace Microsoft.AspNetCore.Authentication.Twitter /// /// Gets or sets the function that is invoked when the Authenticated method is invoked. /// - public Func OnCreatingTicket { get; set; } = context => Task.FromResult(0); + public Func OnCreatingTicket { get; set; } = context => TaskCache.CompletedTask; /// /// Gets or sets the delegate that is invoked when the ApplyRedirect method is invoked. @@ -22,7 +23,7 @@ namespace Microsoft.AspNetCore.Authentication.Twitter public Func OnRedirectToAuthorizationEndpoint { get; set; } = context => { context.Response.Redirect(context.RedirectUri); - return Task.FromResult(0); + return TaskCache.CompletedTask; }; /// diff --git a/src/Microsoft.AspNetCore.Authentication.Twitter/project.json b/src/Microsoft.AspNetCore.Authentication.Twitter/project.json index 8696821dd8..9d4aa7b4ed 100644 --- a/src/Microsoft.AspNetCore.Authentication.Twitter/project.json +++ b/src/Microsoft.AspNetCore.Authentication.Twitter/project.json @@ -22,6 +22,10 @@ }, "dependencies": { "Microsoft.AspNetCore.Authentication": "1.1.0-*", + "Microsoft.Extensions.TaskCache.Sources": { + "version": "1.1.0-*", + "type": "build" + }, "Newtonsoft.Json": "9.0.1" }, "frameworks": { diff --git a/src/Microsoft.AspNetCore.Authentication/AuthenticationHandler.cs b/src/Microsoft.AspNetCore.Authentication/AuthenticationHandler.cs index f992cde009..55cfa5b01c 100644 --- a/src/Microsoft.AspNetCore.Authentication/AuthenticationHandler.cs +++ b/src/Microsoft.AspNetCore.Authentication/AuthenticationHandler.cs @@ -142,7 +142,7 @@ namespace Microsoft.AspNetCore.Authentication /// protected virtual Task FinishResponseAsync() { - return Task.FromResult(0); + return TaskCache.CompletedTask; } private async Task HandleAutomaticChallengeIfNeeded() @@ -283,7 +283,7 @@ namespace Microsoft.AspNetCore.Authentication protected virtual Task HandleSignInAsync(SignInContext context) { - return Task.FromResult(0); + return TaskCache.CompletedTask; } public async Task SignOutAsync(SignOutContext context) @@ -308,7 +308,7 @@ namespace Microsoft.AspNetCore.Authentication protected virtual Task HandleSignOutAsync(SignOutContext context) { - return Task.FromResult(0); + return TaskCache.CompletedTask; } protected virtual Task HandleForbiddenAsync(ChallengeContext context) diff --git a/src/Microsoft.AspNetCore.Authentication/ClaimsTransformationHandler.cs b/src/Microsoft.AspNetCore.Authentication/ClaimsTransformationHandler.cs index 7a2c47e401..27965dbf4e 100644 --- a/src/Microsoft.AspNetCore.Authentication/ClaimsTransformationHandler.cs +++ b/src/Microsoft.AspNetCore.Authentication/ClaimsTransformationHandler.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features.Authentication; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authentication { @@ -48,7 +49,7 @@ namespace Microsoft.AspNetCore.Authentication { return PriorHandler.ChallengeAsync(context); } - return Task.FromResult(0); + return TaskCache.CompletedTask; } public void GetDescriptions(DescribeSchemesContext context) @@ -65,7 +66,7 @@ namespace Microsoft.AspNetCore.Authentication { return PriorHandler.SignInAsync(context); } - return Task.FromResult(0); + return TaskCache.CompletedTask; } public Task SignOutAsync(SignOutContext context) @@ -74,7 +75,7 @@ namespace Microsoft.AspNetCore.Authentication { return PriorHandler.SignOutAsync(context); } - return Task.FromResult(0); + return TaskCache.CompletedTask; } public void RegisterAuthenticationHandler(IHttpAuthenticationFeature auth) diff --git a/src/Microsoft.AspNetCore.Authentication/Events/RemoteAuthenticationEvents.cs b/src/Microsoft.AspNetCore.Authentication/Events/RemoteAuthenticationEvents.cs index ee45b8afd9..6e7d6a35c6 100644 --- a/src/Microsoft.AspNetCore.Authentication/Events/RemoteAuthenticationEvents.cs +++ b/src/Microsoft.AspNetCore.Authentication/Events/RemoteAuthenticationEvents.cs @@ -3,14 +3,15 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authentication { public class RemoteAuthenticationEvents : IRemoteAuthenticationEvents { - public Func OnRemoteFailure { get; set; } = context => Task.FromResult(0); + public Func OnRemoteFailure { get; set; } = context => TaskCache.CompletedTask; - public Func OnTicketReceived { get; set; } = context => Task.FromResult(0); + public Func OnTicketReceived { get; set; } = context => TaskCache.CompletedTask; /// /// Invoked when there is a remote failure diff --git a/src/Microsoft.AspNetCore.Authentication/project.json b/src/Microsoft.AspNetCore.Authentication/project.json index 460549c7ba..958170ab7d 100644 --- a/src/Microsoft.AspNetCore.Authentication/project.json +++ b/src/Microsoft.AspNetCore.Authentication/project.json @@ -29,6 +29,10 @@ "type": "build", "version": "1.1.0-*" }, + "Microsoft.Extensions.TaskCache.Sources": { + "version": "1.1.0-*", + "type": "build" + }, "Microsoft.Extensions.Options": "1.1.0-*", "Microsoft.Extensions.WebEncoders": "1.1.0-*" }, diff --git a/src/Microsoft.AspNetCore.Authorization/Infrastructure/ClaimsAuthorizationRequirement.cs b/src/Microsoft.AspNetCore.Authorization/Infrastructure/ClaimsAuthorizationRequirement.cs index 0e28ba0776..4248e4813d 100644 --- a/src/Microsoft.AspNetCore.Authorization/Infrastructure/ClaimsAuthorizationRequirement.cs +++ b/src/Microsoft.AspNetCore.Authorization/Infrastructure/ClaimsAuthorizationRequirement.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authorization.Infrastructure { @@ -67,7 +68,7 @@ namespace Microsoft.AspNetCore.Authorization.Infrastructure context.Succeed(requirement); } } - return Task.FromResult(0); + return TaskCache.CompletedTask; } } } diff --git a/src/Microsoft.AspNetCore.Authorization/Infrastructure/DenyAnonymousAuthorizationRequirement.cs b/src/Microsoft.AspNetCore.Authorization/Infrastructure/DenyAnonymousAuthorizationRequirement.cs index 7f2671775f..5bae319b3e 100644 --- a/src/Microsoft.AspNetCore.Authorization/Infrastructure/DenyAnonymousAuthorizationRequirement.cs +++ b/src/Microsoft.AspNetCore.Authorization/Infrastructure/DenyAnonymousAuthorizationRequirement.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authorization.Infrastructure { @@ -27,7 +28,7 @@ namespace Microsoft.AspNetCore.Authorization.Infrastructure { context.Succeed(requirement); } - return Task.FromResult(0); + return TaskCache.CompletedTask; } } } diff --git a/src/Microsoft.AspNetCore.Authorization/Infrastructure/NameAuthorizationRequirement.cs b/src/Microsoft.AspNetCore.Authorization/Infrastructure/NameAuthorizationRequirement.cs index aca1920d7d..9fb295082b 100644 --- a/src/Microsoft.AspNetCore.Authorization/Infrastructure/NameAuthorizationRequirement.cs +++ b/src/Microsoft.AspNetCore.Authorization/Infrastructure/NameAuthorizationRequirement.cs @@ -4,6 +4,7 @@ using System; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authorization.Infrastructure { @@ -46,7 +47,7 @@ namespace Microsoft.AspNetCore.Authorization.Infrastructure context.Succeed(requirement); } } - return Task.FromResult(0); + return TaskCache.CompletedTask; } } } diff --git a/src/Microsoft.AspNetCore.Authorization/Infrastructure/RolesAuthorizationRequirement.cs b/src/Microsoft.AspNetCore.Authorization/Infrastructure/RolesAuthorizationRequirement.cs index 6e5aa72247..44e2b9a220 100644 --- a/src/Microsoft.AspNetCore.Authorization/Infrastructure/RolesAuthorizationRequirement.cs +++ b/src/Microsoft.AspNetCore.Authorization/Infrastructure/RolesAuthorizationRequirement.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Internal; namespace Microsoft.AspNetCore.Authorization.Infrastructure { @@ -61,7 +62,7 @@ namespace Microsoft.AspNetCore.Authorization.Infrastructure context.Succeed(requirement); } } - return Task.FromResult(0); + return TaskCache.CompletedTask; } } diff --git a/src/Microsoft.AspNetCore.Authorization/project.json b/src/Microsoft.AspNetCore.Authorization/project.json index 8e72b62f82..303ba904a1 100644 --- a/src/Microsoft.AspNetCore.Authorization/project.json +++ b/src/Microsoft.AspNetCore.Authorization/project.json @@ -21,7 +21,11 @@ }, "dependencies": { "Microsoft.Extensions.Logging.Abstractions": "1.1.0-*", - "Microsoft.Extensions.Options": "1.1.0-*" + "Microsoft.Extensions.Options": "1.1.0-*", + "Microsoft.Extensions.TaskCache.Sources": { + "version": "1.1.0-*", + "type": "build" + } }, "frameworks": { "net451": {