Use TaskCache class from Microsoft.Extensions.TaskCache.Sources (#968)

Instead of Task.FromResult(0)
This commit is contained in:
Pavel Krymets 2016-09-08 10:01:53 -07:00 committed by GitHub
parent 5b323e5ba1
commit ce0ed3d237
19 changed files with 77 additions and 38 deletions

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authentication.Cookies namespace Microsoft.AspNetCore.Authentication.Cookies
{ {
@ -17,22 +18,22 @@ namespace Microsoft.AspNetCore.Authentication.Cookies
/// <summary> /// <summary>
/// A delegate assigned to this property will be invoked when the related method is called. /// A delegate assigned to this property will be invoked when the related method is called.
/// </summary> /// </summary>
public Func<CookieValidatePrincipalContext, Task> OnValidatePrincipal { get; set; } = context => Task.FromResult(0); public Func<CookieValidatePrincipalContext, Task> OnValidatePrincipal { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// A delegate assigned to this property will be invoked when the related method is called. /// A delegate assigned to this property will be invoked when the related method is called.
/// </summary> /// </summary>
public Func<CookieSigningInContext, Task> OnSigningIn { get; set; } = context => Task.FromResult(0); public Func<CookieSigningInContext, Task> OnSigningIn { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// A delegate assigned to this property will be invoked when the related method is called. /// A delegate assigned to this property will be invoked when the related method is called.
/// </summary> /// </summary>
public Func<CookieSignedInContext, Task> OnSignedIn { get; set; } = context => Task.FromResult(0); public Func<CookieSignedInContext, Task> OnSignedIn { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// A delegate assigned to this property will be invoked when the related method is called. /// A delegate assigned to this property will be invoked when the related method is called.
/// </summary> /// </summary>
public Func<CookieSigningOutContext, Task> OnSigningOut { get; set; } = context => Task.FromResult(0); public Func<CookieSigningOutContext, Task> OnSigningOut { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// A delegate assigned to this property will be invoked when the related method is called. /// 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); context.Response.Redirect(context.RedirectUri);
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
}; };
/// <summary> /// <summary>
@ -65,7 +66,7 @@ namespace Microsoft.AspNetCore.Authentication.Cookies
{ {
context.Response.Redirect(context.RedirectUri); context.Response.Redirect(context.RedirectUri);
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
}; };
/// <summary> /// <summary>
@ -81,7 +82,7 @@ namespace Microsoft.AspNetCore.Authentication.Cookies
{ {
context.Response.Redirect(context.RedirectUri); context.Response.Redirect(context.RedirectUri);
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
}; };
/// <summary> /// <summary>
@ -97,7 +98,7 @@ namespace Microsoft.AspNetCore.Authentication.Cookies
{ {
context.Response.Redirect(context.RedirectUri); context.Response.Redirect(context.RedirectUri);
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
}; };
private static bool IsAjaxRequest(HttpRequest request) private static bool IsAjaxRequest(HttpRequest request)

View File

@ -23,6 +23,10 @@
"dependencies": { "dependencies": {
"Microsoft.AspNetCore.Authentication": "1.1.0-*", "Microsoft.AspNetCore.Authentication": "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"
},
"Microsoft.Extensions.WebEncoders": "1.1.0-*" "Microsoft.Extensions.WebEncoders": "1.1.0-*"
}, },
"frameworks": { "frameworks": {

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authentication.JwtBearer namespace Microsoft.AspNetCore.Authentication.JwtBearer
{ {
@ -14,22 +15,22 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer
/// <summary> /// <summary>
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. /// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
/// </summary> /// </summary>
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.FromResult(0); public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked when a protocol message is first received. /// Invoked when a protocol message is first received.
/// </summary> /// </summary>
public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => Task.FromResult(0); public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
/// </summary> /// </summary>
public Func<TokenValidatedContext, Task> OnTokenValidated { get; set; } = context => Task.FromResult(0); public Func<TokenValidatedContext, Task> OnTokenValidated { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked before a challenge is sent back to the caller. /// Invoked before a challenge is sent back to the caller.
/// </summary> /// </summary>
public Func<JwtBearerChallengeContext, Task> OnChallenge { get; set; } = context => Task.FromResult(0); public Func<JwtBearerChallengeContext, Task> OnChallenge { get; set; } = context => TaskCache.CompletedTask;
public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context); public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context);

View File

@ -22,6 +22,10 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNetCore.Authentication": "1.1.0-*", "Microsoft.AspNetCore.Authentication": "1.1.0-*",
"Microsoft.Extensions.TaskCache.Sources": {
"version": "1.1.0-*",
"type": "build"
},
"Microsoft.IdentityModel.Protocols.OpenIdConnect": "2.0.0-*" "Microsoft.IdentityModel.Protocols.OpenIdConnect": "2.0.0-*"
}, },
"frameworks": { "frameworks": {

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authentication.OAuth namespace Microsoft.AspNetCore.Authentication.OAuth
{ {
@ -14,7 +15,7 @@ namespace Microsoft.AspNetCore.Authentication.OAuth
/// <summary> /// <summary>
/// Gets or sets the function that is invoked when the CreatingTicket method is invoked. /// Gets or sets the function that is invoked when the CreatingTicket method is invoked.
/// </summary> /// </summary>
public Func<OAuthCreatingTicketContext, Task> OnCreatingTicket { get; set; } = context => Task.FromResult(0); public Func<OAuthCreatingTicketContext, Task> OnCreatingTicket { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Gets or sets the delegate that is invoked when the RedirectToAuthorizationEndpoint method is invoked. /// 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<OAuthRedirectToAuthorizationContext, Task> OnRedirectToAuthorizationEndpoint { get; set; } = context => public Func<OAuthRedirectToAuthorizationContext, Task> OnRedirectToAuthorizationEndpoint { get; set; } = context =>
{ {
context.Response.Redirect(context.RedirectUri); context.Response.Redirect(context.RedirectUri);
return Task.FromResult(0); return TaskCache.CompletedTask;
}; };
/// <summary> /// <summary>

View File

@ -22,6 +22,10 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNetCore.Authentication": "1.1.0-*", "Microsoft.AspNetCore.Authentication": "1.1.0-*",
"Microsoft.Extensions.TaskCache.Sources": {
"version": "1.1.0-*",
"type": "build"
},
"Newtonsoft.Json": "9.0.1" "Newtonsoft.Json": "9.0.1"
}, },
"frameworks": { "frameworks": {

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authentication.OpenIdConnect namespace Microsoft.AspNetCore.Authentication.OpenIdConnect
{ {
@ -14,47 +15,47 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect
/// <summary> /// <summary>
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. /// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
/// </summary> /// </summary>
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.FromResult(0); public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked after security token validation if an authorization code is present in the protocol message. /// Invoked after security token validation if an authorization code is present in the protocol message.
/// </summary> /// </summary>
public Func<AuthorizationCodeReceivedContext, Task> OnAuthorizationCodeReceived { get; set; } = context => Task.FromResult(0); public Func<AuthorizationCodeReceivedContext, Task> OnAuthorizationCodeReceived { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked when a protocol message is first received. /// Invoked when a protocol message is first received.
/// </summary> /// </summary>
public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => Task.FromResult(0); public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked before redirecting to the identity provider to authenticate. /// Invoked before redirecting to the identity provider to authenticate.
/// </summary> /// </summary>
public Func<RedirectContext, Task> OnRedirectToIdentityProvider { get; set; } = context => Task.FromResult(0); public Func<RedirectContext, Task> OnRedirectToIdentityProvider { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked before redirecting to the identity provider to sign out. /// Invoked before redirecting to the identity provider to sign out.
/// </summary> /// </summary>
public Func<RedirectContext, Task> OnRedirectToIdentityProviderForSignOut { get; set; } = context => Task.FromResult(0); public Func<RedirectContext, Task> OnRedirectToIdentityProviderForSignOut { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked when a request is received on the RemoteSignOutPath. /// Invoked when a request is received on the RemoteSignOutPath.
/// </summary> /// </summary>
public Func<RemoteSignOutContext, Task> OnRemoteSignOut { get; set; } = context => Task.FromResult(0); public Func<RemoteSignOutContext, Task> OnRemoteSignOut { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked after "authorization code" is redeemed for tokens at the token endpoint. /// Invoked after "authorization code" is redeemed for tokens at the token endpoint.
/// </summary> /// </summary>
public Func<TokenResponseReceivedContext, Task> OnTokenResponseReceived { get; set; } = context => Task.FromResult(0); public Func<TokenResponseReceivedContext, Task> OnTokenResponseReceived { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked when an IdToken has been validated and produced an AuthenticationTicket. /// Invoked when an IdToken has been validated and produced an AuthenticationTicket.
/// </summary> /// </summary>
public Func<TokenValidatedContext, Task> OnTokenValidated { get; set; } = context => Task.FromResult(0); public Func<TokenValidatedContext, Task> OnTokenValidated { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked when user information is retrieved from the UserInfoEndpoint. /// Invoked when user information is retrieved from the UserInfoEndpoint.
/// </summary> /// </summary>
public Func<UserInformationReceivedContext, Task> OnUserInformationReceived { get; set; } = context => Task.FromResult(0); public Func<UserInformationReceivedContext, Task> OnUserInformationReceived { get; set; } = context => TaskCache.CompletedTask;
public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context); public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context);

View File

@ -22,6 +22,10 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNetCore.Authentication": "1.1.0-*", "Microsoft.AspNetCore.Authentication": "1.1.0-*",
"Microsoft.Extensions.TaskCache.Sources": {
"version": "1.1.0-*",
"type": "build"
},
"Microsoft.IdentityModel.Protocols.OpenIdConnect": "2.0.0-*" "Microsoft.IdentityModel.Protocols.OpenIdConnect": "2.0.0-*"
}, },
"frameworks": { "frameworks": {

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authentication.Twitter namespace Microsoft.AspNetCore.Authentication.Twitter
{ {
@ -14,7 +15,7 @@ namespace Microsoft.AspNetCore.Authentication.Twitter
/// <summary> /// <summary>
/// Gets or sets the function that is invoked when the Authenticated method is invoked. /// Gets or sets the function that is invoked when the Authenticated method is invoked.
/// </summary> /// </summary>
public Func<TwitterCreatingTicketContext, Task> OnCreatingTicket { get; set; } = context => Task.FromResult(0); public Func<TwitterCreatingTicketContext, Task> OnCreatingTicket { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Gets or sets the delegate that is invoked when the ApplyRedirect method is invoked. /// 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<TwitterRedirectToAuthorizationEndpointContext, Task> OnRedirectToAuthorizationEndpoint { get; set; } = context => public Func<TwitterRedirectToAuthorizationEndpointContext, Task> OnRedirectToAuthorizationEndpoint { get; set; } = context =>
{ {
context.Response.Redirect(context.RedirectUri); context.Response.Redirect(context.RedirectUri);
return Task.FromResult(0); return TaskCache.CompletedTask;
}; };
/// <summary> /// <summary>

View File

@ -22,6 +22,10 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNetCore.Authentication": "1.1.0-*", "Microsoft.AspNetCore.Authentication": "1.1.0-*",
"Microsoft.Extensions.TaskCache.Sources": {
"version": "1.1.0-*",
"type": "build"
},
"Newtonsoft.Json": "9.0.1" "Newtonsoft.Json": "9.0.1"
}, },
"frameworks": { "frameworks": {

View File

@ -142,7 +142,7 @@ namespace Microsoft.AspNetCore.Authentication
/// <returns></returns> /// <returns></returns>
protected virtual Task FinishResponseAsync() protected virtual Task FinishResponseAsync()
{ {
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
private async Task HandleAutomaticChallengeIfNeeded() private async Task HandleAutomaticChallengeIfNeeded()
@ -283,7 +283,7 @@ namespace Microsoft.AspNetCore.Authentication
protected virtual Task HandleSignInAsync(SignInContext context) protected virtual Task HandleSignInAsync(SignInContext context)
{ {
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
public async Task SignOutAsync(SignOutContext context) public async Task SignOutAsync(SignOutContext context)
@ -308,7 +308,7 @@ namespace Microsoft.AspNetCore.Authentication
protected virtual Task HandleSignOutAsync(SignOutContext context) protected virtual Task HandleSignOutAsync(SignOutContext context)
{ {
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
protected virtual Task<bool> HandleForbiddenAsync(ChallengeContext context) protected virtual Task<bool> HandleForbiddenAsync(ChallengeContext context)

View File

@ -4,6 +4,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features.Authentication; using Microsoft.AspNetCore.Http.Features.Authentication;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authentication namespace Microsoft.AspNetCore.Authentication
{ {
@ -48,7 +49,7 @@ namespace Microsoft.AspNetCore.Authentication
{ {
return PriorHandler.ChallengeAsync(context); return PriorHandler.ChallengeAsync(context);
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
public void GetDescriptions(DescribeSchemesContext context) public void GetDescriptions(DescribeSchemesContext context)
@ -65,7 +66,7 @@ namespace Microsoft.AspNetCore.Authentication
{ {
return PriorHandler.SignInAsync(context); return PriorHandler.SignInAsync(context);
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
public Task SignOutAsync(SignOutContext context) public Task SignOutAsync(SignOutContext context)
@ -74,7 +75,7 @@ namespace Microsoft.AspNetCore.Authentication
{ {
return PriorHandler.SignOutAsync(context); return PriorHandler.SignOutAsync(context);
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
public void RegisterAuthenticationHandler(IHttpAuthenticationFeature auth) public void RegisterAuthenticationHandler(IHttpAuthenticationFeature auth)

View File

@ -3,14 +3,15 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authentication namespace Microsoft.AspNetCore.Authentication
{ {
public class RemoteAuthenticationEvents : IRemoteAuthenticationEvents public class RemoteAuthenticationEvents : IRemoteAuthenticationEvents
{ {
public Func<FailureContext, Task> OnRemoteFailure { get; set; } = context => Task.FromResult(0); public Func<FailureContext, Task> OnRemoteFailure { get; set; } = context => TaskCache.CompletedTask;
public Func<TicketReceivedContext, Task> OnTicketReceived { get; set; } = context => Task.FromResult(0); public Func<TicketReceivedContext, Task> OnTicketReceived { get; set; } = context => TaskCache.CompletedTask;
/// <summary> /// <summary>
/// Invoked when there is a remote failure /// Invoked when there is a remote failure

View File

@ -29,6 +29,10 @@
"type": "build", "type": "build",
"version": "1.1.0-*" "version": "1.1.0-*"
}, },
"Microsoft.Extensions.TaskCache.Sources": {
"version": "1.1.0-*",
"type": "build"
},
"Microsoft.Extensions.Options": "1.1.0-*", "Microsoft.Extensions.Options": "1.1.0-*",
"Microsoft.Extensions.WebEncoders": "1.1.0-*" "Microsoft.Extensions.WebEncoders": "1.1.0-*"
}, },

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authorization.Infrastructure namespace Microsoft.AspNetCore.Authorization.Infrastructure
{ {
@ -67,7 +68,7 @@ namespace Microsoft.AspNetCore.Authorization.Infrastructure
context.Succeed(requirement); context.Succeed(requirement);
} }
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
} }
} }

View File

@ -3,6 +3,7 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authorization.Infrastructure namespace Microsoft.AspNetCore.Authorization.Infrastructure
{ {
@ -27,7 +28,7 @@ namespace Microsoft.AspNetCore.Authorization.Infrastructure
{ {
context.Succeed(requirement); context.Succeed(requirement);
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
} }
} }

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authorization.Infrastructure namespace Microsoft.AspNetCore.Authorization.Infrastructure
{ {
@ -46,7 +47,7 @@ namespace Microsoft.AspNetCore.Authorization.Infrastructure
context.Succeed(requirement); context.Succeed(requirement);
} }
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
} }
} }

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Authorization.Infrastructure namespace Microsoft.AspNetCore.Authorization.Infrastructure
{ {
@ -61,7 +62,7 @@ namespace Microsoft.AspNetCore.Authorization.Infrastructure
context.Succeed(requirement); context.Succeed(requirement);
} }
} }
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
} }

View File

@ -21,7 +21,11 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.Extensions.Logging.Abstractions": "1.1.0-*", "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": { "frameworks": {
"net451": { "net451": {