Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 3e8cd1e

Browse files
committed
MVC => Auth 2.0
1 parent d65e77e commit 3e8cd1e

File tree

20 files changed

+120
-169
lines changed

20 files changed

+120
-169
lines changed

‎src/Microsoft.AspNetCore.Mvc.Core/Authorization/AuthorizeFilter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Security.Claims;
99
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Authentication;
1011
using Microsoft.AspNetCore.Authorization;
1112
using Microsoft.AspNetCore.Mvc.Core;
1213
using Microsoft.AspNetCore.Mvc.Filters;
@@ -131,10 +132,10 @@ public virtual async Task OnAuthorizationAsync(AuthorizationFilterContext contex
131132
for (var i = 0; i < effectivePolicy.AuthenticationSchemes.Count; i++)
132133
{
133134
var scheme = effectivePolicy.AuthenticationSchemes[i];
134-
var result = await context.HttpContext.Authentication.AuthenticateAsync(scheme);
135-
if (result != null)
135+
var result = await context.HttpContext.AuthenticateAsync(scheme);
136+
if (result.Succeeded)
136137
{
137-
newPrincipal = SecurityHelper.MergeUserPrincipal(newPrincipal, result);
138+
newPrincipal = SecurityHelper.MergeUserPrincipal(newPrincipal, result.Principal);
138139
}
139140
}
140141
// If all schemes failed authentication, provide a default identity anyways

‎src/Microsoft.AspNetCore.Mvc.Core/ChallengeResult.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Threading.Tasks;
7-
using Microsoft.AspNetCore.Http.Authentication;
7+
using Microsoft.AspNetCore.Authentication;
88
using Microsoft.AspNetCore.Mvc.Internal;
99
using Microsoft.Extensions.DependencyInjection;
1010
using Microsoft.Extensions.Logging;
@@ -103,17 +103,16 @@ public override async Task ExecuteResultAsync(ActionContext context)
103103

104104
logger.ChallengeResultExecuting(AuthenticationSchemes);
105105

106-
var authentication = context.HttpContext.Authentication;
107106
if (AuthenticationSchemes != null && AuthenticationSchemes.Count > 0)
108107
{
109108
foreach (var scheme in AuthenticationSchemes)
110109
{
111-
await authentication.ChallengeAsync(scheme, Properties);
110+
await context.HttpContext.ChallengeAsync(scheme, Properties);
112111
}
113112
}
114113
else
115114
{
116-
await authentication.ChallengeAsync(Properties);
115+
await context.HttpContext.ChallengeAsync(Properties);
117116
}
118117
}
119118
}

