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

View File

@ -2,7 +2,6 @@ using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Security.Claims; using System.Security.Claims;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Authentication.Cookies; using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.AspNet.Authentication.Google; using Microsoft.AspNet.Authentication.Google;
using Microsoft.AspNet.Authentication.MicrosoftAccount; using Microsoft.AspNet.Authentication.MicrosoftAccount;
@ -21,18 +20,7 @@ namespace CookieSample
{ {
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddAuthentication(); services.AddAuthentication(options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
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;
});
} }
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)

View File

@ -4,7 +4,6 @@
using System; using System;
using Microsoft.AspNet.Authentication.Cookies; using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder namespace Microsoft.AspNet.Builder
{ {
@ -17,13 +16,10 @@ namespace Microsoft.AspNet.Builder
/// Adds a cookie-based authentication middleware to your web application pipeline. /// Adds a cookie-based authentication middleware to your web application pipeline.
/// </summary> /// </summary>
/// <param name="app">The IApplicationBuilder passed to your configuration method</param> /// <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> /// <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>( return app.UseCookieAuthentication(new CookieAuthenticationOptions());
new ConfigureOptions<CookieAuthenticationOptions>(configureOptions ?? (o => { })));
} }
/// <summary> /// <summary>
@ -31,12 +27,26 @@ namespace Microsoft.AspNet.Builder
/// </summary> /// </summary>
/// <param name="app">The IApplicationBuilder passed to your configuration method</param> /// <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="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> /// <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, var options = new CookieAuthenticationOptions();
new ConfigureOptions<CookieAuthenticationOptions>(o => { })); 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.AspNet.DataProtection;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.WebEncoders; using Microsoft.Framework.WebEncoders;
namespace Microsoft.AspNet.Authentication.Cookies namespace Microsoft.AspNet.Authentication.Cookies
@ -18,9 +17,8 @@ namespace Microsoft.AspNet.Authentication.Cookies
[NotNull] IDataProtectionProvider dataProtectionProvider, [NotNull] IDataProtectionProvider dataProtectionProvider,
[NotNull] ILoggerFactory loggerFactory, [NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder urlEncoder, [NotNull] IUrlEncoder urlEncoder,
[NotNull] IOptions<CookieAuthenticationOptions> options, [NotNull] CookieAuthenticationOptions options)
ConfigureOptions<CookieAuthenticationOptions> configureOptions) : base(next, options, loggerFactory, urlEncoder)
: base(next, options, loggerFactory, urlEncoder, configureOptions)
{ {
if (Options.Events == null) if (Options.Events == null)
{ {

View File

@ -4,7 +4,6 @@
using System; using System;
using Microsoft.AspNet.Authentication.Facebook; using Microsoft.AspNet.Authentication.Facebook;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder namespace Microsoft.AspNet.Builder
{ {
@ -18,10 +17,25 @@ namespace Microsoft.AspNet.Builder
/// </summary> /// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param> /// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns> /// <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>( return app.UseMiddleware<FacebookMiddleware>(options);
new ConfigureOptions<FacebookOptions>(configureOptions ?? (o => { }))); }
/// <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] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder, [NotNull] IUrlEncoder encoder,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions, [NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<FacebookOptions> options, [NotNull] FacebookOptions options)
ConfigureOptions<FacebookOptions> configureOptions = null) : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options)
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options, configureOptions)
{ {
if (string.IsNullOrEmpty(Options.AppId)) 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 System;
using Microsoft.AspNet.Authentication.Google; using Microsoft.AspNet.Authentication.Google;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder namespace Microsoft.AspNet.Builder
{ {
@ -13,6 +12,17 @@ namespace Microsoft.AspNet.Builder
/// </summary> /// </summary>
public static class GoogleAppBuilderExtensions 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> /// <summary>
/// Authenticate users using Google OAuth 2.0. /// Authenticate users using Google OAuth 2.0.
/// </summary> /// </summary>
@ -20,10 +30,14 @@ namespace Microsoft.AspNet.Builder
/// <param name="configureOptions">Used to configure Middleware options.</param> /// <param name="configureOptions">Used to configure Middleware options.</param>
/// <param name="optionsName">Name of the options instance to be used</param> /// <param name="optionsName">Name of the options instance to be used</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns> /// <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>( var options = new GoogleOptions();
new ConfigureOptions<GoogleOptions>(configureOptions ?? (o => { }))); if (configureOptions != null)
{
configureOptions(options);
}
return app.UseGoogleAuthentication(options);
} }
} }
} }

