Remove usage IOptions from middleware ctors

This commit is contained in:
Hao Kung 2015-09-18 12:24:33 -07:00
parent 06c89aa340
commit 081577e4f4
40 changed files with 262 additions and 421 deletions

View File

@ -1,10 +1,9 @@
using System.Linq;
using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.AspNet.Authentication.OpenIdConnect;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Authentication;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.AspNet.Authentication.OpenIdConnect;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
@ -14,11 +13,7 @@ namespace OpenIdConnectSample
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication();
services.Configure<SharedAuthenticationOptions>(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
services.AddAuthentication(sharedOptions => sharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
@ -51,8 +46,6 @@ namespace OpenIdConnectSample
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Hello Authenticated User");
});
}
}
}

View File

@ -2,7 +2,6 @@ using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.AspNet.Authentication.Google;
using Microsoft.AspNet.Authentication.MicrosoftAccount;
@ -21,18 +20,7 @@ namespace CookieSample
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication();
services.Configure<SharedAuthenticationOptions>(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
services.AddClaimsTransformation(p =>
{
var id = new ClaimsIdentity("xform");
id.AddClaim(new Claim("ClaimsTransformation", "TransformAddedClaim"));
p.AddIdentity(id);
return p;
});
services.AddAuthentication(options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)

View File

@ -4,7 +4,6 @@
using System;
using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder
{
@ -17,13 +16,10 @@ namespace Microsoft.AspNet.Builder
/// Adds a cookie-based authentication middleware to your web application pipeline.
/// </summary>
/// <param name="app">The IApplicationBuilder passed to your configuration method</param>
/// <param name="configureOptions">Used to configure the options for the middleware</param>
/// <param name="optionsName">The name of the options class that controls the middleware behavior, null will use the default options</param>
/// <returns>The original app parameter</returns>
public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app, Action<CookieAuthenticationOptions> configureOptions = null)
public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app)
{
return app.UseMiddleware<CookieAuthenticationMiddleware>(
new ConfigureOptions<CookieAuthenticationOptions>(configureOptions ?? (o => { })));
return app.UseCookieAuthentication(new CookieAuthenticationOptions());
}
/// <summary>
@ -31,12 +27,26 @@ namespace Microsoft.AspNet.Builder
/// </summary>
/// <param name="app">The IApplicationBuilder passed to your configuration method</param>
/// <param name="configureOptions">Used to configure the options for the middleware</param>
/// <param name="optionsName">The name of the options class that controls the middleware behavior, null will use the default options</param>
/// <returns>The original app parameter</returns>
public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app, IOptions<CookieAuthenticationOptions> options)
public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app, Action<CookieAuthenticationOptions> configureOptions)
{
return app.UseMiddleware<CookieAuthenticationMiddleware>(options,
new ConfigureOptions<CookieAuthenticationOptions>(o => { }));
var options = new CookieAuthenticationOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseCookieAuthentication(options);
}
/// <summary>
/// Adds a cookie-based authentication middleware to your web application pipeline.
/// </summary>
/// <param name="app">The IApplicationBuilder passed to your configuration method</param>
/// <param name="options">Used to configure the middleware</param>
/// <returns>The original app parameter</returns>
public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app, [NotNull] CookieAuthenticationOptions options)
{
return app.UseMiddleware<CookieAuthenticationMiddleware>(options);
}
}
}

View File