‎src/Microsoft.AspNetCore.Mvc.Core/ControllerBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
using System.Security.Claims;
99
using System.Text;
1010
using System.Threading.Tasks;
11+
using Microsoft.AspNetCore.Authentication;
1112
using Microsoft.AspNetCore.Http;
12-
using Microsoft.AspNetCore.Http.Authentication;
1313
using Microsoft.AspNetCore.Mvc.Core;
1414
using Microsoft.AspNetCore.Mvc.ModelBinding;
1515
using Microsoft.AspNetCore.Mvc.ModelBinding.Internal;
@@ -1459,7 +1459,7 @@ public virtual AcceptedAtRouteResult AcceptedAtRoute(string routeName, object ro
14591459
/// </summary>
14601460
/// <returns>The created <see cref="ChallengeResult"/> for the response.</returns>
14611461
/// <remarks>
1462-
/// The behavior of this method depends on the <see cref="AuthenticationManager"/> in use.
1462+
/// The behavior of this method depends on the <see cref="IAuthenticationService"/> in use.
14631463
/// <see cref="StatusCodes.Status401Unauthorized"/> and <see cref="StatusCodes.Status403Forbidden"/>
14641464
/// are among likely status results.
14651465
/// </remarks>
@@ -1473,7 +1473,7 @@ public virtual ChallengeResult Challenge()
14731473
/// <param name="authenticationSchemes">The authentication schemes to challenge.</param>
14741474
/// <returns>The created <see cref="ChallengeResult"/> for the response.</returns>
14751475
/// <remarks>
1476-
/// The behavior of this method depends on the <see cref="AuthenticationManager"/> in use.
1476+
/// The behavior of this method depends on the <see cref="IAuthenticationService"/> in use.
14771477
/// <see cref="StatusCodes.Status401Unauthorized"/> and <see cref="StatusCodes.Status403Forbidden"/>
14781478
/// are among likely status results.
14791479
/// </remarks>
@@ -1488,7 +1488,7 @@ public virtual ChallengeResult Challenge(params string[] authenticationSchemes)
14881488
/// challenge.</param>
14891489
/// <returns>The created <see cref="ChallengeResult"/> for the response.</returns>
14901490
/// <remarks>
1491-
/// The behavior of this method depends on the <see cref="AuthenticationManager"/> in use.
1491+
/// The behavior of this method depends on the <see cref="IAuthenticationService"/> in use.
14921492
/// <see cref="StatusCodes.Status401Unauthorized"/> and <see cref="StatusCodes.Status403Forbidden"/>
14931493
/// are among likely status results.
14941494
/// </remarks>
@@ -1505,7 +1505,7 @@ public virtual ChallengeResult Challenge(AuthenticationProperties properties)
15051505
/// <param name="authenticationSchemes">The authentication schemes to challenge.</param>
15061506
/// <returns>The created <see cref="ChallengeResult"/> for the response.</returns>
15071507
/// <remarks>
1508-
/// The behavior of this method depends on the <see cref="AuthenticationManager"/> in use.
1508+
/// The behavior of this method depends on the <see cref="IAuthenticationService"/> in use.
15091509
/// <see cref="StatusCodes.Status401Unauthorized"/> and <see cref="StatusCodes.Status403Forbidden"/>
15101510
/// are among likely status results.
15111511
/// </remarks>

‎src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public static IMvcCoreBuilder AddAuthorization(
9090
// Internal for testing.
9191
internal static void AddAuthorizationServices(IServiceCollection services)
9292
{
93+
services.AddAuthenticationCore();
9394
services.AddAuthorization();
9495

9596
services.TryAddEnumerable(

‎src/Microsoft.AspNetCore.Mvc.Core/ForbidResult.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Threading.Tasks;
7-
using Microsoft.AspNetCore.Http.Authentication;
7+
using Microsoft.AspNetCore.Authentication;
88
using Microsoft.AspNetCore.Mvc.Internal;
99
using Microsoft.Extensions.DependencyInjection;
1010
using Microsoft.Extensions.Logging;
@@ -103,18 +103,16 @@ public override async Task ExecuteResultAsync(ActionContext context)
103103

104104
logger.ForbidResultExecuting(AuthenticationSchemes);
105105

106-
var authentication = context.HttpContext.Authentication;
107-
108106
if (AuthenticationSchemes != null && AuthenticationSchemes.Count > 0)
109107
{
110108
for (var i = 0; i < AuthenticationSchemes.Count; i++)
111109
{
112-
await authentication.ForbidAsync(AuthenticationSchemes[i], Properties);
110+
await context.HttpContext.ForbidAsync(AuthenticationSchemes[i], Properties);
113111
}
114112
}
115113
else
116114
{
117-
await authentication.ForbidAsync(Properties);
115+
await context.HttpContext.ForbidAsync(Properties);
118116
}
119117
}
120118
}

‎src/Microsoft.AspNetCore.Mvc.Core/Microsoft.AspNetCore.Mvc.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Microsoft.AspNetCore.Mvc.RouteAttribute</Description>
2121
<ItemGroup>
2222
<ProjectReference Include="..\Microsoft.AspNetCore.Mvc.Abstractions\Microsoft.AspNetCore.Mvc.Abstractions.csproj" />
2323

24+
<PackageReference Include="Microsoft.AspNetCore.Authentication.Core" Version="$(AspNetCoreVersion)" />
2425
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="$(AspNetCoreVersion)" />
2526
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="$(AspNetCoreVersion)" />
2627
<PackageReference Include="Microsoft.AspNetCore.Http" Version="$(AspNetCoreVersion)" />

‎src/Microsoft.AspNetCore.Mvc.Core/SignInResult.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Security.Claims;
66
using System.Threading.Tasks;
7-
using Microsoft.AspNetCore.Http.Authentication;
7+
using Microsoft.AspNetCore.Authentication;
88
using Microsoft.AspNetCore.Mvc.Core;
99
using Microsoft.AspNetCore.Mvc.Internal;
1010
using Microsoft.Extensions.DependencyInjection;
@@ -88,8 +88,7 @@ public override async Task ExecuteResultAsync(ActionContext context)
8888

8989
logger.SignInResultExecuting(AuthenticationScheme, Principal);
9090

91-
var authentication = context.HttpContext.Authentication;
92-
await authentication.SignInAsync(AuthenticationScheme, Principal, Properties);
91+
await context.HttpContext.SignInAsync(AuthenticationScheme, Principal, Properties);
9392
}
9493
}
9594
}