View File

@ -34,9 +34,8 @@ namespace Microsoft.AspNet.Authentication.Google
[NotNull] ILoggerFactory loggerFactory, [NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder, [NotNull] IUrlEncoder encoder,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions, [NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<GoogleOptions> options, [NotNull] GoogleOptions options)
ConfigureOptions<GoogleOptions> configureOptions = null) : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options)
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options, configureOptions)
{ {
if (Options.Scope.Count == 0) 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="app">The application builder</param>
/// <param name="options">Options which control the processing of the bearer header.</param> /// <param name="options">Options which control the processing of the bearer header.</param>
/// <returns>The application builder</returns> /// <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>( return app.UseMiddleware<JwtBearerMiddleware>(options);
new ConfigureOptions<JwtBearerOptions>(configureOptions ?? (o => { }))); }
/// <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] RequestDelegate next,
[NotNull] ILoggerFactory loggerFactory, [NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder, [NotNull] IUrlEncoder encoder,
[NotNull] IOptions<JwtBearerOptions> options, [NotNull] JwtBearerOptions options)
ConfigureOptions<JwtBearerOptions> configureOptions) : base(next, options, loggerFactory, encoder)
: base(next, options, loggerFactory, encoder, configureOptions)
{ {
if (Options.Events == null) 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 System;
using Microsoft.AspNet.Authentication.MicrosoftAccount; using Microsoft.AspNet.Authentication.MicrosoftAccount;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder namespace Microsoft.AspNet.Builder
{ {
@ -13,10 +12,19 @@ namespace Microsoft.AspNet.Builder
/// </summary> /// </summary>
public static class MicrosoftAccountAuthenticationExtensions 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>( return app.UseMiddleware<MicrosoftAccountMiddleware>(options);
new ConfigureOptions<MicrosoftAccountOptions>(configureOptions ?? (o => { }))); }
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] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder, [NotNull] IUrlEncoder encoder,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions, [NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<MicrosoftAccountOptions> options, [NotNull] MicrosoftAccountOptions options)
ConfigureOptions<MicrosoftAccountOptions> configureOptions = null) : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options)
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options, configureOptions)
{ {
if (Options.Scope.Count == 0) 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> /// </summary>
public static class OAuthExtensions 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> /// <summary>
/// Authenticate users using OAuth. /// Authenticate users using OAuth.
/// </summary> /// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param> /// <param name="app">The <see cref="IApplicationBuilder"/> passed to the configure method.</param>
/// <param name="options">The middleware configuration options.</param> /// <param name="options">The middleware configuration options.</param>
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns> /// <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>>( return app.UseMiddleware<OAuthMiddleware<OAuthOptions>>(options);
options,
new ConfigureOptions<OAuthOptions>(o => { }));
} }
} }
} }

View File

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

View File

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

View File

@ -3,7 +3,7 @@
using System; using System;
using Microsoft.AspNet.Authentication.OpenIdConnect; using Microsoft.AspNet.Authentication.OpenIdConnect;
using Microsoft.Framework.OptionsModel; using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Builder namespace Microsoft.AspNet.Builder
{ {
@ -18,10 +18,15 @@ namespace Microsoft.AspNet.Builder
/// <param name="app">The application builder</param> /// <param name="app">The application builder</param>
/// <param name="options">Options which control the processing of the OpenIdConnect protocol and token validation.</param> /// <param name="options">Options which control the processing of the OpenIdConnect protocol and token validation.</param>
/// <returns>The application builder</returns> /// <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> /// <summary>
@ -30,7 +35,7 @@ namespace Microsoft.AspNet.Builder
/// <param name="app">The application builder</param> /// <param name="app">The application builder</param>
/// <param name="options">Options which control the processing of the OpenIdConnect protocol and token validation.</param> /// <param name="options">Options which control the processing of the OpenIdConnect protocol and token validation.</param>
/// <returns>The application builder</returns> /// <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); return app.UseMiddleware<OpenIdConnectMiddleware>(options);
} }