@ -6,7 +6,6 @@ using Microsoft.AspNet.Builder;
using Microsoft.AspNet.DataProtection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.WebEncoders;
namespace Microsoft.AspNet.Authentication.Cookies
@ -18,9 +17,8 @@ namespace Microsoft.AspNet.Authentication.Cookies
[NotNull] IDataProtectionProvider dataProtectionProvider,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder urlEncoder,
[NotNull] IOptions<CookieAuthenticationOptions> options,
ConfigureOptions<CookieAuthenticationOptions> configureOptions)
: base(next, options, loggerFactory, urlEncoder, configureOptions)
[NotNull] CookieAuthenticationOptions options)
: base(next, options, loggerFactory, urlEncoder)
{
if (Options.Events == null)
{

View File

@ -4,7 +4,6 @@
using System;
using Microsoft.AspNet.Authentication.Facebook;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder
{
@ -18,10 +17,25 @@ namespace Microsoft.AspNet.Builder
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseFacebookAuthentication([NotNull] this IApplicationBuilder app, Action<FacebookOptions> configureOptions = null)
public static IApplicationBuilder UseFacebookAuthentication([NotNull] this IApplicationBuilder app, [NotNull] FacebookOptions options)
{
return app.UseMiddleware<FacebookMiddleware>(
new ConfigureOptions<FacebookOptions>(configureOptions ?? (o => { })));
return app.UseMiddleware<FacebookMiddleware>(options);
}
/// <summary>
/// Authenticate users using Facebook.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param>
/// <param name="configureOptions">Configures the options.</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseFacebookAuthentication([NotNull] this IApplicationBuilder app, Action<FacebookOptions> configureOptions)
{
var options = new FacebookOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseFacebookAuthentication(options);
}
}
}

View File

@ -34,9 +34,8 @@ namespace Microsoft.AspNet.Authentication.Facebook
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<FacebookOptions> options,
ConfigureOptions<FacebookOptions> configureOptions = null)
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options, configureOptions)
[NotNull] FacebookOptions options)
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options)
{
if (string.IsNullOrEmpty(Options.AppId))
{

View File

@ -1,26 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Authentication.Facebook;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Extension methods for using <see cref="FacebookMiddleware"/>.
/// </summary>
public static class FacebookServiceCollectionExtensions
{
public static IServiceCollection AddFacebookAuthentication([NotNull] this IServiceCollection services, [NotNull] Action<FacebookOptions> configure)
{
return services.Configure(configure);
}
public static IServiceCollection AddFacebookAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config)
{
return services.Configure<FacebookOptions>(config);
}
}
}

View File

@ -4,7 +4,6 @@
using System;
using Microsoft.AspNet.Authentication.Google;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder
{
@ -13,6 +12,17 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class GoogleAppBuilderExtensions
{
/// <summary>
/// Authenticate users using Google OAuth 2.0.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param>
/// <param name="options">The Middleware options.</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseGoogleAuthentication([NotNull] this IApplicationBuilder app, [NotNull] GoogleOptions options)
{
return app.UseMiddleware<GoogleMiddleware>(options);
}
/// <summary>
/// Authenticate users using Google OAuth 2.0.
/// </summary>
@ -20,10 +30,14 @@ namespace Microsoft.AspNet.Builder
/// <param name="configureOptions">Used to configure Middleware options.</param>
/// <param name="optionsName">Name of the options instance to be used</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseGoogleAuthentication([NotNull] this IApplicationBuilder app, Action<GoogleOptions> configureOptions = null, string optionsName = "")
public static IApplicationBuilder UseGoogleAuthentication([NotNull] this IApplicationBuilder app, Action<GoogleOptions> configureOptions)
{
return app.UseMiddleware<GoogleMiddleware>(
new ConfigureOptions<GoogleOptions>(configureOptions ?? (o => { })));
var options = new GoogleOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseGoogleAuthentication(options);
}
}
}

View File

@ -34,9 +34,8 @@ namespace Microsoft.AspNet.Authentication.Google
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<GoogleOptions> options,
ConfigureOptions<GoogleOptions> configureOptions = null)
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options, configureOptions)
[NotNull] GoogleOptions options)
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options)
{
if (Options.Scope.Count == 0)
{

View File

@ -1,26 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Authentication.Google;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Extension methods for using <see cref="GoogleMiddleware"/>.
/// </summary>
public static class GoogleServiceCollectionExtensions
{
public static IServiceCollection AddGoogleAuthentication([NotNull] this IServiceCollection services, [NotNull] Action<GoogleOptions> configure)
{
return services.Configure(configure);
}
public static IServiceCollection AddGoogleAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config)
{
return services.Configure<GoogleOptions>(config);
}
}
}

View File

