From 78f472fd205599850119dfbd70f46b7334c06936 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Wed, 15 Oct 2014 23:42:45 -0700 Subject: [PATCH] Switch to automatically ensure request services --- samples/CookieSample/Startup.cs | 1 - samples/CookieSessionSample/Startup.cs | 1 - .../CookieAuthenticationMiddleware.cs | 3 ++- .../FacebookAuthenticationMiddleware.cs | 3 ++- .../GoogleAuthenticationMiddleware.cs | 4 +++- ...icrosoftAccountAuthenticationMiddleware.cs | 4 +++- .../OAuthAuthenticationMiddleware.cs | 4 +++- .../OAuthBearerAuthenticationMiddleware.cs | 4 +++- .../TwitterAuthenticationMiddleware.cs | 4 +++- .../AuthenticationMiddleware.cs | 20 ++++++++++++------- src/Microsoft.AspNet.Security/project.json | 1 + .../Cookies/CookieMiddlewareTests.cs | 1 - 12 files changed, 33 insertions(+), 17 deletions(-) diff --git a/samples/CookieSample/Startup.cs b/samples/CookieSample/Startup.cs index fc78aec37d..cff673d752 100644 --- a/samples/CookieSample/Startup.cs +++ b/samples/CookieSample/Startup.cs @@ -9,7 +9,6 @@ namespace CookieSample { public void Configure(IApplicationBuilder app) { - app.UseServices(services => { }); app.UseCookieAuthentication(options => { }); diff --git a/samples/CookieSessionSample/Startup.cs b/samples/CookieSessionSample/Startup.cs index e03c034f37..2b6cfd8464 100644 --- a/samples/CookieSessionSample/Startup.cs +++ b/samples/CookieSessionSample/Startup.cs @@ -10,7 +10,6 @@ namespace CookieSessionSample { public void Configure(IApplicationBuilder app) { - app.UseRequestServices(); app.UseCookieAuthentication(options => { options.SessionStore = new MemoryCacheSessionStore(); diff --git a/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationMiddleware.cs b/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationMiddleware.cs index 087820e295..fdbd3cc081 100644 --- a/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationMiddleware.cs @@ -18,11 +18,12 @@ namespace Microsoft.AspNet.Security.Cookies private readonly ILogger _logger; public CookieAuthenticationMiddleware(RequestDelegate next, + IServiceProvider services, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions options, ConfigureOptions configureOptions) - : base(next, options, configureOptions) + : base(next, services, options, configureOptions) { if (Options.Notifications == null) { diff --git a/src/Microsoft.AspNet.Security.Facebook/FacebookAuthenticationMiddleware.cs b/src/Microsoft.AspNet.Security.Facebook/FacebookAuthenticationMiddleware.cs index b7891d139d..5f6eb525eb 100644 --- a/src/Microsoft.AspNet.Security.Facebook/FacebookAuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Security.Facebook/FacebookAuthenticationMiddleware.cs @@ -26,12 +26,13 @@ namespace Microsoft.AspNet.Security.Facebook /// Configuration options for the middleware. public FacebookAuthenticationMiddleware( RequestDelegate next, + IServiceProvider services, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions externalOptions, IOptions options, ConfigureOptions configureOptions = null) - : base(next, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions) + : base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions) { if (string.IsNullOrWhiteSpace(Options.AppId)) { diff --git a/src/Microsoft.AspNet.Security.Google/GoogleAuthenticationMiddleware.cs b/src/Microsoft.AspNet.Security.Google/GoogleAuthenticationMiddleware.cs index 51072c65c4..7de2530704 100644 --- a/src/Microsoft.AspNet.Security.Google/GoogleAuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Security.Google/GoogleAuthenticationMiddleware.cs @@ -25,17 +25,19 @@ namespace Microsoft.AspNet.Security.Google /// Initializes a new . /// /// The next middleware in the HTTP pipeline to invoke. + /// /// /// /// Configuration options for the middleware. public GoogleAuthenticationMiddleware( RequestDelegate next, + IServiceProvider services, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions externalOptions, IOptions options, ConfigureOptions configureOptions = null) - : base(next, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions) + : base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions) { if (Options.Notifications == null) { diff --git a/src/Microsoft.AspNet.Security.MicrosoftAccount/MicrosoftAccountAuthenticationMiddleware.cs b/src/Microsoft.AspNet.Security.MicrosoftAccount/MicrosoftAccountAuthenticationMiddleware.cs index 26c80bac4b..ce50b7033e 100644 --- a/src/Microsoft.AspNet.Security.MicrosoftAccount/MicrosoftAccountAuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Security.MicrosoftAccount/MicrosoftAccountAuthenticationMiddleware.cs @@ -21,17 +21,19 @@ namespace Microsoft.AspNet.Security.MicrosoftAccount /// Initializes a new . /// /// The next middleware in the HTTP pipeline to invoke. + /// /// /// /// Configuration options for the middleware. public MicrosoftAccountAuthenticationMiddleware( RequestDelegate next, + IServiceProvider services, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions externalOptions, IOptions options, ConfigureOptions configureOptions = null) - : base(next, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions) + : base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions) { if (Options.Notifications == null) { diff --git a/src/Microsoft.AspNet.Security.OAuth/OAuthAuthenticationMiddleware.cs b/src/Microsoft.AspNet.Security.OAuth/OAuthAuthenticationMiddleware.cs index 23160b0532..1f9eace704 100644 --- a/src/Microsoft.AspNet.Security.OAuth/OAuthAuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Security.OAuth/OAuthAuthenticationMiddleware.cs @@ -26,17 +26,19 @@ namespace Microsoft.AspNet.Security.OAuth /// Initializes a new . /// /// The next middleware in the HTTP pipeline to invoke. + /// /// /// /// Configuration options for the middleware. public OAuthAuthenticationMiddleware( RequestDelegate next, + IServiceProvider services, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions externalOptions, IOptions options, ConfigureOptions configureOptions = null) - : base(next, options, configureOptions) + : base(next, services, options, configureOptions) { // todo: review error handling if (string.IsNullOrWhiteSpace(Options.AuthenticationType)) diff --git a/src/Microsoft.AspNet.Security.OAuth/OAuthBearerAuthenticationMiddleware.cs b/src/Microsoft.AspNet.Security.OAuth/OAuthBearerAuthenticationMiddleware.cs index 8993e107cd..eaa862a280 100644 --- a/src/Microsoft.AspNet.Security.OAuth/OAuthBearerAuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Security.OAuth/OAuthBearerAuthenticationMiddleware.cs @@ -7,6 +7,7 @@ using Microsoft.AspNet.Security.DataProtection; using Microsoft.AspNet.Security.Infrastructure; using Microsoft.Framework.Logging; using Microsoft.Framework.OptionsModel; +using System; namespace Microsoft.AspNet.Security.OAuth { @@ -28,11 +29,12 @@ namespace Microsoft.AspNet.Security.OAuth /// public OAuthBearerAuthenticationMiddleware( RequestDelegate next, + IServiceProvider services, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions options, ConfigureOptions configureOptions) - : base(next, options, configureOptions) + : base(next, services, options, configureOptions) { _logger = loggerFactory.Create(); diff --git a/src/Microsoft.AspNet.Security.Twitter/TwitterAuthenticationMiddleware.cs b/src/Microsoft.AspNet.Security.Twitter/TwitterAuthenticationMiddleware.cs index 7ca77a9c1f..e3a56efab5 100644 --- a/src/Microsoft.AspNet.Security.Twitter/TwitterAuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Security.Twitter/TwitterAuthenticationMiddleware.cs @@ -29,17 +29,19 @@ namespace Microsoft.AspNet.Security.Twitter /// Initializes a /// /// The next middleware in the HTTP pipeline to invoke + /// /// /// /// Configuration options for the middleware public TwitterAuthenticationMiddleware( RequestDelegate next, + IServiceProvider services, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions externalOptions, IOptions options, ConfigureOptions configureOptions = null) - : base(next, options, configureOptions) + : base(next, services, options, configureOptions) { if (string.IsNullOrWhiteSpace(Options.ConsumerSecret)) { diff --git a/src/Microsoft.AspNet.Security/Infrastructure/AuthenticationMiddleware.cs b/src/Microsoft.AspNet.Security/Infrastructure/AuthenticationMiddleware.cs index 86c2fb921f..b7fb77fe81 100644 --- a/src/Microsoft.AspNet.Security/Infrastructure/AuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Security/Infrastructure/AuthenticationMiddleware.cs @@ -6,6 +6,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; +using Microsoft.AspNet.RequestContainer; using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Security.Infrastructure @@ -13,8 +14,9 @@ namespace Microsoft.AspNet.Security.Infrastructure public abstract class AuthenticationMiddleware where TOptions : AuthenticationOptions, new() { private readonly RequestDelegate _next; + private readonly IServiceProvider _services; - protected AuthenticationMiddleware([NotNull] RequestDelegate next, [NotNull] IOptions options, ConfigureOptions configureOptions) + protected AuthenticationMiddleware([NotNull] RequestDelegate next, [NotNull] IServiceProvider services, [NotNull] IOptions options, ConfigureOptions configureOptions) { if (configureOptions != null) { @@ -26,6 +28,7 @@ namespace Microsoft.AspNet.Security.Infrastructure Options = options.Options; } _next = next; + _services = services; } public string AuthenticationType { get; set; } @@ -34,15 +37,18 @@ namespace Microsoft.AspNet.Security.Infrastructure public async Task Invoke(HttpContext context) { - AuthenticationHandler handler = CreateHandler(); - await handler.Initialize(Options, context); - if (!await handler.InvokeAsync()) + using (RequestServicesContainer.EnsureRequestServices(context, _services)) { - await _next(context); + AuthenticationHandler handler = CreateHandler(); + await handler.Initialize(Options, context); + if (!await handler.InvokeAsync()) + { + await _next(context); + } + await handler.TeardownAsync(); } - await handler.TeardownAsync(); } protected abstract AuthenticationHandler CreateHandler(); } -} +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Security/project.json b/src/Microsoft.AspNet.Security/project.json index 176c631f7b..160e03b519 100644 --- a/src/Microsoft.AspNet.Security/project.json +++ b/src/Microsoft.AspNet.Security/project.json @@ -1,6 +1,7 @@ { "version": "1.0.0-*", "dependencies": { + "Microsoft.AspNet.RequestContainer": "1.0.0-*", "Microsoft.AspNet.HttpFeature": { "version": "1.0.0-*", "type": "build" }, "Microsoft.AspNet.PipelineCore": "1.0.0-*", "Microsoft.AspNet.Security.DataProtection": "1.0.0-*", diff --git a/test/Microsoft.AspNet.Security.Test/Cookies/CookieMiddlewareTests.cs b/test/Microsoft.AspNet.Security.Test/Cookies/CookieMiddlewareTests.cs index a02dc14752..fc101faaa5 100644 --- a/test/Microsoft.AspNet.Security.Test/Cookies/CookieMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Security.Test/Cookies/CookieMiddlewareTests.cs @@ -369,7 +369,6 @@ namespace Microsoft.AspNet.Security.Cookies { return TestServer.Create(app => { - app.UseServices(services => { }); app.UseCookieAuthentication(configureOptions); app.Use(async (context, next) => {