Stardardizing middleware to use ConfigureOption lambda

This commit is contained in:
John Luo 2015-12-21 17:06:48 -08:00
parent 84279c07cf
commit 5837ce160a
13 changed files with 131 additions and 296 deletions

View File

@ -72,26 +72,29 @@ namespace CookieSample
// You must first create an app with facebook and add it's ID and Secret to your config.json or user-secrets.
// https://developers.facebook.com/apps/
app.UseFacebookAuthentication(new FacebookOptions()
app.UseFacebookAuthentication(options =>
{
AppId = Configuration["facebook:appid"],
AppSecret = Configuration["facebook:appsecret"],
Scope = { "email" },
Fields = { "name", "email" },
options.AppId = Configuration["facebook:appid"];
options.AppSecret = Configuration["facebook:appsecret"];
options.Scope.Add("email");
options.Fields.Add("name");
options.Fields.Add("email");
});
// See config.json
app.UseOAuthAuthentication(new OAuthOptions
app.UseOAuthAuthentication(options =>
{
AuthenticationScheme = "Google-AccessToken",
DisplayName = "Google-AccessToken",
ClientId = Configuration["google:clientid"],
ClientSecret = Configuration["google:clientsecret"],
CallbackPath = new PathString("/signin-google-token"),
AuthorizationEndpoint = GoogleDefaults.AuthorizationEndpoint,
TokenEndpoint = GoogleDefaults.TokenEndpoint,
Scope = { "openid", "profile", "email" },
SaveTokensAsClaims = true
options.AuthenticationScheme = "Google-AccessToken";
options.DisplayName = "Google-AccessToken";
options.ClientId = Configuration["google:clientid"];
options.ClientSecret = Configuration["google:clientsecret"];
options.CallbackPath = new PathString("/signin-google-token");
options.AuthorizationEndpoint = GoogleDefaults.AuthorizationEndpoint;
options.TokenEndpoint = GoogleDefaults.TokenEndpoint;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.SaveTokensAsClaims = true;
});
// See config.json
@ -148,17 +151,17 @@ namespace CookieSample
The sample app can then be run via:
dnx . web
*/
app.UseOAuthAuthentication(new OAuthOptions
app.UseOAuthAuthentication(options =>
{
AuthenticationScheme = "Microsoft-AccessToken",
DisplayName = "MicrosoftAccount-AccessToken - Requires project changes",
ClientId = Configuration["msa:clientid"],
ClientSecret = Configuration["msa:clientsecret"],
CallbackPath = new PathString("/signin-microsoft-token"),
AuthorizationEndpoint = MicrosoftAccountDefaults.AuthorizationEndpoint,
TokenEndpoint = MicrosoftAccountDefaults.TokenEndpoint,
Scope = { "wl.basic" },
SaveTokensAsClaims = true
options.AuthenticationScheme = "Microsoft-AccessToken";
options.DisplayName = "MicrosoftAccount-AccessToken - Requires project changes";
options.ClientId = Configuration["msa:clientid"];
options.ClientSecret = Configuration["msa:clientsecret"];
options.CallbackPath = new PathString("/signin-microsoft-token");
options.AuthorizationEndpoint = MicrosoftAccountDefaults.AuthorizationEndpoint;
options.TokenEndpoint = MicrosoftAccountDefaults.TokenEndpoint;
options.Scope.Add("wl.basic");
options.SaveTokensAsClaims = true;
});
//// You must first create an app with live.com and add it's ID and Secret to your config.json or user-secrets.
@ -172,32 +175,32 @@ namespace CookieSample
// See config.json
// https://github.com/settings/applications/
app.UseOAuthAuthentication(new OAuthOptions
app.UseOAuthAuthentication(options =>
{
AuthenticationScheme = "GitHub-AccessToken",
DisplayName = "Github-AccessToken",
ClientId = Configuration["github-token:clientid"],
ClientSecret = Configuration["github-token:clientsecret"],
CallbackPath = new PathString("/signin-github-token"),
AuthorizationEndpoint = "https://github.com/login/oauth/authorize",
TokenEndpoint = "https://github.com/login/oauth/access_token",
SaveTokensAsClaims = true
options.AuthenticationScheme = "GitHub-AccessToken";
options.DisplayName = "Github-AccessToken";
options.ClientId = Configuration["github-token:clientid"];
options.ClientSecret = Configuration["github-token:clientsecret"];
options.CallbackPath = new PathString("/signin-github-token");
options.AuthorizationEndpoint = "https://github.com/login/oauth/authorize";
options.TokenEndpoint = "https://github.com/login/oauth/access_token";
options.SaveTokensAsClaims = true;
});
// See config.json
app.UseOAuthAuthentication(new OAuthOptions
app.UseOAuthAuthentication(options =>
{
AuthenticationScheme = "GitHub",
DisplayName = "Github",
ClientId = Configuration["github:clientid"],
ClientSecret = Configuration["github:clientsecret"],
CallbackPath = new PathString("/signin-github"),
AuthorizationEndpoint = "https://github.com/login/oauth/authorize",
TokenEndpoint = "https://github.com/login/oauth/access_token",
UserInformationEndpoint = "https://api.github.com/user",
ClaimsIssuer = "OAuth2-Github",
options.AuthenticationScheme = "GitHub";
options.DisplayName = "Github";
options.ClientId = Configuration["github:clientid"];
options.ClientSecret = Configuration["github:clientsecret"];
options.CallbackPath = new PathString("/signin-github");
options.AuthorizationEndpoint = "https://github.com/login/oauth/authorize";
options.TokenEndpoint = "https://github.com/login/oauth/access_token";
options.UserInformationEndpoint = "https://api.github.com/user";
options.ClaimsIssuer = "OAuth2-Github";
// Retrieving user information is unique to each provider.
Events = new OAuthEvents
options.Events = new OAuthEvents
{
OnCreatingTicket = async context =>
{
@ -210,7 +213,7 @@ namespace CookieSample
response.EnsureSuccessStatusCode();
var user = JObject.Parse(await response.Content.ReadAsStringAsync());
var identifier = user.Value<string>("id");
if (!string.IsNullOrEmpty(identifier))
{
@ -243,7 +246,7 @@ namespace CookieSample
ClaimValueTypes.String, context.Options.ClaimsIssuer));
}
}
}
};
});
// Choose an authentication type

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(app));
}
return app.UseCookieAuthentication(new CookieAuthenticationOptions());
return app.UseCookieAuthentication(options => { });
}
/// <summary>
@ -38,32 +38,13 @@ namespace Microsoft.AspNet.Builder
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new CookieAuthenticationOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseCookieAuthentication(options);
}
/// <summary>
/// Adds the <see cref="CookieAuthenticationMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables cookie authentication capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">A <see cref="JwtBearerOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseCookieAuthentication(this IApplicationBuilder app, CookieAuthenticationOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
configureOptions(options);
return app.UseMiddleware<CookieAuthenticationMiddleware>(options);
}