@ -24,10 +24,30 @@ namespace Microsoft.AspNet.Builder
/// <param name="app">The application builder</param>
/// <param name="options">Options which control the processing of the bearer header.</param>
/// <returns>The application builder</returns>
public static IApplicationBuilder UseJwtBearerAuthentication([NotNull] this IApplicationBuilder app, Action<JwtBearerOptions> configureOptions = null, string optionsName = "")
public static IApplicationBuilder UseJwtBearerAuthentication([NotNull] this IApplicationBuilder app, [NotNull] JwtBearerOptions options)
{
return app.UseMiddleware<JwtBearerMiddleware>(
new ConfigureOptions<JwtBearerOptions>(configureOptions ?? (o => { })));
return app.UseMiddleware<JwtBearerMiddleware>(options);
}
/// <summary>
/// Adds Bearer token processing to an HTTP application pipeline. This middleware understands appropriately
/// formatted and secured tokens which appear in the request header. If the Options.AuthenticationMode is Active, the
/// claims within the bearer token are added to the current request's IPrincipal User. If the Options.AuthenticationMode
/// is Passive, then the current request is not modified, but IAuthenticationManager AuthenticateAsync may be used at
/// any time to obtain the claims from the request's bearer token.
/// See also http://tools.ietf.org/html/rfc6749
/// </summary>
/// <param name="app">The application builder</param>
/// <param name="configureOptions">Used to configure Middleware options.</param>
/// <returns>The application builder</returns>
public static IApplicationBuilder UseJwtBearerAuthentication([NotNull] this IApplicationBuilder app, Action<JwtBearerOptions> configureOptions)
{
var options = new JwtBearerOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseJwtBearerAuthentication(options);
}
}
}

View File

@ -29,9 +29,8 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
[NotNull] RequestDelegate next,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder,
[NotNull] IOptions<JwtBearerOptions> options,
ConfigureOptions<JwtBearerOptions> configureOptions)
: base(next, options, loggerFactory, encoder, configureOptions)
[NotNull] JwtBearerOptions options)
: base(next, options, loggerFactory, encoder)
{
if (Options.Events == null)
{

View File

@ -1,26 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Authentication.JwtBearer;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Extension methods to add Jwt Bearer authentication capabilities to an HTTP application pipeline
/// </summary>
public static class JwtBearerServiceCollectionExtensions
{
public static IServiceCollection AddJwtBearerAuthentication([NotNull] this IServiceCollection services, [NotNull] Action<JwtBearerOptions> configure)
{
return services.Configure(configure);
}
public static IServiceCollection AddJwtBearerAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config)
{
return services.Configure<JwtBearerOptions>(config);
}
}
}

View File

@ -4,7 +4,6 @@
using System;
using Microsoft.AspNet.Authentication.MicrosoftAccount;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder
{
@ -13,10 +12,19 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class MicrosoftAccountAuthenticationExtensions
{
public static IApplicationBuilder UseMicrosoftAccountAuthentication([NotNull] this IApplicationBuilder app, Action<MicrosoftAccountOptions> configureOptions = null)
public static IApplicationBuilder UseMicrosoftAccountAuthentication([NotNull] this IApplicationBuilder app, [NotNull] MicrosoftAccountOptions options)
{
return app.UseMiddleware<MicrosoftAccountMiddleware>(
new ConfigureOptions<MicrosoftAccountOptions>(configureOptions ?? (o => { })));
return app.UseMiddleware<MicrosoftAccountMiddleware>(options);
}
public static IApplicationBuilder UseMicrosoftAccountAuthentication([NotNull] this IApplicationBuilder app, Action<MicrosoftAccountOptions> configureOptions)
{
var options = new MicrosoftAccountOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseMicrosoftAccountAuthentication(options);
}
}
}

View File

@ -32,9 +32,8 @@ namespace Microsoft.AspNet.Authentication.MicrosoftAccount
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<MicrosoftAccountOptions> options,
ConfigureOptions<MicrosoftAccountOptions> configureOptions = null)
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options, configureOptions)
[NotNull] MicrosoftAccountOptions options)
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options)
{
if (Options.Scope.Count == 0)
{

View File

@ -1,26 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Authentication.MicrosoftAccount;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Extension methods for using <see cref="MicrosoftAccountMiddleware"/>
/// </summary>
public static class MicrosoftAccountServiceCollectionExtensions
{
public static IServiceCollection AddMicrosoftAccountAuthentication([NotNull] this IServiceCollection services, [NotNull] Action<MicrosoftAccountOptions> configure)
{
return services.Configure(configure);
}
public static IServiceCollection AddMicrosoftAccountAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config)
{
return services.Configure<MicrosoftAccountOptions>(config);
}
}
}

View File

