AllowAnonymous for external login providers and confirm email (#1769)

Addresses #1762
This commit is contained in:
Jass Bagga 2018-05-01 12:30:46 -07:00 committed by GitHub
parent 5cf691e35b
commit a89dc30d43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 151 additions and 4 deletions

View File

@ -3,11 +3,13 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Microsoft.AspNetCore.Identity.UI.Pages.Account.Internal
{
[AllowAnonymous]
[IdentityDefaultUI(typeof(ConfirmEmailModel<>))]
public abstract class ConfirmEmailModel : PageModel
{

View File

@ -6,12 +6,14 @@ using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Identity.UI.Pages.Account.Internal
{
[AllowAnonymous]
[IdentityDefaultUI(typeof(ExternalLoginModel<>))]
public class ExternalLoginModel : PageModel
{

View File

@ -8,7 +8,6 @@ using Identity.DefaultUI.WebSite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
namespace Microsoft.AspNetCore.Identity.FunctionalTests
@ -41,6 +40,29 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
await UserStories.LoginExistingUserAsync(newClient, userName, password);
}
[Fact]
public async Task CanLogInWithAPreviouslyRegisteredUser_WithGlobalAuthorizeFilter()
{
// Arrange
void ConfigureTestServices(IServiceCollection services) =>
services.SetupGlobalAuthorizeFilter();
var server = ServerFactory
.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));
var client = server.CreateClient();
var newClient = server.CreateClient();
var userName = $"{Guid.NewGuid()}@example.com";
var password = $"!Test.Password1$";
// Act & Assert
await UserStories.RegisterNewUserAsync(client, userName, password);
// Use a new client to simulate a new browser session.
await UserStories.LoginExistingUserAsync(newClient, userName, password);
}
[Fact]
public async Task CanLogInWithTwoFactorAuthentication()
{
@ -65,8 +87,14 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
public async Task CanLogInWithTwoFactorAuthentication_WithGlobalAuthorizeFilter()
{
// Arrange
var client = ServerFactory.CreateClient();
var newClient = ServerFactory.CreateClient();
void ConfigureTestServices(IServiceCollection services) =>
services.SetupGlobalAuthorizeFilter();
var server = ServerFactory
.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));
var client = server.CreateClient();
var newClient = server.CreateClient();
var userName = $"{Guid.NewGuid()}@example.com";
var password = $"!Test.Password1$";
@ -130,7 +158,6 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
public async Task CannotLogInWithoutRequiredEmailConfirmation()
{
// Arrange
var emailSender = new ContosoEmailSender();
void ConfigureTestServices(IServiceCollection services) => services
.SetupTestEmailSender(emailSender)
@ -151,6 +178,31 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
await Assert.ThrowsAnyAsync<XunitException>(() => UserStories.LoginExistingUserAsync(newClient, userName, password));
}
[Fact]
public async Task CannotLogInWithoutRequiredEmailConfirmation_WithGlobalAuthorizeFilter()
{
// Arrange
var emailSender = new ContosoEmailSender();
void ConfigureTestServices(IServiceCollection services) => services
.SetupTestEmailSender(emailSender)
.SetupEmailRequired()
.SetupGlobalAuthorizeFilter();
var server = ServerFactory.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));
var client = server.CreateClient();
var newClient = server.CreateClient();
var userName = $"{Guid.NewGuid()}@example.com";
var password = $"!Test.Password1$";
var loggedIn = await UserStories.RegisterNewUserAsync(client, userName, password);
// Act & Assert
// Use a new client to simulate a new browser session.
await Assert.ThrowsAnyAsync<XunitException>(() => UserStories.LoginExistingUserAsync(newClient, userName, password));
}
[Fact]
public async Task CanLogInAfterConfirmingEmail()
{
@ -178,6 +230,34 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
await UserStories.LoginExistingUserAsync(newClient, userName, password);
}
[Fact]
public async Task CanLogInAfterConfirmingEmail_WithGlobalAuthorizeFilter()
{
// Arrange
var emailSender = new ContosoEmailSender();
void ConfigureTestServices(IServiceCollection services) => services
.SetupTestEmailSender(emailSender)
.SetupEmailRequired()
.SetupGlobalAuthorizeFilter();
var server = ServerFactory.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));
var client = server.CreateClient();
var newClient = server.CreateClient();
var userName = $"{Guid.NewGuid()}@example.com";
var password = $"!Test.Password1$";
var loggedIn = await UserStories.RegisterNewUserAsync(client, userName, password);
// Act & Assert
// Use a new client to simulate a new browser session.
var email = Assert.Single(emailSender.SentEmails);
await UserStories.ConfirmEmailAsync(email, newClient);
await UserStories.LoginExistingUserAsync(newClient, userName, password);
}
[Fact]
public async Task CanLoginWithASocialLoginProvider()
{
@ -199,6 +279,28 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
await UserStories.LoginWithSocialLoginAsync(newClient, userName);
}
[Fact]
public async Task CanLoginWithASocialLoginProvider_WithGlobalAuthorizeFilter()
{
// Arrange
void ConfigureTestServices(IServiceCollection services) => services
.SetupTestThirdPartyLogin()
.SetupGlobalAuthorizeFilter();
var server = ServerFactory.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));
var client = server.CreateClient();
var newClient = server.CreateClient();
var guid = Guid.NewGuid();
var userName = $"{guid}";
var email = $"{guid}@example.com";
// Act & Assert
await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email);
await UserStories.LoginWithSocialLoginAsync(newClient, userName);
}
[Fact]
public async Task CanLogInAfterResettingThePassword()
{

View File

@ -37,6 +37,24 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
await UserStories.RegisterNewUserAsync(client, userName, password);
}
[Fact]
public async Task CanRegisterAUser_WithGlobalAuthorizeFilter()
{
// Arrange
void ConfigureTestServices(IServiceCollection services) =>
services.SetupGlobalAuthorizeFilter();
var client = ServerFactory
.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices))
.CreateClient();
var userName = $"{Guid.NewGuid()}@example.com";
var password = $"!Test.Password1$";
// Act & Assert
await UserStories.RegisterNewUserAsync(client, userName, password);
}
[Fact]
public async Task CanRegisterWithASocialLoginProvider()
{
@ -56,5 +74,26 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
// Act & Assert
await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email);
}
[Fact]
public async Task CanRegisterWithASocialLoginProvider_WithGlobalAuthorizeFilter()
{
// Arrange
void ConfigureTestServices(IServiceCollection services) =>
services
.SetupTestThirdPartyLogin()
.SetupGlobalAuthorizeFilter();
var client = ServerFactory
.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices))
.CreateClient();
var guid = Guid.NewGuid();
var userName = $"{guid}";
var email = $"{guid}@example.com";
// Act & Assert
await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email);
}
}
}

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Options;
@ -13,6 +14,7 @@ using System.Threading.Tasks;
namespace Identity.DefaultUI.WebSite.Pages
{
[AllowAnonymous]
public class LoginModel : PageModel
{
public LoginModel(IOptionsMonitor<ContosoAuthenticationOptions> options)