View File

@ -11,27 +11,6 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class FacebookAppBuilderExtensions
{
/// <summary>
/// Adds the <see cref="FacebookMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables Facebook authentication capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">A <see cref="FacebookOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseFacebookAuthentication(this IApplicationBuilder app, FacebookOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
return app.UseMiddleware<FacebookMiddleware>(options);
}
/// <summary>
/// Adds the <see cref="FacebookMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables Facebook authentication capabilities.
/// </summary>
@ -44,13 +23,15 @@ namespace Microsoft.AspNet.Builder
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new FacebookOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseFacebookAuthentication(options);
configureOptions(options);
return app.UseMiddleware<FacebookMiddleware>(options);
}
}
}

View File

@ -11,27 +11,6 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class GoogleAppBuilderExtensions
{
/// <summary>
/// Adds the <see cref="GoogleMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables Google authentication capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">A <see cref="GoogleOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseGoogleAuthentication(this IApplicationBuilder app, GoogleOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
return app.UseMiddleware<GoogleMiddleware>(options);
}
/// <summary>
/// Adds the <see cref="GoogleMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables Google authentication capabilities.
/// </summary>
@ -44,13 +23,15 @@ namespace Microsoft.AspNet.Builder
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new GoogleOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseGoogleAuthentication(options);
configureOptions(options);
return app.UseMiddleware<GoogleMiddleware>(options);
}
}
}

View File

@ -11,33 +11,6 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class JwtBearerAppBuilderExtensions
{
/// <summary>
/// Adds the <see cref="JwtBearerMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables Bearer token processing capabilities.
/// 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 <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">A <see cref="JwtBearerOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseJwtBearerAuthentication(this IApplicationBuilder app, JwtBearerOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
return app.UseMiddleware<JwtBearerMiddleware>(options);
}
/// <summary>
/// Adds the <see cref="JwtBearerMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables Bearer token processing capabilities.
/// This middleware understands appropriately
@ -56,13 +29,15 @@ namespace Microsoft.AspNet.Builder
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new JwtBearerOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseJwtBearerAuthentication(options);
configureOptions(options);
return app.UseMiddleware<JwtBearerMiddleware>(options);
}
}
}

View File

@ -11,27 +11,6 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class MicrosoftAccountAppBuilderExtensions
{
/// <summary>
/// Adds the <see cref="MicrosoftAccountMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables Microsoft Account authentication capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">A <see cref="MicrosoftAccountOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseMicrosoftAccountAuthentication(this IApplicationBuilder app, MicrosoftAccountOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
return app.UseMiddleware<MicrosoftAccountMiddleware>(options);
}
/// <summary>
/// Adds the <see cref="MicrosoftAccountMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables Microsoft Account authentication capabilities.
/// </summary>
@ -44,13 +23,15 @@ namespace Microsoft.AspNet.Builder
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new MicrosoftAccountOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseMicrosoftAccountAuthentication(options);
configureOptions(options);
return app.UseMiddleware<MicrosoftAccountMiddleware>(options);
}
}
}

View File