@ -13,17 +13,31 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class OAuthExtensions
{
/// <summary>
/// Authenticate users using OAuth.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param>
/// <param name="configureOptions">Configures the middleware options.</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseOAuthAuthentication([NotNull] this IApplicationBuilder app, [NotNull] Action<OAuthOptions> configureOptions)
{
var options = new OAuthOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseOAuthAuthentication(options);
}
/// <summary>
/// Authenticate users using OAuth.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param>
/// <param name="options">The middleware configuration options.</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseOAuthAuthentication([NotNull] this IApplicationBuilder app, [NotNull] IOptions<OAuthOptions> options)
public static IApplicationBuilder UseOAuthAuthentication([NotNull] this IApplicationBuilder app, [NotNull] OAuthOptions options)
{
return app.UseMiddleware<OAuthMiddleware<OAuthOptions>>(
options,
new ConfigureOptions<OAuthOptions>(o => { }));
return app.UseMiddleware<OAuthMiddleware<OAuthOptions>>(options);
}
}
}

View File

@ -33,9 +33,8 @@ namespace Microsoft.AspNet.Authentication.OAuth
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<TOptions> options,
ConfigureOptions<TOptions> configureOptions = null)
: base(next, options, loggerFactory, encoder, configureOptions)
[NotNull] TOptions options)
: base(next, options, loggerFactory, encoder)
{
// todo: review error handling
if (string.IsNullOrEmpty(Options.AuthenticationScheme))

View File

@ -6,14 +6,13 @@ using System.Collections.Generic;
using System.Net.Http;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Authentication;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Authentication.OAuth
{
/// <summary>
/// Configuration options for <see cref="OAuthMiddleware"/>.
/// </summary>
public class OAuthOptions : AuthenticationOptions, IOptions<OAuthOptions>
public class OAuthOptions : AuthenticationOptions
{
/// <summary>
/// Gets or sets the provider-assigned client id.
@ -102,13 +101,5 @@ namespace Microsoft.AspNet.Authentication.OAuth
/// authentication cookie. Note that social providers set this property to <c>false</c> by default.
/// </summary>
public bool SaveTokensAsClaims { get; set; } = true;
OAuthOptions IOptions<OAuthOptions>.Value
{
get
{
return this;
}
}
}
}

View File

@ -3,7 +3,7 @@
using System;
using Microsoft.AspNet.Authentication.OpenIdConnect;
using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Builder
{
@ -18,10 +18,15 @@ namespace Microsoft.AspNet.Builder
/// <param name="app">The application builder</param>
/// <param name="options">Options which control the processing of the OpenIdConnect protocol and token validation.</param>
/// <returns>The application builder</returns>
public static IApplicationBuilder UseOpenIdConnectAuthentication(this IApplicationBuilder app, Action<OpenIdConnectOptions> configureOptions = null)
public static IApplicationBuilder UseOpenIdConnectAuthentication([NotNull] this IApplicationBuilder app, Action<OpenIdConnectOptions> configureOptions)
{
return app.UseMiddleware<OpenIdConnectMiddleware>(
new ConfigureOptions<OpenIdConnectOptions>(configureOptions ?? (o => { })));
var options = new OpenIdConnectOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseOpenIdConnectAuthentication(options);
}
/// <summary>
@ -30,7 +35,7 @@ namespace Microsoft.AspNet.Builder
/// <param name="app">The application builder</param>
/// <param name="options">Options which control the processing of the OpenIdConnect protocol and token validation.</param>
/// <returns>The application builder</returns>
public static IApplicationBuilder UseOpenIdConnectAuthentication(this IApplicationBuilder app, IOptions<OpenIdConnectOptions> options)
public static IApplicationBuilder UseOpenIdConnectAuthentication([NotNull] this IApplicationBuilder app, [NotNull] OpenIdConnectOptions options)
{
return app.UseMiddleware<OpenIdConnectMiddleware>(options);
}

View File

@ -31,10 +31,7 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
/// <param name="encoder"></param>
/// <param name="services"></param>
/// <param name="sharedOptions"></param>
/// <param name="options">a <see cref="IOptions{OpenIdConnectOptions}"/> instance that will supply <see cref="OpenIdConnectOptions"/>
/// if configureOptions is null.</param>
/// <param name="configureOptions">a <see cref="ConfigureOptions{OpenIdConnectOptions}"/> instance that will be passed to an instance of <see cref="OpenIdConnectOptions"/>
/// that is retrieved by calling <see cref="IOptions{OpenIdConnectOptions}.GetNamedOptions(string)"/> where string == <see cref="ConfigureOptions{OpenIdConnectOptions}.Name"/> provides runtime configuration.</param>
/// <param name="options"></param>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Managed by caller")]
public OpenIdConnectMiddleware(
[NotNull] RequestDelegate next,
@ -43,9 +40,8 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
[NotNull] IUrlEncoder encoder,
[NotNull] IServiceProvider services,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<OpenIdConnectOptions> options,
ConfigureOptions<OpenIdConnectOptions> configureOptions = null)
: base(next, options, loggerFactory, encoder, configureOptions)
[NotNull] OpenIdConnectOptions options)
: base(next, options, loggerFactory, encoder)
{
if (string.IsNullOrEmpty(Options.SignInScheme) && !string.IsNullOrEmpty(sharedOptions.Value.SignInScheme))
{

View File

@ -1,26 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Authentication.OpenIdConnect;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Extension methods to configure OpenIdConnect authentication options
/// </summary>
public static class OpenIdConnectServiceCollectionExtensions
{
public static IServiceCollection AddOpenIdConnectAuthentication([NotNull] this IServiceCollection services, [NotNull] Action<OpenIdConnectOptions> configure)
{
return services.Configure(configure);
}
public static IServiceCollection AddOpenIdConnectAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config)
{
return services.Configure<OpenIdConnectOptions>(config);
}
}
}

View File

@ -4,7 +4,6 @@
using System;
using Microsoft.AspNet.Authentication.Twitter;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder
{
@ -15,8 +14,18 @@ namespace Microsoft.AspNet.Builder
{
public static IApplicationBuilder UseTwitterAuthentication([NotNull] this IApplicationBuilder app, Action<TwitterOptions> configureOptions = null)
{
return app.UseMiddleware<TwitterMiddleware>(
new ConfigureOptions<TwitterOptions>(configureOptions ?? (o => { })));
var options = new TwitterOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseTwitterAuthentication(options);
}
public static IApplicationBuilder UseTwitterAuthentication([NotNull] this IApplicationBuilder app, [NotNull] TwitterOptions options)
{
return app.UseMiddleware<TwitterMiddleware>(options);
}
}
}

View File

@ -38,9 +38,8 @@ namespace Microsoft.AspNet.Authentication.Twitter
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<TwitterOptions> options,
ConfigureOptions<TwitterOptions> configureOptions = null)
: base(next, options, loggerFactory, encoder, configureOptions)
[NotNull] TwitterOptions options)
: base(next, options, loggerFactory, encoder)
{
if (string.IsNullOrEmpty(Options.ConsumerSecret))
{

View File

@ -1,26 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Authentication.Twitter;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Extension methods for using <see cref="TwitterMiddleware"/>
/// </summary>
public static class TwitterAuthenticationExtensions
{
public static IServiceCollection AddTwitterAuthentication([NotNull] this IServiceCollection services, [NotNull] Action<TwitterOptions> configure)
{
return services.Configure(configure);
}
public static IServiceCollection AddTwitterAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config)
{
return services.Configure<TwitterOptions>(config);
}
}
}

