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

Commit

Permalink
Add SignOut overload + Use new auth api (#6476)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK authored Jul 3, 2017
1 parent d917504 commit eeee3ef
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 36 deletions.
14 changes: 0 additions & 14 deletions src/Microsoft.AspNetCore.Mvc.Core/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src/Microsoft.AspNetCore.Mvc.Core/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,6 @@
<data name="Formatter_NoMediaTypes" xml:space="preserve">
<value>No media types found in '{0}.{1}'. Add at least one media type to the list of supported media types.</value>
</data>
<data name="MustSpecifyAtLeastOneAuthenticationScheme" xml:space="preserve">
<value>At least one authentication scheme must be specified.</value>
</data>
<data name="CouldNotCreateIModelBinder" xml:space="preserve">
<value>Could not create a model binder for model object of type '{0}'.</value>
</data>
Expand Down
29 changes: 17 additions & 12 deletions src/Microsoft.AspNetCore.Mvc.Core/SignOutResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ namespace Microsoft.AspNetCore.Mvc
/// </summary>
public class SignOutResult : ActionResult
{
/// <summary>
/// Initializes a new instance of <see cref="SignOutResult"/> with the default sign out scheme.
/// </summary>
public SignOutResult()
: this(Array.Empty<string>())
{
}

/// <summary>
/// Initializes a new instance of <see cref="SignOutResult"/> with the
/// specified authentication scheme.
Expand Down Expand Up @@ -61,11 +69,6 @@ public SignOutResult(IList<string> authenticationSchemes, AuthenticationProperti
throw new ArgumentNullException(nameof(authenticationSchemes));
}

if (authenticationSchemes.Count == 0)
{
throw new ArgumentException(Resources.MustSpecifyAtLeastOneAuthenticationScheme, nameof(authenticationSchemes));
}

AuthenticationSchemes = authenticationSchemes;
Properties = properties;
}
Expand Down Expand Up @@ -96,19 +99,21 @@ public override async Task ExecuteResultAsync(ActionContext context)
/* type: */ nameof(SignOutResult)));
}

if (AuthenticationSchemes.Count == 0)
{
throw new ArgumentException(Resources.MustSpecifyAtLeastOneAuthenticationScheme, nameof(AuthenticationSchemes));
}

var loggerFactory = context.HttpContext.RequestServices.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<SignOutResult>();

logger.SignOutResultExecuting(AuthenticationSchemes);

for (var i = 0; i < AuthenticationSchemes.Count; i++)
if (AuthenticationSchemes.Count == 0)
{
await context.HttpContext.SignOutAsync(Properties);
}
else
{
await context.HttpContext.SignOutAsync(AuthenticationSchemes[i], Properties);
for (var i = 0; i < AuthenticationSchemes.Count; i++)
{
await context.HttpContext.SignOutAsync(AuthenticationSchemes[i], Properties);
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/Microsoft.AspNetCore.Mvc.Core.Test/SignOutResultTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ namespace Microsoft.AspNetCore.Mvc
{
public class SignOutResultTest
{
[Fact]
public async Task ExecuteResultAsync_NoArgsInvokesDefaultSignOut()
{
// Arrange
var httpContext = new Mock<HttpContext>();
var auth = new Mock<IAuthenticationService>();
auth
.Setup(c => c.SignOutAsync(httpContext.Object, null, null))
.Returns(Task.CompletedTask)
.Verifiable();
httpContext.Setup(c => c.RequestServices).Returns(CreateServices(auth.Object));
var result = new SignOutResult();
var routeData = new RouteData();

var actionContext = new ActionContext(
httpContext.Object,
routeData,
new ActionDescriptor());

// Act
await result.ExecuteResultAsync(actionContext);

// Assert
auth.Verify();
}

[Fact]
public async Task ExecuteResultAsync_InvokesSignOutAsyncOnAuthenticationManager()
{
Expand Down
5 changes: 2 additions & 3 deletions test/WebSites/RazorPagesWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddCookieAuthentication(options => options.LoginPath = "/Login")
.AddMvc()
services.AddAuthentication().AddCookie(options => options.LoginPath = "/Login");
services.AddMvc()
.AddCookieTempDataProvider()
.AddRazorPagesOptions(options =>
{
Expand Down
6 changes: 3 additions & 3 deletions test/WebSites/RazorPagesWebSite/StartupWithBasePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class StartupWithBasePath
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddCookieAuthentication(options => options.LoginPath = "/Login")
.AddMvc()
services.AddAuthentication()
.AddCookie(options => options.LoginPath = "/Login");
services.AddMvc()
.AddCookieTempDataProvider()
.AddRazorPagesOptions(options =>
{
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/SecurityWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void ConfigureServices(IServiceCollection services)
// Add framework services.
services.AddMvc();
services.AddAntiforgery();
services.AddCookieAuthentication(options =>
services.AddAuthentication().AddCookie(options =>
{
options.LoginPath = "/Home/Login";
options.LogoutPath = "/Home/Logout";
Expand Down

0 comments on commit eeee3ef

Please sign in to comment.