View File

@ -31,10 +31,7 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
/// <param name="encoder"></param> /// <param name="encoder"></param>
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="sharedOptions"></param> /// <param name="sharedOptions"></param>
/// <param name="options">a <see cref="IOptions{OpenIdConnectOptions}"/> instance that will supply <see cref="OpenIdConnectOptions"/> /// <param name="options"></param>
/// 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>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Managed by caller")] [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Managed by caller")]
public OpenIdConnectMiddleware( public OpenIdConnectMiddleware(
[NotNull] RequestDelegate next, [NotNull] RequestDelegate next,
@ -43,9 +40,8 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
[NotNull] IUrlEncoder encoder, [NotNull] IUrlEncoder encoder,
[NotNull] IServiceProvider services, [NotNull] IServiceProvider services,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions, [NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<OpenIdConnectOptions> options, [NotNull] OpenIdConnectOptions options)
ConfigureOptions<OpenIdConnectOptions> configureOptions = null) : base(next, options, loggerFactory, encoder)
: base(next, options, loggerFactory, encoder, configureOptions)
{ {
if (string.IsNullOrEmpty(Options.SignInScheme) && !string.IsNullOrEmpty(sharedOptions.Value.SignInScheme)) 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 System;
using Microsoft.AspNet.Authentication.Twitter; using Microsoft.AspNet.Authentication.Twitter;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder namespace Microsoft.AspNet.Builder
{ {
@ -15,8 +14,18 @@ namespace Microsoft.AspNet.Builder
{ {
public static IApplicationBuilder UseTwitterAuthentication([NotNull] this IApplicationBuilder app, Action<TwitterOptions> configureOptions = null) public static IApplicationBuilder UseTwitterAuthentication([NotNull] this IApplicationBuilder app, Action<TwitterOptions> configureOptions = null)
{ {
return app.UseMiddleware<TwitterMiddleware>( var options = new TwitterOptions();
new ConfigureOptions<TwitterOptions>(configureOptions ?? (o => { }))); 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] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder, [NotNull] IUrlEncoder encoder,
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions, [NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
[NotNull] IOptions<TwitterOptions> options, [NotNull] TwitterOptions options)
ConfigureOptions<TwitterOptions> configureOptions = null) : base(next, options, loggerFactory, encoder)
: base(next, options, loggerFactory, encoder, configureOptions)
{ {
if (string.IsNullOrEmpty(Options.ConsumerSecret)) 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.AspNet.Http;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.WebEncoders; using Microsoft.Framework.WebEncoders;
namespace Microsoft.AspNet.Authentication namespace Microsoft.AspNet.Authentication
@ -18,16 +17,11 @@ namespace Microsoft.AspNet.Authentication
protected AuthenticationMiddleware( protected AuthenticationMiddleware(
[NotNull] RequestDelegate next, [NotNull] RequestDelegate next,
[NotNull] IOptions<TOptions> options, [NotNull] TOptions options,
[NotNull] ILoggerFactory loggerFactory, [NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder, [NotNull] IUrlEncoder encoder)
ConfigureOptions<TOptions> configureOptions)
{ {
Options = options.Value; Options = options;
if (configureOptions != null)
{
configureOptions.Configure(Options);
}
Logger = loggerFactory.CreateLogger(this.GetType().FullName); Logger = loggerFactory.CreateLogger(this.GetType().FullName);
UrlEncoder = encoder; 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authentication; using Microsoft.AspNet.Authentication;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
@ -23,20 +21,5 @@ namespace Microsoft.Framework.DependencyInjection
services.Configure(configureOptions); services.Configure(configureOptions);
return services.AddAuthentication(); 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authentication; using Microsoft.AspNet.Authentication;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Builder namespace Microsoft.AspNet.Builder
{ {
@ -16,11 +16,28 @@ namespace Microsoft.AspNet.Builder
/// <summary> /// <summary>
/// Adds a claims transformation middleware to your web application pipeline. /// Adds a claims transformation middleware to your web application pipeline.
/// </summary> /// </summary>
/// <param name="options">The options for the middleware</param>
/// <param name="app">The IApplicationBuilder passed to your configuration method</param> /// <param name="app">The IApplicationBuilder passed to your configuration method</param>
/// <returns>The original app parameter</returns> /// <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> /// <summary>
@ -29,10 +46,14 @@ namespace Microsoft.AspNet.Builder
/// <param name="app">The IApplicationBuilder passed to your configuration method</param> /// <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="configureOptions">Used to configure the options for the middleware</param>
/// <returns>The original app parameter</returns> /// <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>( var options = new ClaimsTransformationOptions();
new ConfigureOptions<ClaimsTransformationOptions>(configureOptions)); if (configureOptions != null)
{
configureOptions(options);
}
return app.UseClaimsTransformation(options);
} }
} }
} }

View File

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

View File

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

View File

@ -1,44 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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 namespace Microsoft.AspNet.Authentication
{ {
public class ClaimsTransformationOptions public class ClaimsTransformationOptions
{ {
public IClaimsTransformer Transformer { get; set; } 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) if (auth == null)
{ {
auth = new HttpAuthenticationFeature(); auth = new HttpAuthenticationFeature();
context.Features.Set<IHttpAuthenticationFeature>(auth); context.Features.Set(auth);
} }
return 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 namespace Microsoft.Framework.DependencyInjection
{ {
public static class ServiceCollectionExtensions public static class AuthorizationServiceCollectionExtensions
{ {
public static IServiceCollection AddAuthorization([NotNull] this IServiceCollection services) public static IServiceCollection AddAuthorization([NotNull] this IServiceCollection services)
{ {

View File

@ -232,17 +232,7 @@ namespace Microsoft.AspNet.Authentication.Cookies
baseAddress: null, baseAddress: null,
claimsTransform: o => o.Transformer = new ClaimsTransformer claimsTransform: o => o.Transformer = new ClaimsTransformer
{ {
TransformSyncDelegate = p => OnTransform = 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 =>
{ {
if (!p.Identities.Any(i => i.AuthenticationType == "xform")) 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=")); 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] [Fact]
public async Task MapWithSignInOnlyRedirectToReturnUrlOnLoginPath() public async Task MapWithSignInOnlyRedirectToReturnUrlOnLoginPath()
{ {

View File

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

View File

@ -548,7 +548,13 @@ namespace Microsoft.AspNet.Authentication.Google
options.AutomaticAuthentication = true; options.AutomaticAuthentication = true;
}); });
app.UseGoogleAuthentication(configureOptions); 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) => app.Use(async (context, next) =>
{ {
var req = context.Request; var req = context.Request;
@ -601,13 +607,6 @@ namespace Microsoft.AspNet.Authentication.Google
services => services =>
{ {
services.AddAuthentication(options => options.SignInScheme = TestExtensions.CookieAuthenticationScheme); 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.AspNet.TestHost;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.WebEncoders; using Microsoft.Framework.WebEncoders;
using Microsoft.IdentityModel.Protocols.OpenIdConnect; using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Xunit; using Xunit;
@ -58,7 +57,7 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
public async Task AuthenticateCoreState(Action<OpenIdConnectOptions> action, OpenIdConnectMessage message) public async Task AuthenticateCoreState(Action<OpenIdConnectOptions> action, OpenIdConnectMessage message)
{ {
var handler = new OpenIdConnectHandlerForTestingAuthenticate(); 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))); 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( return TestServer.Create(
app => app =>
{ {
var options = new OpenIdConnectOptions();
configureOptions(options);
app.UseMiddleware<OpenIdConnectMiddlewareForTestingAuthenticate>(options, encoder, handler); app.UseMiddleware<OpenIdConnectMiddlewareForTestingAuthenticate>(options, encoder, handler);
app.Use(async (context, next) => app.Use(async (context, next) =>
{ {

View File

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