View File

@ -7,7 +7,6 @@ using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.WebEncoders;
namespace Microsoft.AspNet.Authentication
@ -18,16 +17,11 @@ namespace Microsoft.AspNet.Authentication
protected AuthenticationMiddleware(
[NotNull] RequestDelegate next,
[NotNull] IOptions<TOptions> options,
[NotNull] TOptions options,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder,
ConfigureOptions<TOptions> configureOptions)
[NotNull] IUrlEncoder encoder)
{
Options = options.Value;
if (configureOptions != null)
{
configureOptions.Configure(Options);
}
Options = options;
Logger = loggerFactory.CreateLogger(this.GetType().FullName);
UrlEncoder = encoder;

View File

@ -2,8 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authentication;
using Microsoft.Framework.Internal;
@ -23,20 +21,5 @@ namespace Microsoft.Framework.DependencyInjection
services.Configure(configureOptions);
return services.AddAuthentication();
}
public static IServiceCollection AddClaimsTransformation([NotNull] this IServiceCollection services, [NotNull] Action<ClaimsTransformationOptions> configure)
{
return services.Configure(configure);
}
public static IServiceCollection AddClaimsTransformation([NotNull] this IServiceCollection services, [NotNull] Func<ClaimsPrincipal, ClaimsPrincipal> transform)
{
return services.Configure<ClaimsTransformationOptions>(o => o.Transformer = new ClaimsTransformer { TransformSyncDelegate = transform });
}
public static IServiceCollection AddClaimsTransformation([NotNull] this IServiceCollection services, [NotNull] Func<ClaimsPrincipal, Task<ClaimsPrincipal>> asyncTransform)
{
return services.Configure<ClaimsTransformationOptions>(o => o.Transformer = new ClaimsTransformer { TransformAsyncDelegate = asyncTransform });
}
}
}

