Skip to content

Commit db3bdff

Browse files
kinosangkevinchalet
authored andcommitted
Migrate the VisualStudio provider to ASP.NET Core 2.0
1 parent 63f2543 commit db3bdff

8 files changed

+75
-174
lines changed

samples/Mvc.Client/Mvc.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Twitch\AspNet.Security.OAuth.Twitch.csproj" />
4242
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Untappd\AspNet.Security.OAuth.Untappd.csproj" />
4343
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Vimeo\AspNet.Security.OAuth.Vimeo.csproj" />
44+
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.VisualStudio\AspNet.Security.OAuth.VisualStudio.csproj" />
4445
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Vkontakte\AspNet.Security.OAuth.Vkontakte.csproj" />
4546
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Weibo\AspNet.Security.OAuth.Weibo.csproj" />
4647
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Weixin\AspNet.Security.OAuth.Weixin.csproj" />

src/AspNet.Security.OAuth.VisualStudio/AspNet.Security.OAuth.VisualStudio.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Import Project="..\..\build\packages.props" />
44

55
<PropertyGroup>
6-
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
6+
<TargetFramework>netstandard2.0</TargetFramework>
77
</PropertyGroup>
88

99
<PropertyGroup>
@@ -12,10 +12,6 @@
1212
<PackageTags>aspnetcore;authentication;oauth;security;visualstudio</PackageTags>
1313
</PropertyGroup>
1414

15-
<ItemGroup>
16-
<Compile Include="..\..\shared\AspNet.Security.OAuth.Extensions\*.cs" />
17-
</ItemGroup>
18-
1915
<ItemGroup>
2016
<PackageReference Include="JetBrains.Annotations" Version="$(JetBrainsVersion)" PrivateAssets="All" />
2117
<PackageReference Include="Microsoft.AspNetCore.Authentication.OAuth" Version="$(AspNetCoreVersion)" />

src/AspNet.Security.OAuth.VisualStudio/VisualStudioAuthenticationDefaults.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* for more information concerning the license and the contributors participating to this project.
55
*/
66

7-
using Microsoft.AspNetCore.Builder;
7+
using Microsoft.AspNetCore.Authentication;
8+
using Microsoft.AspNetCore.Authentication.OAuth;
89

