Skip to content

Commit 4ae3f69

Browse files
committed
Avoid manually using UrlEncoder.Encode() to prevent the state from being encoded twice
1 parent 7e66f8d commit 4ae3f69

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

samples/Mvc.Client/Startup.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,31 @@ public void Configure(IApplicationBuilder app)
3737
LogoutPath = new PathString("/signout")
3838
});
3939

40-
app.UseOpenIdAuthentication(new OpenIdAuthenticationOptions
40+
app.UseOpenIdAuthentication(options =>
4141
{
42-
AuthenticationScheme = "Orange",
43-
DisplayName = "Orange",
44-
Authority = new Uri("https://orange.fr/"),
45-
CallbackPath = new PathString("/signin-orange")
42+
options.AuthenticationScheme = "Orange";
43+
options.DisplayName = "Orange";
44+
options.Authority = new Uri("https://openid.orange.fr/");
45+
options.CallbackPath = new PathString("/signin-orange");
4646
});
4747

48-
app.UseOpenIdAuthentication(new OpenIdAuthenticationOptions
48+
app.UseOpenIdAuthentication(options =>
4949
{
50-
AuthenticationScheme = "StackExchange",
51-
DisplayName = "StackExchange",
52-
Authority = new Uri("https://openid.stackexchange.com/"),
53-
CallbackPath = new PathString("/signin-stackexchange")
50+
options.AuthenticationScheme = "StackExchange";
51+
options.DisplayName = "StackExchange";
52+
options.Authority = new Uri("https://openid.stackexchange.com/");
53+
options.CallbackPath = new PathString("/signin-stackexchange");
5454
});
5555

56-
app.UseOpenIdAuthentication(new OpenIdAuthenticationOptions
56+
app.UseOpenIdAuthentication(options =>
5757
{
58-
AuthenticationScheme = "Intuit",
59-
DisplayName = "Intuit",
60-
CallbackPath = new PathString("/signin-intuit"),
61-
Configuration = new OpenIdAuthenticationConfiguration
58+
options.AuthenticationScheme = "Intuit";
59+
options.DisplayName = "Intuit";
60+
options.CallbackPath = new PathString("/signin-intuit");
61+
options.Configuration = new OpenIdAuthenticationConfiguration
6262
{
6363
AuthenticationEndpoint = "https://openid.intuit.com/OpenId/Provider"
64-
}
64+
};
6565
});
6666

6767
app.UseSteamAuthentication();

src/AspNet.Security.OpenId/OpenIdAuthenticationExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ namespace Microsoft.AspNetCore.Builder
1313
{
1414
/// <summary>
1515
/// Exposes convenient extensions that can be used to add an instance
16-
/// of the OpenID authentication middleware in an ASP.NET 5 pipeline.
16+
/// of the OpenID authentication middleware in an ASP.NET Core pipeline.
1717
/// </summary>
1818
public static class OpenIdAuthenticationExtensions
1919
{
2020
/// <summary>
2121
/// Adds <see cref="OpenIdAuthenticationMiddleware{TOptions}"/> to the specified
22-
/// <see cref="IApplicationBuilder"/>, which enables OpenID2 authentication capabilities.
22+
/// <see cref="IApplicationBuilder"/>, which enables OpenID 2.0 authentication capabilities.
2323
/// </summary>
2424
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
25-
/// <param name="options">The <see cref="OpenIdAuthenticationOptions"/> used to configure the OAuth2 options.</param>
25+
/// <param name="options">The <see cref="OpenIdAuthenticationOptions"/> used to configure the OpenID 2.0 options.</param>
2626
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
2727
public static IApplicationBuilder UseOpenIdAuthentication(
2828
[NotNull] this IApplicationBuilder app,
@@ -43,10 +43,10 @@ public static IApplicationBuilder UseOpenIdAuthentication(
4343

4444
/// <summary>
4545
/// Adds <see cref="OpenIdAuthenticationMiddleware{TOptions}"/> to the specified
46-
/// <see cref="IApplicationBuilder"/>, which enables OpenID2 authentication capabilities.
46+
/// <see cref="IApplicationBuilder"/>, which enables OpenID 2.0 authentication capabilities.
4747
/// </summary>
4848
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
49-
/// <param name="configuration">The delegate used to configure the OAuth2 options.</param>
49+
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
5050
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
5151
public static IApplicationBuilder UseOpenIdAuthentication(
5252
[NotNull] this IApplicationBuilder app,

src/AspNet.Security.OpenId/OpenIdAuthenticationHandler.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,6 @@ protected override async Task<bool> HandleUnauthorizedAsync(ChallengeContext con
259259
// Generate a new anti-forgery token.
260260
GenerateCorrelationId(properties);
261261

262-
var state = UrlEncoder.Encode(Options.StateDataFormat.Protect(properties));
263-
264262
// Create a new message containing the OpenID 2.0 request parameters.
265263
// See http://openid.net/specs/openid-authentication-2_0.html#requesting_authentication
266264
var message = new OpenIdAuthenticationMessage
@@ -272,7 +270,8 @@ protected override async Task<bool> HandleUnauthorizedAsync(ChallengeContext con
272270
Realm = realm,
273271
ReturnTo = QueryHelpers.AddQueryString(
274272
uri: properties.Items[OpenIdAuthenticationConstants.Properties.ReturnTo],
275-
name: OpenIdAuthenticationConstants.Parameters.State, value: state)
273+
name: OpenIdAuthenticationConstants.Parameters.State,
274+
value: Options.StateDataFormat.Protect(properties))
276275
};
277276

278277
if (Options.Attributes.Count != 0)
@@ -355,8 +354,10 @@ private async Task<bool> VerifyAssertionAsync([NotNull] OpenIdAuthenticationMess
355354
}
356355

357356
// Create a new check_authentication request to verify the assertion.
358-
var request = new HttpRequestMessage(HttpMethod.Post, configuration.AuthenticationEndpoint);
359-
request.Content = new FormUrlEncodedContent(payload);
357+
var request = new HttpRequestMessage(HttpMethod.Post, configuration.AuthenticationEndpoint)
358+
{
359+
Content = new FormUrlEncodedContent(payload)
360+
};
360361

361362
var response = await Options.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
362363
if (!response.IsSuccessStatusCode)

0 commit comments

Comments
 (0)