View File

@ -2,9 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authentication;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder
{
@ -16,11 +16,28 @@ namespace Microsoft.AspNet.Builder
/// <summary>
/// Adds a claims transformation middleware to your web application pipeline.
/// </summary>
/// <param name="options">The options for the middleware</param>
/// <param name="app">The IApplicationBuilder passed to your configuration method</param>
/// <returns>The original app parameter</returns>
public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app)
public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, ClaimsTransformationOptions options)
{
return app.UseClaimsTransformation(configureOptions: o => { });
return app.UseMiddleware<ClaimsTransformationMiddleware>(options);
}
/// <summary>
/// Adds a claims transformation middleware to your web application pipeline.
/// </summary>
/// <param name="options">The options for the middleware</param>
/// <param name="app">The IApplicationBuilder passed to your configuration method</param>
/// <returns>The original app parameter</returns>
public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, Func<ClaimsPrincipal, Task<ClaimsPrincipal>> transform)
{
var options = new ClaimsTransformationOptions();
options.Transformer = new ClaimsTransformer
{
OnTransform = transform
};
return app.UseClaimsTransformation(options);
}
/// <summary>
@ -29,10 +46,14 @@ namespace Microsoft.AspNet.Builder
/// <param name="app">The IApplicationBuilder passed to your configuration method</param>
/// <param name="configureOptions">Used to configure the options for the middleware</param>
/// <returns>The original app parameter</returns>
public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, [NotNull] Action<ClaimsTransformationOptions> configureOptions)
public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, Action<ClaimsTransformationOptions> configureOptions)
{
return app.UseMiddleware<ClaimsTransformationMiddleware>(
new ConfigureOptions<ClaimsTransformationOptions>(configureOptions));
var options = new ClaimsTransformationOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseClaimsTransformation(options);
}
}
}

View File

@ -80,6 +80,5 @@ namespace Microsoft.AspNet.Authentication
{
auth.Handler = PriorHandler;
}
}
}

View File

@ -4,7 +4,6 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Authentication
@ -15,14 +14,9 @@ namespace Microsoft.AspNet.Authentication
public ClaimsTransformationMiddleware(
[NotNull] RequestDelegate next,
[NotNull] IOptions<ClaimsTransformationOptions> options,
ConfigureOptions<ClaimsTransformationOptions> configureOptions)
[NotNull] ClaimsTransformationOptions options)
{
Options = options.Value;
if (configureOptions != null)
{
configureOptions.Configure(Options);
}
Options = options;
_next = next;
}

View File

@ -1,44 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Authentication
{
public class ClaimsTransformationOptions
{
public IClaimsTransformer Transformer { get; set; }
}
public interface IClaimsTransformer
{
Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal);
ClaimsPrincipal Transform(ClaimsPrincipal principal);
}
public class ClaimsTransformer : IClaimsTransformer
{
public Func<ClaimsPrincipal, Task<ClaimsPrincipal>> TransformAsyncDelegate { get; set; }
public Func<ClaimsPrincipal, ClaimsPrincipal> TransformSyncDelegate { get; set; }
public virtual ClaimsPrincipal Transform(ClaimsPrincipal principal)
{
if (TransformSyncDelegate != null)
{
return TransformSyncDelegate(principal);
}
return principal;
}
public virtual Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
if (TransformAsyncDelegate != null)
{
return TransformAsyncDelegate(principal);
}
return Task.FromResult(Transform(principal));
}
}
}

View File

@ -0,0 +1,19 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Authentication
{
public class ClaimsTransformer : IClaimsTransformer
{
public Func<ClaimsPrincipal, Task<ClaimsPrincipal>> OnTransform { get; set; }
public virtual Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
return OnTransform?.Invoke(principal) ?? Task.FromResult(principal);
}
}
}

View File

@ -16,7 +16,7 @@ namespace Microsoft.AspNet.Authentication
if (auth == null)
{
auth = new HttpAuthenticationFeature();
context.Features.Set<IHttpAuthenticationFeature>(auth);
context.Features.Set(auth);
}
return auth;
}