910
namespace AspNet.Security.OAuth.VisualStudio
1011
{
@@ -14,17 +15,17 @@ namespace AspNet.Security.OAuth.VisualStudio
1415
public static class VisualStudioAuthenticationDefaults
1516
{
1617
/// <summary>
17-
/// Default value for <see cref="AuthenticationOptions.AuthenticationScheme"/>.
18+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
1819
/// </summary>
1920
public const string AuthenticationScheme = "Visual Studio Online";
2021

2122
/// <summary>
22-
/// Default value for <see cref="RemoteAuthenticationOptions.DisplayName"/>.
23+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
2324
/// </summary>
2425
public const string DisplayName = "Visual Studio Online";
2526

2627
/// <summary>
27-
/// Default value for <see cref="AuthenticationOptions.ClaimsIssuer"/>.
28+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
2829
/// </summary>
2930
public const string Issuer = "Visual Studio Online";
3031

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,76 @@
11
/*
22
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3-
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
3+
* See https://VisualStudio.com/aspnet-contrib/AspNet.Security.OAuth.Providers
44
* for more information concerning the license and the contributors participating to this project.
55
*/
66

77
using System;
88
using AspNet.Security.OAuth.VisualStudio;
99
using JetBrains.Annotations;
10-
using Microsoft.Extensions.Options;
10+
using Microsoft.AspNetCore.Authentication;
1111

12-
namespace Microsoft.AspNetCore.Builder
12+
namespace Microsoft.Extensions.DependencyInjection
1313
{
1414
/// <summary>
15-
/// Extension methods to add VisualStudio authentication capabilities to an HTTP application pipeline.
15+
/// Extension methods to add authentication capabilities to an HTTP application pipeline.
1616
/// </summary>
1717
public static class VisualStudioAuthenticationExtensions
1818
{
1919
/// <summary>
20-
/// Adds the <see cref="VisualStudioAuthenticationMiddleware"/> middleware to the specified
21-
/// <see cref="IApplicationBuilder"/>, which enables VisualStudio authentication capabilities.
20+
/// Adds <see cref="VisualStudioAuthenticationHandler"/> to the specified
21+
/// <see cref="AuthenticationBuilder"/>, which enables VisualStudio authentication capabilities.
2222
/// </summary>
23-
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
24-
/// <param name="options">A <see cref="VisualStudioAuthenticationOptions"/> that specifies options for the middleware.</param>
25-
/// <returns>A reference to this instance after the operation has completed.</returns>
26-
public static IApplicationBuilder UseVisualStudioAuthentication(
27-
[NotNull] this IApplicationBuilder app,
28-
[NotNull] VisualStudioAuthenticationOptions options)
23+
/// <param name="builder">The authentication builder.</param>
24+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
25+
public static AuthenticationBuilder AddVisualStudio([NotNull] this AuthenticationBuilder builder)
2926
{
30-
if (app == null)
31-
{
32-
throw new ArgumentNullException(nameof(app));
33-
}
34-
35-
if (options == null)
36-
{
37-
throw new ArgumentNullException(nameof(options));
38-
}
39-
40-
return app.UseMiddleware<VisualStudioAuthenticationMiddleware>(Options.Create(options));
27+
return builder.AddVisualStudio(VisualStudioAuthenticationDefaults.AuthenticationScheme, options => { });
4128
}
4229

4330
/// <summary>
44-
/// Adds the <see cref="VisualStudioAuthenticationMiddleware"/> middleware to the specified
45-
/// <see cref="IApplicationBuilder"/>, which enables VisualStudio authentication capabilities.
31+
/// Adds <see cref="VisualStudioAuthenticationHandler"/> to the specified
32+
/// <see cref="AuthenticationBuilder"/>, which enables VisualStudio authentication capabilities.
4633
/// </summary>
47-
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
48-
/// <param name="configuration">An action delegate to configure the provided <see cref="VisualStudioAuthenticationOptions"/>.</param>
49-
/// <returns>A reference to this instance after the operation has completed.</returns>
50-
public static IApplicationBuilder UseVisualStudioAuthentication(
51-
[NotNull] this IApplicationBuilder app,
34+
/// <param name="builder">The authentication builder.</param>
35+
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
36+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
37+
public static AuthenticationBuilder AddVisualStudio(
38+
[NotNull] this AuthenticationBuilder builder,
5239
[NotNull] Action<VisualStudioAuthenticationOptions> configuration)
5340
{
54-
if (app == null)
55-
{
56-
throw new ArgumentNullException(nameof(app));
57-
}
58-
59-
if (configuration == null)
60-
{
61-
throw new ArgumentNullException(nameof(configuration));
62-
}
41+
return builder.AddVisualStudio(VisualStudioAuthenticationDefaults.AuthenticationScheme, configuration);
42+
}
6343

64-
var options = new VisualStudioAuthenticationOptions();
65-
configuration(options);
44+
/// <summary>
45+
/// Adds <see cref="VisualStudioAuthenticationHandler"/> to the specified
46+
/// <see cref="AuthenticationBuilder"/>, which enables VisualStudio authentication capabilities.
47+
/// </summary>
48+
/// <param name="builder">The authentication builder.</param>
49+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
50+
/// <param name="configuration">The delegate used to configure the VisualStudio options.</param>
51+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
52+
public static AuthenticationBuilder AddVisualStudio(
53+
[NotNull] this AuthenticationBuilder builder, [NotNull] string scheme,
54+
[NotNull] Action<VisualStudioAuthenticationOptions> configuration)
55+
{
56+
return builder.AddVisualStudio(scheme, VisualStudioAuthenticationDefaults.DisplayName, configuration);
57+
}
6658

67-
return app.UseMiddleware<VisualStudioAuthenticationMiddleware>(Options.Create(options));
59+
/// <summary>
60+
/// Adds <see cref="VisualStudioAuthenticationHandler"/> to the specified
61+
/// <see cref="AuthenticationBuilder"/>, which enables VisualStudio authentication capabilities.
62+
/// </summary>
63+
/// <param name="builder">The authentication builder.</param>
64+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
65+
/// <param name="name">The optional display name associated with this instance.</param>
66+
/// <param name="configuration">The delegate used to configure the VisualStudio options.</param>
67+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
68+
public static AuthenticationBuilder AddVisualStudio(
69+
[NotNull] this AuthenticationBuilder builder,
70+
[NotNull] string scheme, [CanBeNull] string name,
71+
[NotNull] Action<VisualStudioAuthenticationOptions> configuration)
72+
{
73+
return builder.AddOAuth<VisualStudioAuthenticationOptions, VisualStudioAuthenticationHandler>(scheme, name, configuration);
6874
}
6975
}
70-
}
76+
}

src/AspNet.Security.OAuth.VisualStudio/VisualStudioAuthenticationHandler.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,26 @@
99
using System.Net.Http;
1010
using System.Net.Http.Headers;
1111
using System.Security.Claims;
12+
using System.Text.Encodings.Web;
1213
using System.Threading.Tasks;
13-
using AspNet.Security.OAuth.Extensions;
1414
using JetBrains.Annotations;
1515
using Microsoft.AspNetCore.Authentication;
1616
using Microsoft.AspNetCore.Authentication.OAuth;
17-
using Microsoft.AspNetCore.Http.Authentication;
1817
using Microsoft.AspNetCore.WebUtilities;
1918
using Microsoft.Extensions.Logging;
19+
using Microsoft.Extensions.Options;
2020
using Newtonsoft.Json.Linq;
2121

2222
namespace AspNet.Security.OAuth.VisualStudio
2323
{
2424
public class VisualStudioAuthenticationHandler : OAuthHandler<VisualStudioAuthenticationOptions>
2525
{
26-
public VisualStudioAuthenticationHandler([NotNull] HttpClient client)
27-
: base(client)
26+
public VisualStudioAuthenticationHandler(
27+
[NotNull] IOptionsMonitor<VisualStudioAuthenticationOptions> options,
28+
[NotNull] ILoggerFactory logger,
29+
[NotNull] UrlEncoder encoder,
30+
[NotNull] ISystemClock clock)
31+
: base(options, logger, encoder, clock)
2832
{
2933
}
3034

@@ -50,17 +54,12 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]
5054
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
5155

5256
var principal = new ClaimsPrincipal(identity);
53-
var ticket = new AuthenticationTicket(principal, properties, Options.AuthenticationScheme);
57+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload);
58+
context.RunClaimActions(payload);
5459

55-
identity.AddOptionalClaim(ClaimTypes.NameIdentifier, VisualStudioAuthenticationHelper.GetIdentifier(payload), Options.ClaimsIssuer)
56-
.AddOptionalClaim(ClaimTypes.Email, VisualStudioAuthenticationHelper.GetEmail(payload), Options.ClaimsIssuer)
57-
.AddOptionalClaim(ClaimTypes.Name, VisualStudioAuthenticationHelper.GetLogin(payload), Options.ClaimsIssuer)
58-
.AddOptionalClaim(ClaimTypes.GivenName, VisualStudioAuthenticationHelper.GetName(payload), Options.ClaimsIssuer);
59-
60-
var context = new OAuthCreatingTicketContext(ticket, Context, Options, Backchannel, tokens, payload);
6160
await Options.Events.CreatingTicket(context);
6261

63-
return context.Ticket;
62+
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
6463
}
6564

6665
protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri)

