UseOauth now requires an instance of options

This commit is contained in:
Hao Kung 2015-09-09 11:05:14 -07:00
parent 76fd055d8e
commit 5bc13cbd6b
4 changed files with 60 additions and 50 deletions

View File

@ -52,17 +52,21 @@ namespace CookieSample
options.AppSecret = "a124463c4719c94b4228d9a240e5dc1a"; options.AppSecret = "a124463c4719c94b4228d9a240e5dc1a";
}); });
app.UseOAuthAuthentication("Google-AccessToken", options => var googleOptions = new OAuthAuthenticationOptions
{ {
options.ClientId = "560027070069-37ldt4kfuohhu3m495hk2j4pjp92d382.apps.googleusercontent.com"; AuthenticationScheme = "Google-AccessToken",
options.ClientSecret = "n2Q-GEw9RQjzcRbU3qhfTj8f"; Caption = "Google-AccessToken",
options.CallbackPath = new PathString("/signin-google-token"); ClientId = "560027070069-37ldt4kfuohhu3m495hk2j4pjp92d382.apps.googleusercontent.com",
options.AuthorizationEndpoint = GoogleAuthenticationDefaults.AuthorizationEndpoint; ClientSecret = "n2Q-GEw9RQjzcRbU3qhfTj8f",
options.TokenEndpoint = GoogleAuthenticationDefaults.TokenEndpoint; CallbackPath = new PathString("/signin-google-token"),
options.Scope.Add("openid"); AuthorizationEndpoint = GoogleAuthenticationDefaults.AuthorizationEndpoint,
options.Scope.Add("profile"); TokenEndpoint = GoogleAuthenticationDefaults.TokenEndpoint
options.Scope.Add("email"); };
}); googleOptions.Scope.Add("openid");
googleOptions.Scope.Add("profile");
googleOptions.Scope.Add("email");
app.UseOAuthAuthentication(googleOptions);
// https://console.developers.google.com/project // https://console.developers.google.com/project
app.UseGoogleAuthentication(options => app.UseGoogleAuthentication(options =>
@ -95,16 +99,18 @@ namespace CookieSample
The sample app can then be run via: The sample app can then be run via:
dnx . web dnx . web
*/ */
app.UseOAuthAuthentication("Microsoft-AccessToken", options => var msOAuthOptions = new OAuthAuthenticationOptions
{ {
options.Caption = "MicrosoftAccount-AccessToken - Requires project changes"; AuthenticationScheme = "Microsoft-AccessToken",
options.ClientId = "00000000480FF62E"; Caption = "MicrosoftAccount-AccessToken - Requires project changes",
options.ClientSecret = "bLw2JIvf8Y1TaToipPEqxTVlOeJwCUsr"; ClientId = "00000000480FF62E",
options.CallbackPath = new PathString("/signin-microsoft-token"); ClientSecret = "bLw2JIvf8Y1TaToipPEqxTVlOeJwCUsr",
options.AuthorizationEndpoint = MicrosoftAccountAuthenticationDefaults.AuthorizationEndpoint; CallbackPath = new PathString("/signin-microsoft-token"),
options.TokenEndpoint = MicrosoftAccountAuthenticationDefaults.TokenEndpoint; AuthorizationEndpoint = MicrosoftAccountAuthenticationDefaults.AuthorizationEndpoint,
options.Scope.Add("wl.basic"); TokenEndpoint = MicrosoftAccountAuthenticationDefaults.TokenEndpoint
}); };
msOAuthOptions.Scope.Add("wl.basic");
app.UseOAuthAuthentication(msOAuthOptions);
app.UseMicrosoftAccountAuthentication(options => app.UseMicrosoftAccountAuthentication(options =>
{ {
@ -115,27 +121,31 @@ namespace CookieSample
}); });
// https://github.com/settings/applications/ // https://github.com/settings/applications/
app.UseOAuthAuthentication("GitHub-AccessToken", options => app.UseOAuthAuthentication(new OAuthAuthenticationOptions
{ {
options.ClientId = "8c0c5a572abe8fe89588"; AuthenticationScheme = "GitHub-AccessToken",
options.ClientSecret = "e1d95eaf03461d27acd6f49d4fc7bf19d6ac8cda"; Caption = "Github-AccessToken",
options.CallbackPath = new PathString("/signin-github-token"); ClientId = "8c0c5a572abe8fe89588",
options.AuthorizationEndpoint = "https://github.com/login/oauth/authorize"; ClientSecret = "e1d95eaf03461d27acd6f49d4fc7bf19d6ac8cda",
options.TokenEndpoint = "https://github.com/login/oauth/access_token"; CallbackPath = new PathString("/signin-github-token"),
AuthorizationEndpoint = "https://github.com/login/oauth/authorize",
TokenEndpoint = "https://github.com/login/oauth/access_token"
}); });
app.UseOAuthAuthentication("GitHub", options => app.UseOAuthAuthentication(new OAuthAuthenticationOptions
{ {
options.ClientId = "49e302895d8b09ea5656"; AuthenticationScheme = "GitHub",
options.ClientSecret = "98f1bf028608901e9df91d64ee61536fe562064b"; Caption = "Github",
options.CallbackPath = new PathString("/signin-github"); ClientId = "49e302895d8b09ea5656",
options.AuthorizationEndpoint = "https://github.com/login/oauth/authorize"; ClientSecret = "98f1bf028608901e9df91d64ee61536fe562064b",
options.TokenEndpoint = "https://github.com/login/oauth/access_token"; CallbackPath = new PathString("/signin-github"),
options.UserInformationEndpoint = "https://api.github.com/user"; AuthorizationEndpoint = "https://github.com/login/oauth/authorize",
options.ClaimsIssuer = "OAuth2-Github"; TokenEndpoint = "https://github.com/login/oauth/access_token",
options.SaveTokensAsClaims = false; UserInformationEndpoint = "https://api.github.com/user",
ClaimsIssuer = "OAuth2-Github",
SaveTokensAsClaims = false,
// Retrieving user information is unique to each provider. // Retrieving user information is unique to each provider.
options.Events = new OAuthAuthenticationEvents Events = new OAuthAuthenticationEvents
{ {
OnAuthenticated = async context => OnAuthenticated = async context =>
{ {
@ -180,8 +190,8 @@ namespace CookieSample
"urn:github:url", link, "urn:github:url", link,
ClaimValueTypes.String, context.Options.ClaimsIssuer)); ClaimValueTypes.String, context.Options.ClaimsIssuer));
} }
}, }
}; }
}); });
// Choose an authentication type // Choose an authentication type

