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)
{
app.UseServices(services => { });
app.UseCookieAuthentication(options =>
{
});

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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
/// </summary>
public OAuthBearerAuthenticationMiddleware(
RequestDelegate next,
IServiceProvider services,
IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory,
IOptions<OAuthBearerAuthenticationOptions> options,
ConfigureOptions<OAuthBearerAuthenticationOptions> configureOptions)
: base(next, options, configureOptions)
: base(next, services, options, configureOptions)
{
_logger = loggerFactory.Create<OAuthBearerAuthenticationMiddleware>();

View File

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

View File

@ -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<TOptions> where TOptions : AuthenticationOptions, new()
{
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)
{
@ -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<TOptions> handler = CreateHandler();
await handler.Initialize(Options, context);
if (!await handler.InvokeAsync())
using (RequestServicesContainer.EnsureRequestServices(context, _services))
{
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();
}
}
}

View File

@ -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-*",

View File

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