‎src/Microsoft.AspNetCore.Mvc.Core/SignOutResult.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Threading.Tasks;
7-
using Microsoft.AspNetCore.Http.Authentication;
7+
using Microsoft.AspNetCore.Authentication;
88
using Microsoft.AspNetCore.Mvc.Core;
99
using Microsoft.AspNetCore.Mvc.Internal;
1010
using Microsoft.Extensions.DependencyInjection;
@@ -106,11 +106,9 @@ public override async Task ExecuteResultAsync(ActionContext context)
106106

107107
logger.SignOutResultExecuting(AuthenticationSchemes);
108108

109-
var authentication = context.HttpContext.Authentication;
110-
111109
for (var i = 0; i < AuthenticationSchemes.Count; i++)
112110
{
113-
await authentication.SignOutAsync(AuthenticationSchemes[i], Properties);
111+
await context.HttpContext.SignOutAsync(AuthenticationSchemes[i], Properties);
114112
}
115113
}
116114
}

‎test/Microsoft.AspNetCore.Mvc.Core.Test/Authorization/AuthorizeFilterTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
using System.Linq;
66
using System.Security.Claims;
77
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Authentication;
89
using Microsoft.AspNetCore.Authorization;
910
using Microsoft.AspNetCore.Http;
10-
using Microsoft.AspNetCore.Http.Authentication;
1111
using Microsoft.AspNetCore.Mvc.Abstractions;
1212
using Microsoft.AspNetCore.Mvc.Filters;
1313
using Microsoft.AspNetCore.Routing;
@@ -482,28 +482,28 @@ private AuthorizationFilterContext GetAuthorizationContext(
482482

483483
// ServiceProvider
484484
var serviceCollection = new ServiceCollection();
485+
var auth = new Mock<IAuthenticationService>();
485486
if (registerServices != null)
486487
{
487488
serviceCollection.AddOptions();
488489
serviceCollection.AddLogging();
490+
serviceCollection.AddSingleton(auth.Object);
489491
registerServices(serviceCollection);
490492
}
491493

492494
var serviceProvider = serviceCollection.BuildServiceProvider();
493495

494496
// HttpContext
495497
var httpContext = new Mock<HttpContext>();
496-
var auth = new Mock<AuthenticationManager>();
497-
httpContext.Setup(o => o.Authentication).Returns(auth.Object);
498498
httpContext.SetupProperty(c => c.User);
499499
if (!anonymous)
500500
{
501501
httpContext.Object.User = validUser;
502502
}
503503
httpContext.SetupGet(c => c.RequestServices).Returns(serviceProvider);
504-
auth.Setup(c => c.AuthenticateAsync("Bearer")).ReturnsAsync(bearerPrincipal);
505-
auth.Setup(c => c.AuthenticateAsync("Basic")).ReturnsAsync(basicPrincipal);
506-
auth.Setup(c => c.AuthenticateAsync("Fails")).ReturnsAsync(default(ClaimsPrincipal));
504+
auth.Setup(c => c.AuthenticateAsync(httpContext.Object, "Bearer")).ReturnsAsync(AuthenticateResult.Success(new AuthenticationTicket(bearerPrincipal, "Bearer")));
505+
auth.Setup(c => c.AuthenticateAsync(httpContext.Object, "Basic")).ReturnsAsync(AuthenticateResult.Success(new AuthenticationTicket(basicPrincipal, "Basic")));
506+
auth.Setup(c => c.AuthenticateAsync(httpContext.Object, "Fails")).ReturnsAsync(AuthenticateResult.Fail("Fails"));
507507

508508
// AuthorizationFilterContext
509509
var actionContext = new ActionContext(

‎test/Microsoft.AspNetCore.Mvc.Core.Test/ChallengeResultTest.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Authentication;
56
using Microsoft.AspNetCore.Http;
6-
using Microsoft.AspNetCore.Http.Authentication;
77
using Microsoft.AspNetCore.Mvc.Abstractions;
88
using Microsoft.AspNetCore.Routing;
99
using Microsoft.Extensions.DependencyInjection;
@@ -22,11 +22,11 @@ public async Task ChallengeResult_Execute()
2222
// Arrange
2323
var result = new ChallengeResult("", null);
2424

25-
var httpContext = new Mock<HttpContext>();
26-
httpContext.SetupGet(c => c.RequestServices).Returns(CreateServices().BuildServiceProvider());
25+
var auth = new Mock<IAuthenticationService>();
2726

28-
var auth = new Mock<AuthenticationManager>();
29-
httpContext.Setup(o => o.Authentication).Returns(auth.Object);
27+
var httpContext = new Mock<HttpContext>();
28+
httpContext.SetupGet(c => c.RequestServices)
29+
.Returns(CreateServices().AddSingleton(auth.Object).BuildServiceProvider());
3030

3131
var routeData = new RouteData();
3232
routeData.Routers.Add(Mock.Of<IRouter>());
@@ -39,7 +39,7 @@ public async Task ChallengeResult_Execute()
3939
await result.ExecuteResultAsync(actionContext);
4040

4141
// Assert
42-
auth.Verify(c => c.ChallengeAsync("", null), Times.Exactly(1));
42+
auth.Verify(c => c.ChallengeAsync(httpContext.Object, "", null, ChallengeBehavior.Automatic), Times.Exactly(1));
4343
}
4444

4545
[Fact]
@@ -48,11 +48,10 @@ public async Task ChallengeResult_ExecuteNoSchemes()
4848
// Arrange
4949
var result = new ChallengeResult(new string[] { }, null);
5050

51+
var auth = new Mock<IAuthenticationService>();
5152
var httpContext = new Mock<HttpContext>();
52-
httpContext.SetupGet(c => c.RequestServices).Returns(CreateServices().BuildServiceProvider());
53-
54-
var auth = new Mock<AuthenticationManager>();
55-
httpContext.Setup(o => o.Authentication).Returns(auth.Object);
53+
httpContext.SetupGet(c => c.RequestServices)
54+
.Returns(CreateServices().AddSingleton(auth.Object).BuildServiceProvider());
5655

5756
var routeData = new RouteData();
5857
routeData.Routers.Add(Mock.Of<IRouter>());
@@ -65,13 +64,14 @@ public async Task ChallengeResult_ExecuteNoSchemes()
6564
await result.ExecuteResultAsync(actionContext);
6665

6766
// Assert
68-
auth.Verify(c => c.ChallengeAsync((AuthenticationProperties)null), Times.Exactly(1));
67+
auth.Verify(c => c.ChallengeAsync(httpContext.Object, null, null, ChallengeBehavior.Automatic), Times.Exactly(1));
6968
}
7069

7170
private static IServiceCollection CreateServices()
7271
{
7372
var services = new ServiceCollection();
7473
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
74+
services.AddAuthenticationCore();
7575
return services;
7676
}
7777
}

0 commit comments

Comments
 (0)