React to hosting changes

This commit is contained in:
Hao Kung 2015-03-19 11:04:33 -07:00
parent 6086bb0a62
commit 776593ec71
24 changed files with 143 additions and 170 deletions

View File

@ -8,13 +8,13 @@ namespace CookieSample
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection();
}
public void Configure(IApplicationBuilder app)
{
app.UseServices(services =>
{
services.AddDataProtection();
});
app.UseCookieAuthentication(options =>
{
});

View File

@ -9,13 +9,13 @@ namespace CookieSessionSample
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection();
}
public void Configure(IApplicationBuilder app)
{
app.UseServices(services =>
{
services.AddDataProtection();
});
app.UseCookieAuthentication(options =>
{
options.SessionStore = new MemoryCacheSessionStore();

View File

@ -10,18 +10,17 @@ namespace OpenIdConnectSample
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection();
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
}
public void Configure(IApplicationBuilder app)
{
app.UseServices(services =>
{
services.AddDataProtection();
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
});
app.UseCookieAuthentication(options =>
{
options.AutomaticAuthentication = true;

View File

@ -18,12 +18,11 @@ namespace Microsoft.AspNet.Authentication.Cookies
public CookieAuthenticationMiddleware(
[NotNull] RequestDelegate next,
[NotNull] IServiceProvider services,
[NotNull] IDataProtectionProvider dataProtectionProvider,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IOptions<CookieAuthenticationOptions> options,
ConfigureOptions<CookieAuthenticationOptions> configureOptions)
: base(next, services, options, configureOptions)
: base(next, options, configureOptions)
{
if (Options.Notifications == null)
{

View File

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

View File

@ -1,7 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Net.Http;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Authentication.OAuth;
using Newtonsoft.Json.Linq;

View File

@ -3,7 +3,8 @@
"description": "ASP.NET 5 middleware that enables an application to support Facebook's OAuth 2.0 authentication workflow.",
"dependencies": {
"Microsoft.AspNet.Authentication.OAuth": "1.0.0-*",
"Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" }
"Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" },
"Newtonsoft.Json": "6.0.6"
},
"frameworks": {
"dnx451": { },

View File

@ -22,19 +22,17 @@ namespace Microsoft.AspNet.Authentication.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(
[NotNull] RequestDelegate next,
[NotNull] IServiceProvider services,
[NotNull] IDataProtectionProvider dataProtectionProvider,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
[NotNull] IOptions<GoogleAuthenticationOptions> options,
ConfigureOptions<GoogleAuthenticationOptions> configureOptions = null)
: base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
: base(next, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
{
if (Options.Notifications == null)
{

View File

@ -20,19 +20,17 @@ namespace Microsoft.AspNet.Authentication.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(
[NotNull] RequestDelegate next,
[NotNull] IServiceProvider services,
[NotNull] IDataProtectionProvider dataProtectionProvider,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
[NotNull] IOptions<MicrosoftAccountAuthenticationOptions> options,
ConfigureOptions<MicrosoftAccountAuthenticationOptions> configureOptions = null)
: base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
: base(next, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions)
{
if (Options.Notifications == null)
{

View File

@ -5,12 +5,10 @@ using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Extensions;
using Microsoft.AspNet.Http.Authentication;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Http.Extensions;
using Microsoft.AspNet.WebUtilities;
using Microsoft.Framework.Logging;
using Newtonsoft.Json.Linq;

View File

@ -26,19 +26,17 @@ namespace Microsoft.AspNet.Authentication.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(
[NotNull] RequestDelegate next,
[NotNull] IServiceProvider services,
[NotNull] IDataProtectionProvider dataProtectionProvider,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
[NotNull] IOptions<TOptions> options,
ConfigureOptions<TOptions> configureOptions = null)
: base(next, services, options, configureOptions)
: base(next, options, configureOptions)
{
// todo: review error handling
if (string.IsNullOrWhiteSpace(Options.AuthenticationScheme))

View File

@ -4,7 +4,8 @@
"dependencies": {
"Microsoft.AspNet.DataProtection": "1.0.0-*",
"Microsoft.AspNet.Authentication": "1.0.0-*",
"Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" }
"Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" },
"Newtonsoft.Json": "6.0.6"
},
"frameworks": {
"dnx451": {

View File

@ -31,11 +31,10 @@ namespace Microsoft.AspNet.Authentication.OAuthBearer
/// </summary>
public OAuthBearerAuthenticationMiddleware(
[NotNull] RequestDelegate next,
[NotNull] IServiceProvider services,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IOptions<OAuthBearerAuthenticationOptions> options,
ConfigureOptions<OAuthBearerAuthenticationOptions> configureOptions)
: base(next, services, options, configureOptions)
: base(next, options, configureOptions)
{
_logger = loggerFactory.CreateLogger<OAuthBearerAuthenticationMiddleware>();
if (Options.Notifications == null)

View File

@ -36,13 +36,12 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Managed by caller")]
public OpenIdConnectAuthenticationMiddleware(
[NotNull] RequestDelegate next,
[NotNull] IServiceProvider services,
[NotNull] IDataProtectionProvider dataProtectionProvider,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
[NotNull] IOptions<OpenIdConnectAuthenticationOptions> options,
ConfigureOptions<OpenIdConnectAuthenticationOptions> configureOptions)
: base(next, services, options, configureOptions)
: base(next, options, configureOptions)
{
_logger = loggerFactory.CreateLogger<OpenIdConnectAuthenticationMiddleware>();

View File

@ -35,13 +35,12 @@ namespace Microsoft.AspNet.Authentication.Twitter
/// <param name="options">Configuration options for the middleware</param>
public TwitterAuthenticationMiddleware(
[NotNull] RequestDelegate next,
[NotNull] IServiceProvider services,
[NotNull] IDataProtectionProvider dataProtectionProvider,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
[NotNull] IOptions<TwitterAuthenticationOptions> options,
ConfigureOptions<TwitterAuthenticationOptions> configureOptions = null)
: base(next, services, options, configureOptions)
: base(next, options, configureOptions)
{
if (string.IsNullOrWhiteSpace(Options.ConsumerSecret))
{

View File

@ -5,7 +5,6 @@ using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.RequestContainer;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
@ -14,11 +13,9 @@ namespace Microsoft.AspNet.Authentication
public abstract class AuthenticationMiddleware<TOptions> where TOptions : AuthenticationOptions, new()
{
private readonly RequestDelegate _next;
private readonly IServiceProvider _services;
protected AuthenticationMiddleware(
[NotNull] RequestDelegate next,
[NotNull] IServiceProvider services,
[NotNull] IOptions<TOptions> options,
ConfigureOptions<TOptions> configureOptions)
{
@ -32,7 +29,6 @@ namespace Microsoft.AspNet.Authentication
Options = options.Options;
}
_next = next;
_services = services;
}
public string AuthenticationScheme { get; set; }
@ -41,32 +37,29 @@ namespace Microsoft.AspNet.Authentication
public async Task Invoke(HttpContext context)
{
using (RequestServicesContainer.EnsureRequestServices(context, _services))
AuthenticationHandler<TOptions> handler = CreateHandler();
await handler.Initialize(Options, context);
try
{
if (!await handler.InvokeAsync())
{
await _next(context);
}
}
catch (Exception)
{
AuthenticationHandler<TOptions> handler = CreateHandler();
await handler.Initialize(Options, context);
try
{
if (!await handler.InvokeAsync())
{
await _next(context);
}
handler.Faulted = true;
await handler.TeardownAsync();
}
catch (Exception)
{
try
{
handler.Faulted = true;
await handler.TeardownAsync();
}
catch (Exception)
{
// Don't mask the original exception
}
throw;
// Don't mask the original exception
}
await handler.TeardownAsync();
throw;
}
await handler.TeardownAsync();
}
protected abstract AuthenticationHandler<TOptions> CreateHandler();

View File

@ -3,9 +3,9 @@
"description": "ASP.NET 5 common types used by the various authentication middleware.",
"dependencies": {
"Microsoft.AspNet.DataProtection": "1.0.0-*",
"Microsoft.AspNet.RequestContainer": "1.0.0-*",
"Microsoft.AspNet.Http.Interfaces": "1.0.0-*",
"Microsoft.AspNet.Http.Core": "1.0.0-*",
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
"Microsoft.Framework.Logging.Interfaces": "1.0.0-*",
"Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" }
},

View File

@ -524,17 +524,6 @@ namespace Microsoft.AspNet.Authentication.Cookies
{
var server = TestServer.Create(app =>
{
if (claimsTransform != null)
{
app.UseServices(services => {
services.AddDataProtection();
services.ConfigureClaimsTransformation(claimsTransform);
});
}
else
{
app.UseServices(services => services.AddDataProtection());
}
app.UseCookieAuthentication(configureOptions);
if (claimsTransform != null)
{
@ -581,6 +570,14 @@ namespace Microsoft.AspNet.Authentication.Cookies
await next();
}
});
},
services =>
{
services.AddDataProtection();
if (claimsTransform != null)
{
services.ConfigureClaimsTransformation(claimsTransform);
}
});
server.BaseAddress = baseAddress;
return server;

View File

@ -24,34 +24,34 @@ namespace Microsoft.AspNet.Authentication.Facebook
var server = CreateServer(
app =>
{
app.UseServices(services =>
{
services.AddDataProtection();
services.ConfigureFacebookAuthentication(options =>
{
options.AppId = "Test App Id";
options.AppSecret = "Test App Secret";
options.Notifications = new FacebookAuthenticationNotifications
{
OnApplyRedirect = context =>
{
context.Response.Redirect(context.RedirectUri + "&custom=test");
}
};
});
services.ConfigureCookieAuthentication(options =>
{
options.AuthenticationScheme = "External";
options.AutomaticAuthentication = true;
});
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = "External";
});
});
app.UseFacebookAuthentication();
app.UseCookieAuthentication();
},
services =>
{
services.AddDataProtection();
services.ConfigureFacebookAuthentication(options =>
{
options.AppId = "Test App Id";
options.AppSecret = "Test App Secret";
options.Notifications = new FacebookAuthenticationNotifications
{
OnApplyRedirect = context =>
{
context.Response.Redirect(context.RedirectUri + "&custom=test");
}
};
});
services.ConfigureCookieAuthentication(options =>
{
options.AuthenticationScheme = "External";
options.AutomaticAuthentication = true;
});
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = "External";
});
},
context =>
{
context.Response.Challenge("Facebook");
@ -69,26 +69,26 @@ namespace Microsoft.AspNet.Authentication.Facebook
var server = CreateServer(
app =>
{
app.UseServices(services =>
{
services.AddDataProtection();
services.ConfigureFacebookAuthentication(options =>
{
options.AppId = "Test App Id";
options.AppSecret = "Test App Secret";
});
services.ConfigureCookieAuthentication(options =>
{
options.AuthenticationScheme = "External";
});
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = "External";
});
});
app.UseFacebookAuthentication();
app.UseCookieAuthentication();
},
services =>
{
services.AddDataProtection();
services.ConfigureFacebookAuthentication(options =>
{
options.AppId = "Test App Id";
options.AppSecret = "Test App Secret";
});
services.ConfigureCookieAuthentication(options =>
{
options.AuthenticationScheme = "External";
});
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = "External";
});
},
context =>
{
context.Response.Challenge("Facebook");
@ -105,7 +105,7 @@ namespace Microsoft.AspNet.Authentication.Facebook
location.ShouldContain("state=");
}
private static TestServer CreateServer(Action<IApplicationBuilder> configure, Func<HttpContext, bool> handler)
private static TestServer CreateServer(Action<IApplicationBuilder> configure, Action<IServiceCollection> configureServices, Func<HttpContext, bool> handler)
{
return TestServer.Create(app =>
{
@ -120,7 +120,8 @@ namespace Microsoft.AspNet.Authentication.Facebook
await next();
}
});
});
},
configureServices);
}
private static async Task<Transaction> SendAsync(TestServer server, string uri, string cookieHeader = null)

View File

@ -482,21 +482,6 @@ namespace Microsoft.AspNet.Authentication.Google
{
return TestServer.Create(app =>
{
app.UseServices(services =>
{
services.AddDataProtection();
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = CookieAuthenticationScheme;
});
services.ConfigureClaimsTransformation(p =>
{
var id = new ClaimsIdentity("xform");
id.AddClaim(new Claim("xform", "yup"));
p.AddIdentity(id);
return p;
});
});
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = CookieAuthenticationScheme;
@ -542,6 +527,21 @@ namespace Microsoft.AspNet.Authentication.Google
await next();
}
});
},
services =>
{
services.AddDataProtection();
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = CookieAuthenticationScheme;
});
services.ConfigureClaimsTransformation(p =>
{
var id = new ClaimsIdentity("xform");
id.AddClaim(new Claim("xform", "yup"));
p.AddIdentity(id);
return p;
});
});
}

