Adding AllowAnonymousToPage & AllowAnonymousToFolder

Fixes #5884
This commit is contained in:
Hisham Bin Ateya 2017-04-01 06:17:31 +03:00 committed by Pranav K
parent 2cabd589ac
commit fe3d45fad1
5 changed files with 139 additions and 0 deletions

View File

@ -36,6 +36,52 @@ namespace Microsoft.Extensions.DependencyInjection
return options;
}
/// <summary>
/// Adds a <see cref="AllowAnonymousFilter"/> to the page with the specified path.
/// </summary>
/// <param name="options">The <see cref="RazorPagesOptions"/> to configure.</param>
/// <param name="path">The path of the Razor Page.</param>
/// <returns>The <see cref="RazorPagesOptions"/>.</returns>
public static RazorPagesOptions AllowAnonymousToPage(this RazorPagesOptions options, string path)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(path));
}
var anonymousFilter = new AllowAnonymousFilter();
options.Conventions.Add(new PageConvention(path, model => model.Filters.Add(anonymousFilter)));
return options;
}
/// <summary>
/// Adds a <see cref="AllowAnonymousFilter"/> to all pages under the specified path.
/// </summary>
/// <param name="options">The <see cref="RazorPagesOptions"/> to configure.</param>
/// <param name="folderPath">The folder path.</param>
/// <returns>The <see cref="RazorPagesOptions"/>.</returns>
public static RazorPagesOptions AllowAnonymousToFolder(this RazorPagesOptions options, string folderPath)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (string.IsNullOrEmpty(folderPath))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(folderPath));
}
var anonymousFilter = new AllowAnonymousFilter();
options.Conventions.Add(new FolderConvention(folderPath, model => model.Filters.Add(anonymousFilter)));
return options;
}
/// <summary>
/// Adds a <see cref="AuthorizeFilter"/> with the specified policy to the page with the specified path.
/// </summary>

View File

@ -399,6 +399,22 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
Assert.Equal("/Login?ReturnUrl=%2FHelloWorldWithAuth", response.Headers.Location.PathAndQuery);
}
[Fact]
public async Task AuthorizePage_AllowAnonymousForSpecificPages()
{
// Arrange
var url = "/Pages/Admin/Login";
// Act
var response = await Client.GetAsync(url);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.Equal("Login Page", content);
}
[Fact]
public async Task PageStart_IsDiscoveredWhenRootDirectoryIsNotSpecified()

View File

@ -37,6 +37,79 @@ namespace Microsoft.Extensions.DependencyInjection
model => Assert.Same(filter, Assert.Single(model.Filters)));
}
[Fact]
public void AuthorizePage_AddsAllowAnonymousFilterToSpecificPage()
{
// Arrange
var options = new RazorPagesOptions();
var models = new[]
{
new PageApplicationModel("/Pages/Index.cshtml", "/Index.cshtml"),
new PageApplicationModel("/Pages/Users/Account.cshtml", "/Users/Account.cshtml"),
new PageApplicationModel("/Pages/Users/Contact.cshtml", "/Users/Contact.cshtml"),
};
// Act
options.AuthorizeFolder("/Users");
options.AllowAnonymousToPage("/Users/Contact.cshtml");
ApplyConventions(options, models);
// Assert
Assert.Collection(models,
model => Assert.Empty(model.Filters),
model =>
{
Assert.Equal("/Users/Account.cshtml", model.ViewEnginePath);
Assert.IsType<AuthorizeFilter>(Assert.Single(model.Filters));
},
model =>
{
Assert.Equal("/Users/Contact.cshtml", model.ViewEnginePath);
Assert.IsType<AuthorizeFilter>(model.Filters[0]);
Assert.IsType<AllowAnonymousFilter>(model.Filters[1]);
});
}
[Theory]
[InlineData("/Users")]
[InlineData("/Users/")]
public void AuthorizePage_AddsAllowAnonymousFilterToPagesUnderFolder(string folderName)
{
// Arrange
var options = new RazorPagesOptions();
var models = new[]
{
new PageApplicationModel("/Pages/Index.cshtml", "/Index.cshtml"),
new PageApplicationModel("/Pages/Users/Account.cshtml", "/Users/Account.cshtml"),
new PageApplicationModel("/Pages/Users/Contact.cshtml", "/Users/Contact.cshtml"),
};
// Act
options.AuthorizeFolder("/");
options.AllowAnonymousToFolder("/Users");
ApplyConventions(options, models);
// Assert
Assert.Collection(models,
model =>
{
Assert.Equal("/Index.cshtml", model.ViewEnginePath);
Assert.IsType<AuthorizeFilter>(Assert.Single(model.Filters));
},
model =>
{
Assert.Equal("/Users/Account.cshtml", model.ViewEnginePath);
Assert.IsType<AuthorizeFilter>(model.Filters[0]);
Assert.IsType<AllowAnonymousFilter>(model.Filters[1]);
},
model =>
{
Assert.Equal("/Users/Contact.cshtml", model.ViewEnginePath);
Assert.IsType<AuthorizeFilter>(model.Filters[0]);
Assert.IsType<AllowAnonymousFilter>(model.Filters[1]);
});
}
[Fact]
public void AuthorizePage_AddsAuthorizeFilterWithPolicyToSpecificPage()
{

View File

@ -0,0 +1,2 @@
@page
Login Page

View File

@ -16,6 +16,8 @@ namespace RazorPagesWebSite
.AddRazorPagesOptions(options =>
{
options.AuthorizePage("/HelloWorldWithAuth");
options.AuthorizeFolder("/Pages/Admin");
options.AllowAnonymousToPage("/Pages/Admin/Login");
});
}