View File

@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Security.Claims;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Authentication
{
public interface IClaimsTransformer
{
Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal);
}
}

View File

@ -8,7 +8,7 @@ using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
public static class ServiceCollectionExtensions
public static class AuthorizationServiceCollectionExtensions
{
public static IServiceCollection AddAuthorization([NotNull] this IServiceCollection services)
{

View File

@ -232,17 +232,7 @@ namespace Microsoft.AspNet.Authentication.Cookies
baseAddress: null,
claimsTransform: o => o.Transformer = new ClaimsTransformer
{
TransformSyncDelegate = p =>
{
if (!p.Identities.Any(i => i.AuthenticationType == "xform"))
{
var id = new ClaimsIdentity("xform");
id.AddClaim(new Claim("sync", "no"));
p.AddIdentity(id);
}
return p;
},
TransformAsyncDelegate = p =>
OnTransform = p =>
{
if (!p.Identities.Any(i => i.AuthenticationType == "xform"))
{
@ -745,22 +735,6 @@ namespace Microsoft.AspNet.Authentication.Cookies
Assert.True(transaction.SetCookie[0].StartsWith(".AspNet.Cookies="));
}
[Fact]
public async Task UseCookieWithOutInstanceDoesUseSharedOptions()
{
var server = TestServer.Create(app =>
{
app.UseCookieAuthentication(options => options.CookieName = "One");
app.UseCookieAuthentication(options => options.AuthenticationScheme = "Two");
app.Run(context => context.Authentication.SignInAsync("Two", new ClaimsPrincipal(new ClaimsIdentity())));
}, services => services.AddAuthentication());
var transaction = await server.SendAsync("http://example.com");
Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode);
Assert.True(transaction.SetCookie[0].StartsWith("One="));
}
[Fact]
public async Task MapWithSignInOnlyRedirectToReturnUrlOnLoginPath()
{

View File

@ -29,16 +29,7 @@ namespace Microsoft.AspNet.Authentication.Facebook
var server = CreateServer(
app =>
{
app.UseFacebookAuthentication();
app.UseCookieAuthentication();
},
services =>
{
services.AddAuthentication(options =>
{
options.SignInScheme = "External";
});
services.AddFacebookAuthentication(options =>
app.UseFacebookAuthentication(options =>
{
options.AppId = "Test App Id";
options.AppSecret = "Test App Secret";
@ -51,12 +42,19 @@ namespace Microsoft.AspNet.Authentication.Facebook
}
};
});
services.AddCookieAuthentication(options =>
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = "External";
options.AutomaticAuthentication = true;
});
},
services =>
{
services.AddAuthentication(options =>
{
options.SignInScheme = "External";
});
},
context =>
{
// REVIEW: Gross.
@ -74,19 +72,15 @@ namespace Microsoft.AspNet.Authentication.Facebook
{
var server = CreateServer(app =>
app.Map("/base", map => {
map.UseFacebookAuthentication();
map.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Facebook", new AuthenticationProperties() { RedirectUri = "/" })));
}),
services =>
{
services.AddAuthentication();
services.AddFacebookAuthentication(options =>
map.UseFacebookAuthentication(options =>
{
options.AppId = "Test App Id";
options.AppSecret = "Test App Secret";
options.SignInScheme = "External";
});
},
map.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Facebook", new AuthenticationProperties() { RedirectUri = "/" })));
}),
services => services.AddAuthentication(),
handler: null);
var transaction = await server.SendAsync("http://example.com/base/login");
Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
@ -105,19 +99,15 @@ namespace Microsoft.AspNet.Authentication.Facebook
var server = CreateServer(
app =>
{
app.UseFacebookAuthentication();
app.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Facebook", new AuthenticationProperties() { RedirectUri = "/" })));
},
services =>
{
services.AddAuthentication();
services.AddFacebookAuthentication(options =>
app.UseFacebookAuthentication(options =>
{
options.AppId = "Test App Id";
options.AppSecret = "Test App Secret";
options.SignInScheme = "External";
});
app.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Facebook", new AuthenticationProperties() { RedirectUri = "/" })));
},
services => services.AddAuthentication(),
handler: null);
var transaction = await server.SendAsync("http://example.com/login");
Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
@ -136,24 +126,16 @@ namespace Microsoft.AspNet.Authentication.Facebook
var server = CreateServer(
app =>
{
app.UseFacebookAuthentication();
app.UseCookieAuthentication();
},
services =>
{
services.AddAuthentication(options =>
{
options.SignInScheme = "External";
});
services.AddFacebookAuthentication(options =>
app.UseFacebookAuthentication(options =>
{
options.AppId = "Test App Id";
options.AppSecret = "Test App Secret";
});
services.AddCookieAuthentication(options =>
{
options.AuthenticationScheme = "External";
});
app.UseCookieAuthentication(options => options.AuthenticationScheme = "External");
},
services =>
{
services.AddAuthentication(options => options.SignInScheme = "External");
},
context =>
{
@ -181,13 +163,7 @@ namespace Microsoft.AspNet.Authentication.Facebook
var server = CreateServer(
app =>
{
app.UseFacebookAuthentication();
app.UseCookieAuthentication();
},
services =>
{
services.AddAuthentication();
services.AddFacebookAuthentication(options =>
app.UseFacebookAuthentication(options =>
{
options.AppId = "Test App Id";
options.AppSecret = "Test App Secret";
@ -224,6 +200,11 @@ namespace Microsoft.AspNet.Authentication.Facebook
}
};
});
app.UseCookieAuthentication();
},
services =>
{
services.AddAuthentication();
}, handler: null);
var properties = new AuthenticationProperties();

View File

@ -548,7 +548,13 @@ namespace Microsoft.AspNet.Authentication.Google
options.AutomaticAuthentication = true;
});
app.UseGoogleAuthentication(configureOptions);
app.UseClaimsTransformation();
app.UseClaimsTransformation(p =>
{
var id = new ClaimsIdentity("xform");
id.AddClaim(new Claim("xform", "yup"));
p.AddIdentity(id);
return Task.FromResult(p);
});
app.Use(async (context, next) =>
{
var req = context.Request;
@ -601,13 +607,6 @@ namespace Microsoft.AspNet.Authentication.Google
services =>
{
services.AddAuthentication(options => options.SignInScheme = TestExtensions.CookieAuthenticationScheme);
services.AddClaimsTransformation(p =>
{
var id = new ClaimsIdentity("xform");
id.AddClaim(new Claim("xform", "yup"));
p.AddIdentity(id);
return p;
});
});
}
}

View File

@ -15,7 +15,6 @@ using Microsoft.AspNet.Http.Authentication;
using Microsoft.AspNet.TestHost;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.WebEncoders;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Xunit;
@ -58,7 +57,7 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
public async Task AuthenticateCoreState(Action<OpenIdConnectOptions> action, OpenIdConnectMessage message)
{
var handler = new OpenIdConnectHandlerForTestingAuthenticate();
var server = CreateServer(new ConfigureOptions<OpenIdConnectOptions>(action), UrlEncoder.Default, handler);
var server = CreateServer(action, UrlEncoder.Default, handler);
await server.CreateClient().PostAsync("http://localhost", new FormUrlEncodedContent(message.Parameters.Where(pair => pair.Value != null)));
}
@ -116,11 +115,13 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
};
}
private static TestServer CreateServer(ConfigureOptions<OpenIdConnectOptions> options, IUrlEncoder encoder, OpenIdConnectHandler handler = null)
private static TestServer CreateServer(Action<OpenIdConnectOptions> configureOptions, IUrlEncoder encoder, OpenIdConnectHandler handler = null)
{
return TestServer.Create(
app =>
{
var options = new OpenIdConnectOptions();
configureOptions(options);
app.UseMiddleware<OpenIdConnectMiddlewareForTestingAuthenticate>(options, encoder, handler);
app.Use(async (context, next) =>
{

View File

@ -27,11 +27,10 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
IUrlEncoder encoder,
IServiceProvider services,
IOptions<SharedAuthenticationOptions> sharedOptions,
IOptions<OpenIdConnectOptions> options,
ConfigureOptions<OpenIdConnectOptions> configureOptions = null,
OpenIdConnectOptions options,
OpenIdConnectHandler handler = null
)
: base(next, dataProtectionProvider, loggerFactory, encoder, services, sharedOptions, options, configureOptions)
: base(next, dataProtectionProvider, loggerFactory, encoder, services, sharedOptions, options)
{
_handler = handler;
var customFactory = loggerFactory as InMemoryLoggerFactory;