ASP.NET Core Razor Pages では、ページレベル(単位)でのアクセス制御は PageModel に対して [Authorize] フィルタを適用することで対応することができます。
[Authorize]
public class IndexModel : PageModel
{
...
}
ただし、このようにページごとにアクセス制御を行うのは手間もかかりがちです。フォルダ単位でアクセス制御を行う場合、AuthorizeFolder メソッドに、制御対象のパスを指定します。
public class Startup
{
...
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddRazorPages()
.AddRazorPagesOptions(options => {
options.Conventions.AuthorizeFolder("/Dashboard");})
.AddRazorRuntimeCompilation();
...
}
...
}
これで、次のような URL へのアクセスを制御することができます。
- https://localhost:44346/Dashboard/Posts
- https://localhost:44346/Dashboard/Posts/100