Switch to automatically ensure request services

This commit is contained in:
Hao Kung 2014-10-15 23:42:45 -07:00
parent 45836c8041
commit 78f472fd20
12 changed files with 33 additions and 17 deletions

View File

@ -9,7 +9,6 @@ namespace CookieSample
{ {
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)
{ {
app.UseServices(services => { });
app.UseCookieAuthentication(options => app.UseCookieAuthentication(options =>
{ {
}); });

View File

@ -10,7 +10,6 @@ namespace CookieSessionSample
{ {
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)
{ {
app.UseRequestServices();
app.UseCookieAuthentication(options => app.UseCookieAuthentication(options =>
{ {
options.SessionStore = new MemoryCacheSessionStore(); options.SessionStore = new MemoryCacheSessionStore();

View File

@ -18,11 +18,12 @@ namespace Microsoft.AspNet.Security.Cookies
private readonly ILogger _logger; private readonly ILogger _logger;
public CookieAuthenticationMiddleware(RequestDelegate next, public CookieAuthenticationMiddleware(RequestDelegate next,
IServiceProvider services,
IDataProtectionProvider dataProtectionProvider, IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
IOptions<CookieAuthenticationOptions> options, IOptions<CookieAuthenticationOptions> options,
ConfigureOptions<CookieAuthenticationOptions> configureOptions) ConfigureOptions<CookieAuthenticationOptions> configureOptions)
: base(next, options, configureOptions) : base(next, services, options, configureOptions)
{ {
if (Options.Notifications == null) if (Options.Notifications == null)
{ {

View File

@ -26,12 +26,13 @@ namespace Microsoft.AspNet.Security.Facebook
/// <param name="options">Configuration options for the middleware.</param> /// <param name="options">Configuration options for the middleware.</param>
public FacebookAuthenticationMiddleware( public FacebookAuthenticationMiddleware(
RequestDelegate next, RequestDelegate next,
IServiceProvider services,
IDataProtectionProvider dataProtectionProvider, IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
IOptions<ExternalAuthenticationOptions> externalOptions, IOptions<ExternalAuthenticationOptions> externalOptions,
IOptions<FacebookAuthenticationOptions> options, IOptions<FacebookAuthenticationOptions> options,
ConfigureOptions<FacebookAuthenticationOptions> configureOptions = null) ConfigureOptions<FacebookAuthenticationOptions> configureOptions = null)
: base(next, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions) : base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
{ {
if (string.IsNullOrWhiteSpace(Options.AppId)) if (string.IsNullOrWhiteSpace(Options.AppId))
{ {

View File

@ -25,17 +25,19 @@ namespace Microsoft.AspNet.Security.Google
/// Initializes a new <see cref="GoogleAuthenticationMiddleware"/>. /// Initializes a new <see cref="GoogleAuthenticationMiddleware"/>.
/// </summary> /// </summary>
/// <param name="next">The next middleware in the HTTP pipeline to invoke.</param> /// <param name="next">The next middleware in the HTTP pipeline to invoke.</param>
/// <param name="services"></param>
/// <param name="dataProtectionProvider"></param> /// <param name="dataProtectionProvider"></param>
/// <param name="loggerFactory"></param> /// <param name="loggerFactory"></param>
/// <param name="options">Configuration options for the middleware.</param> /// <param name="options">Configuration options for the middleware.</param>
public GoogleAuthenticationMiddleware( public GoogleAuthenticationMiddleware(
RequestDelegate next, RequestDelegate next,
IServiceProvider services,
IDataProtectionProvider dataProtectionProvider, IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
IOptions<ExternalAuthenticationOptions> externalOptions, IOptions<ExternalAuthenticationOptions> externalOptions,
IOptions<GoogleAuthenticationOptions> options, IOptions<GoogleAuthenticationOptions> options,
ConfigureOptions<GoogleAuthenticationOptions> configureOptions = null) ConfigureOptions<GoogleAuthenticationOptions> configureOptions = null)
: base(next, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions) : base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
{ {
if (Options.Notifications == null) if (Options.Notifications == null)
{ {

View File

@ -21,17 +21,19 @@ namespace Microsoft.AspNet.Security.MicrosoftAccount
/// Initializes a new <see cref="MicrosoftAccountAuthenticationMiddleware"/>. /// Initializes a new <see cref="MicrosoftAccountAuthenticationMiddleware"/>.
/// </summary> /// </summary>
/// <param name="next">The next middleware in the HTTP pipeline to invoke.</param> /// <param name="next">The next middleware in the HTTP pipeline to invoke.</param>
/// <param name="services"></param>
/// <param name="dataProtectionProvider"></param> /// <param name="dataProtectionProvider"></param>
/// <param name="loggerFactory"></param> /// <param name="loggerFactory"></param>
/// <param name="options">Configuration options for the middleware.</param> /// <param name="options">Configuration options for the middleware.</param>
public MicrosoftAccountAuthenticationMiddleware( public MicrosoftAccountAuthenticationMiddleware(
RequestDelegate next, RequestDelegate next,
IServiceProvider services,
IDataProtectionProvider dataProtectionProvider, IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
IOptions<ExternalAuthenticationOptions> externalOptions, IOptions<ExternalAuthenticationOptions> externalOptions,
IOptions<MicrosoftAccountAuthenticationOptions> options, IOptions<MicrosoftAccountAuthenticationOptions> options,
ConfigureOptions<MicrosoftAccountAuthenticationOptions> configureOptions = null) ConfigureOptions<MicrosoftAccountAuthenticationOptions> configureOptions = null)
: base(next, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions) : base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
{ {
if (Options.Notifications == null) if (Options.Notifications == null)
{ {

View File

@ -26,17 +26,19 @@ namespace Microsoft.AspNet.Security.OAuth
/// Initializes a new <see cref="OAuthAuthenticationMiddleware"/>. /// Initializes a new <see cref="OAuthAuthenticationMiddleware"/>.
/// </summary> /// </summary>
/// <param name="next">The next middleware in the HTTP pipeline to invoke.</param> /// <param name="next">The next middleware in the HTTP pipeline to invoke.</param>
/// <param name="services"></param>
/// <param name="dataProtectionProvider"></param> /// <param name="dataProtectionProvider"></param>
/// <param name="loggerFactory"></param> /// <param name="loggerFactory"></param>
/// <param name="options">Configuration options for the middleware.</param> /// <param name="options">Configuration options for the middleware.</param>
public OAuthAuthenticationMiddleware( public OAuthAuthenticationMiddleware(
RequestDelegate next, RequestDelegate next,
IServiceProvider services,
IDataProtectionProvider dataProtectionProvider, IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
IOptions<ExternalAuthenticationOptions> externalOptions, IOptions<ExternalAuthenticationOptions> externalOptions,
IOptions<TOptions> options, IOptions<TOptions> options,
ConfigureOptions<TOptions> configureOptions = null) ConfigureOptions<TOptions> configureOptions = null)
: base(next, options, configureOptions) : base(next, services, options, configureOptions)
{ {
// todo: review error handling // todo: review error handling
if (string.IsNullOrWhiteSpace(Options.AuthenticationType)) if (string.IsNullOrWhiteSpace(Options.AuthenticationType))

View File

@ -7,6 +7,7 @@ using Microsoft.AspNet.Security.DataProtection;
using Microsoft.AspNet.Security.Infrastructure; using Microsoft.AspNet.Security.Infrastructure;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel; using Microsoft.Framework.OptionsModel;
using System;
namespace Microsoft.AspNet.Security.OAuth namespace Microsoft.AspNet.Security.OAuth
{ {
@ -28,11 +29,12 @@ namespace Microsoft.AspNet.Security.OAuth
/// </summary> /// </summary>
public OAuthBearerAuthenticationMiddleware( public OAuthBearerAuthenticationMiddleware(
RequestDelegate next, RequestDelegate next,
IServiceProvider services,
IDataProtectionProvider dataProtectionProvider, IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
IOptions<OAuthBearerAuthenticationOptions> options, IOptions<OAuthBearerAuthenticationOptions> options,
ConfigureOptions<OAuthBearerAuthenticationOptions> configureOptions) ConfigureOptions<OAuthBearerAuthenticationOptions> configureOptions)
: base(next, options, configureOptions) : base(next, services, options, configureOptions)
{ {
_logger = loggerFactory.Create<OAuthBearerAuthenticationMiddleware>(); _logger = loggerFactory.Create<OAuthBearerAuthenticationMiddleware>();

View File

@ -29,17 +29,19 @@ namespace Microsoft.AspNet.Security.Twitter
/// Initializes a <see cref="TwitterAuthenticationMiddleware"/> /// Initializes a <see cref="TwitterAuthenticationMiddleware"/>
/// </summary> /// </summary>
/// <param name="next">The next middleware in the HTTP pipeline to invoke</param> /// <param name="next">The next middleware in the HTTP pipeline to invoke</param>
/// <param name="services"></param>
/// <param name="dataProtectionProvider"></param> /// <param name="dataProtectionProvider"></param>
/// <param name="loggerFactory"></param> /// <param name="loggerFactory"></param>
/// <param name="options">Configuration options for the middleware</param> /// <param name="options">Configuration options for the middleware</param>
public TwitterAuthenticationMiddleware( public TwitterAuthenticationMiddleware(
RequestDelegate next, RequestDelegate next,
IServiceProvider services,
IDataProtectionProvider dataProtectionProvider, IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
IOptions<ExternalAuthenticationOptions> externalOptions, IOptions<ExternalAuthenticationOptions> externalOptions,
IOptions<TwitterAuthenticationOptions> options, IOptions<TwitterAuthenticationOptions> options,
ConfigureOptions<TwitterAuthenticationOptions> configureOptions = null) ConfigureOptions<TwitterAuthenticationOptions> configureOptions = null)
: base(next, options, configureOptions) : base(next, services, options, configureOptions)
{ {
if (string.IsNullOrWhiteSpace(Options.ConsumerSecret)) if (string.IsNullOrWhiteSpace(Options.ConsumerSecret))
{ {

View File

@ -6,6 +6,7 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.RequestContainer;
using Microsoft.Framework.OptionsModel; using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Security.Infrastructure namespace Microsoft.AspNet.Security.Infrastructure
@ -13,8 +14,9 @@ namespace Microsoft.AspNet.Security.Infrastructure
public abstract class AuthenticationMiddleware<TOptions> where TOptions : AuthenticationOptions, new() public abstract class AuthenticationMiddleware<TOptions> where TOptions : AuthenticationOptions, new()
{ {
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private readonly IServiceProvider _services;
protected AuthenticationMiddleware([NotNull] RequestDelegate next, [NotNull] IOptions<TOptions> options, ConfigureOptions<TOptions> configureOptions) protected AuthenticationMiddleware([NotNull] RequestDelegate next, [NotNull] IServiceProvider services, [NotNull] IOptions<TOptions> options, ConfigureOptions<TOptions> configureOptions)
{ {
if (configureOptions != null) if (configureOptions != null)
{ {
@ -26,6 +28,7 @@ namespace Microsoft.AspNet.Security.Infrastructure
Options = options.Options; Options = options.Options;
} }
_next = next; _next = next;
_services = services;
} }
public string AuthenticationType { get; set; } public string AuthenticationType { get; set; }
@ -34,15 +37,18 @@ namespace Microsoft.AspNet.Security.Infrastructure
public async Task Invoke(HttpContext context) public async Task Invoke(HttpContext context)
{ {
AuthenticationHandler<TOptions> handler = CreateHandler(); using (RequestServicesContainer.EnsureRequestServices(context, _services))
await handler.Initialize(Options, context);
if (!await handler.InvokeAsync())
{ {
await _next(context); AuthenticationHandler<TOptions> handler = CreateHandler();
await handler.Initialize(Options, context);
if (!await handler.InvokeAsync())
{
await _next(context);
}
await handler.TeardownAsync();
} }
await handler.TeardownAsync();
} }
protected abstract AuthenticationHandler<TOptions> CreateHandler(); protected abstract AuthenticationHandler<TOptions> CreateHandler();
} }
} }

View File

@ -1,6 +1,7 @@
{ {
"version": "1.0.0-*", "version": "1.0.0-*",
"dependencies": { "dependencies": {
"Microsoft.AspNet.RequestContainer": "1.0.0-*",
"Microsoft.AspNet.HttpFeature": { "version": "1.0.0-*", "type": "build" }, "Microsoft.AspNet.HttpFeature": { "version": "1.0.0-*", "type": "build" },
"Microsoft.AspNet.PipelineCore": "1.0.0-*", "Microsoft.AspNet.PipelineCore": "1.0.0-*",
"Microsoft.AspNet.Security.DataProtection": "1.0.0-*", "Microsoft.AspNet.Security.DataProtection": "1.0.0-*",

View File

@ -369,7 +369,6 @@ namespace Microsoft.AspNet.Security.Cookies
{ {
return TestServer.Create(app => return TestServer.Create(app =>
{ {
app.UseServices(services => { });
app.UseCookieAuthentication(configureOptions); app.UseCookieAuthentication(configureOptions);
app.Use(async (context, next) => app.Use(async (context, next) =>
{ {