View File

@ -161,14 +161,6 @@ namespace Microsoft.AspNet.Authentication.Tests.MicrosoftAccount
{
return TestServer.Create(app =>
{
app.UseServices(services =>
{
services.AddDataProtection();
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = "External";
});
});
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = "External";
@ -182,6 +174,14 @@ namespace Microsoft.AspNet.Authentication.Tests.MicrosoftAccount
await next();
}
});
},
services =>
{
services.AddDataProtection();
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = "External";
});
});
}

View File

@ -233,11 +233,6 @@ namespace Microsoft.AspNet.Authentication.OAuthBearer
{
return TestServer.Create(app =>
{
app.UseServices(services =>
{
services.AddDataProtection();
});
if (configureOptions != null)
{
app.UseOAuthBearerAuthentication(configureOptions);
@ -261,7 +256,8 @@ namespace Microsoft.AspNet.Authentication.OAuthBearer
}
});
});
},
services => services.AddDataProtection());
}
private static async Task<Transaction> SendAsync(TestServer server, string uri, string authorizationHeader = null)

View File

@ -193,15 +193,6 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
{
return TestServer.Create(app =>
{
app.UseServices(services =>
{
services.AddDataProtection();
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
});
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = "OpenIdConnect";
@ -228,7 +219,7 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
else if (req.Path == new PathString("/signout_with_specific_redirect_uri"))
{
res.SignOut(
OpenIdConnectAuthenticationDefaults.AuthenticationScheme,
OpenIdConnectAuthenticationDefaults.AuthenticationScheme,
new AuthenticationProperties() { RedirectUri = "http://www.example.com/specific_redirect_uri" });
}
else if (handler != null)
@ -240,6 +231,14 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
await next();
}
});
},
services =>
{
services.AddDataProtection();
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
});
}

View File

@ -105,14 +105,6 @@ namespace Microsoft.AspNet.Authentication.Twitter
{
return TestServer.Create(app =>
{
app.UseServices(services =>
{
services.AddDataProtection();
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = "External";
});
});
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = "External";
@ -128,6 +120,14 @@ namespace Microsoft.AspNet.Authentication.Twitter
await next();
}
});
},
services =>
{
services.AddDataProtection();
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = "External";
});
});
}