@ -23,37 +23,13 @@ namespace Microsoft.AspNet.Builder
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new OAuthOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseOAuthAuthentication(options);
}
/// <summary>
/// Adds the <see cref="OAuthMiddleware{TOptions}"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables OAuth 2.0 authentication capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">A <see cref="OAuthOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseOAuthAuthentication(this IApplicationBuilder app, OAuthOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
configureOptions(options);
return app.UseMiddleware<OAuthMiddleware<OAuthOptions>>(options);
}

View File

@ -1,10 +1,7 @@
// 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.Collections.Generic;
using System.Net.Http;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Authentication;
namespace Microsoft.AspNet.Authentication.OAuth

View File

@ -15,7 +15,7 @@ namespace Microsoft.AspNet.Builder
/// Adds the <see cref="OpenIdConnectMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables OpenID Connect authentication capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">An action delegate to configure the provided <see cref="OpenIdConnectOptions"/>.</param>
/// <param name="configureOptions">An action delegate to configure the provided <see cref="OpenIdConnectOptions"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseOpenIdConnectAuthentication(this IApplicationBuilder app, Action<OpenIdConnectOptions> configureOptions)
{
@ -23,33 +23,13 @@ namespace Microsoft.AspNet.Builder
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new OpenIdConnectOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseOpenIdConnectAuthentication(options);
}
/// <summary>
/// Adds the <see cref="OpenIdConnectMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables OpenID Connect authentication capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">An <see cref="OpenIdConnectOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseOpenIdConnectAuthentication(this IApplicationBuilder app, OpenIdConnectOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
configureOptions(options);
return app.UseMiddleware<OpenIdConnectMiddleware>(options);
}

View File

@ -23,32 +23,13 @@ namespace Microsoft.AspNet.Builder
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new TwitterOptions();
if (configureOptions != null)
{
configureOptions(options);
}
return app.UseTwitterAuthentication(options);
}
/// <summary>
/// Adds the <see cref="TwitterMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables Twitter authentication capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">A <see cref="TwitterOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseTwitterAuthentication(this IApplicationBuilder app, TwitterOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
configureOptions(options);
return app.UseMiddleware<TwitterMiddleware>(options);
}

View File

@ -13,17 +13,6 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class ClaimsTransformationAppBuilderExtensions
{
/// <summary>
/// Adds the <see cref="ClaimsTransformationMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables claims transformation capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">A <see cref="ClaimsTransformationOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, ClaimsTransformationOptions options)
{
return app.UseMiddleware<ClaimsTransformationMiddleware>(options);
}
/// <summary>
/// Adds the <see cref="ClaimsTransformationMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables claims transformation capabilities.
/// </summary>
@ -32,12 +21,19 @@ namespace Microsoft.AspNet.Builder
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, Func<ClaimsPrincipal, Task<ClaimsPrincipal>> transform)
{
var options = new ClaimsTransformationOptions();
options.Transformer = new ClaimsTransformer
if (app == null)
{
OnTransform = transform
};
return app.UseClaimsTransformation(options);
throw new ArgumentNullException(nameof(app));
}
if (transform == null)
{
throw new ArgumentNullException(nameof(transform));
}
return app.UseClaimsTransformation(options =>
{
options.Transformer = new ClaimsTransformer { OnTransform = transform };
});
}
/// <summary>
@ -48,12 +44,19 @@ namespace Microsoft.AspNet.Builder
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, Action<ClaimsTransformationOptions> configureOptions)
{
var options = new ClaimsTransformationOptions();
if (configureOptions != null)
if (app == null)
{
configureOptions(options);
throw new ArgumentNullException(nameof(app));
}
return app.UseClaimsTransformation(options);
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new ClaimsTransformationOptions();
configureOptions(options);
return app.UseMiddleware<ClaimsTransformationMiddleware>(options);
}
}
}

View File

@ -11,17 +11,6 @@ namespace Microsoft.AspNet.Builder
/// </summary>
public static class CookiePolicyAppBuilderExtensions
{
/// <summary>
/// Adds the <see cref="CookiePolicyMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables cookie policy capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">A <see cref="CookiePolicyOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseCookiePolicy(this IApplicationBuilder app, CookiePolicyOptions options)
{
return app.UseMiddleware<CookiePolicyMiddleware>(options);
}
/// <summary>
/// Adds the <see cref="CookiePolicyMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables cookie policy capabilities.
/// </summary>
@ -30,12 +19,19 @@ namespace Microsoft.AspNet.Builder
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseCookiePolicy(this IApplicationBuilder app, Action<CookiePolicyOptions> configureOptions)
{
var options = new CookiePolicyOptions();
if (configureOptions != null)
if (app == null)
{
configureOptions(options);
throw new ArgumentNullException(nameof(app));
}
return app.UseCookiePolicy(options);
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new CookiePolicyOptions();
configureOptions(options);
return app.UseMiddleware<CookiePolicyMiddleware>(options);
}
}
}

View File

@ -793,7 +793,7 @@ namespace Microsoft.AspNet.Authentication.Cookies
.Configure(app =>
{
app.UseCookieAuthentication(options => options.CookieName = "One");
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseCookieAuthentication();
app.Run(context => context.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity())));
})
.ConfigureServices(services => services.AddAuthentication());