View File

@ -19,18 +19,11 @@ namespace Microsoft.AspNet.Builder
/// <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] string authenticationScheme, Action<OAuthAuthenticationOptions> configureOptions = null) public static IApplicationBuilder UseOAuthAuthentication([NotNull] this IApplicationBuilder app, [NotNull] IOptions<OAuthAuthenticationOptions> options)
{ {
return app.UseMiddleware<OAuthAuthenticationMiddleware<OAuthAuthenticationOptions>>( return app.UseMiddleware<OAuthAuthenticationMiddleware<OAuthAuthenticationOptions>>(
new ConfigureOptions<OAuthAuthenticationOptions>(options => options,
{ new ConfigureOptions<OAuthAuthenticationOptions>(o => { }));
options.AuthenticationScheme = authenticationScheme;
options.Caption = authenticationScheme;
if (configureOptions != null)
{
configureOptions(options);
}
}));
} }
} }
} }

View File

@ -4,16 +4,16 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Security.Claims;
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="OAuthAuthenticationMiddleware"/>. /// Configuration options for <see cref="OAuthAuthenticationMiddleware"/>.
/// </summary> /// </summary>
public class OAuthAuthenticationOptions : AuthenticationOptions public class OAuthAuthenticationOptions : AuthenticationOptions, IOptions<OAuthAuthenticationOptions>
{ {
/// <summary> /// <summary>
/// Gets or sets the provider-assigned client id. /// Gets or sets the provider-assigned client id.
@ -115,5 +115,13 @@ 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;
OAuthAuthenticationOptions IOptions<OAuthAuthenticationOptions>.Value
{
get
{
return this;
}
}
} }
} }

View File

@ -19,7 +19,6 @@ using Microsoft.AspNet.Http.Authentication;
using Microsoft.AspNet.Http.Features.Authentication; using Microsoft.AspNet.Http.Features.Authentication;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Shouldly; using Shouldly;
using Xunit; using Xunit;