Add SignOut overload + Use new auth api (#6476)

This commit is contained in:
Hao Kung 2017-07-03 12:32:11 -07:00 committed by GitHub
parent d917504c14
commit eeee3ef731
7 changed files with 49 additions and 36 deletions

View File

@ -1046,20 +1046,6 @@ namespace Microsoft.AspNetCore.Mvc.Core
internal static string FormatFormatter_NoMediaTypes(object p0, object p1)
=> string.Format(CultureInfo.CurrentCulture, GetString("Formatter_NoMediaTypes"), p0, p1);
/// <summary>
/// At least one authentication scheme must be specified.
/// </summary>
internal static string MustSpecifyAtLeastOneAuthenticationScheme
{
get => GetString("MustSpecifyAtLeastOneAuthenticationScheme");
}
/// <summary>
/// At least one authentication scheme must be specified.
/// </summary>
internal static string FormatMustSpecifyAtLeastOneAuthenticationScheme()
=> GetString("MustSpecifyAtLeastOneAuthenticationScheme");
/// <summary>
/// Could not create a model binder for model object of type '{0}'.
/// </summary>

View File

@ -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>

View File

@ -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.
@ -61,11 +69,6 @@ namespace Microsoft.AspNetCore.Mvc
throw new ArgumentNullException(nameof(authenticationSchemes));
}
if (authenticationSchemes.Count == 0)
{
throw new ArgumentException(Resources.MustSpecifyAtLeastOneAuthenticationScheme, nameof(authenticationSchemes));
}
AuthenticationSchemes = authenticationSchemes;
Properties = properties;
}
@ -96,19 +99,21 @@ namespace Microsoft.AspNetCore.Mvc
/* 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(AuthenticationSchemes[i], Properties);
await context.HttpContext.SignOutAsync(Properties);
}
else
{
for (var i = 0; i < AuthenticationSchemes.Count; i++)
{
await context.HttpContext.SignOutAsync(AuthenticationSchemes[i], Properties);
}
}
}
}

View File

@ -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()
{

View File

@ -10,9 +10,8 @@ namespace RazorPagesWebSite
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddCookieAuthentication(options => options.LoginPath = "/Login")
.AddMvc()
services.AddAuthentication().AddCookie(options => options.LoginPath = "/Login");
services.AddMvc()
.AddCookieTempDataProvider()
.AddRazorPagesOptions(options =>
{

View File

@ -10,9 +10,9 @@ namespace RazorPagesWebSite
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddCookieAuthentication(options => options.LoginPath = "/Login")
.AddMvc()
services.AddAuthentication()
.AddCookie(options => options.LoginPath = "/Login");
services.AddMvc()
.AddCookieTempDataProvider()
.AddRazorPagesOptions(options =>
{

View File

@ -14,7 +14,7 @@ namespace SecurityWebSite
// Add framework services.
services.AddMvc();
services.AddAntiforgery();
services.AddCookieAuthentication(options =>
services.AddAuthentication().AddCookie(options =>
{
options.LoginPath = "/Home/Login";
options.LogoutPath = "/Home/Logout";