src/AspNet.Security.OAuth.VisualStudio/VisualStudioAuthenticationHelper.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/AspNet.Security.OAuth.VisualStudio/VisualStudioAuthenticationMiddleware.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/AspNet.Security.OAuth.VisualStudio/VisualStudioAuthenticationOptions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* for more information concerning the license and the contributors participating to this project.
55
*/
66

7-
using Microsoft.AspNetCore.Builder;
7+
using System.Security.Claims;
8+
using Microsoft.AspNetCore.Authentication;
9+
using Microsoft.AspNetCore.Authentication.OAuth;
810
using Microsoft.AspNetCore.Http;
911

1012
namespace AspNet.Security.OAuth.VisualStudio
@@ -16,15 +18,18 @@ public class VisualStudioAuthenticationOptions : OAuthOptions
1618
{
1719
public VisualStudioAuthenticationOptions()
1820
{
19-
AuthenticationScheme = VisualStudioAuthenticationDefaults.AuthenticationScheme;
20-
DisplayName = VisualStudioAuthenticationDefaults.DisplayName;
2121
ClaimsIssuer = VisualStudioAuthenticationDefaults.Issuer;
2222

2323
CallbackPath = new PathString(VisualStudioAuthenticationDefaults.CallbackPath);
2424

2525
AuthorizationEndpoint = VisualStudioAuthenticationDefaults.AuthorizationEndpoint;
2626
TokenEndpoint = VisualStudioAuthenticationDefaults.TokenEndpoint;
2727
UserInformationEndpoint = VisualStudioAuthenticationDefaults.UserInformationEndpoint;
28+
29+
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
30+
ClaimActions.MapJsonKey(ClaimTypes.Email, "emailAddress");
31+
ClaimActions.MapJsonKey(ClaimTypes.Name, "publicAlias");
32+
ClaimActions.MapJsonKey(ClaimTypes.GivenName, "displayName");
2833
}
2934
}
3035
}

0 commit comments

Comments
 (0)