parent
2cabd589ac
commit
fe3d45fad1
|
|
@ -36,6 +36,52 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
return options;
|
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>
|
/// <summary>
|
||||||
/// Adds a <see cref="AuthorizeFilter"/> with the specified policy to the page with the specified path.
|
/// Adds a <see cref="AuthorizeFilter"/> with the specified policy to the page with the specified path.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -399,6 +399,22 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
Assert.Equal("/Login?ReturnUrl=%2FHelloWorldWithAuth", response.Headers.Location.PathAndQuery);
|
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]
|
[Fact]
|
||||||
public async Task PageStart_IsDiscoveredWhenRootDirectoryIsNotSpecified()
|
public async Task PageStart_IsDiscoveredWhenRootDirectoryIsNotSpecified()
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,79 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
model => Assert.Same(filter, Assert.Single(model.Filters)));
|
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]
|
[Fact]
|
||||||
public void AuthorizePage_AddsAuthorizeFilterWithPolicyToSpecificPage()
|
public void AuthorizePage_AddsAuthorizeFilterWithPolicyToSpecificPage()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
@page
|
||||||
|
Login Page
|
||||||
|
|
@ -16,6 +16,8 @@ namespace RazorPagesWebSite
|
||||||
.AddRazorPagesOptions(options =>
|
.AddRazorPagesOptions(options =>
|
||||||
{
|
{
|
||||||
options.AuthorizePage("/HelloWorldWithAuth");
|
options.AuthorizePage("/HelloWorldWithAuth");
|
||||||
|
options.AuthorizeFolder("/Pages/Admin");
|
||||||
|
options.AllowAnonymousToPage("/Pages/Admin/Login");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue