Server Side Blazor (Preview 6) - is it possible to require authentication for a particular page?

血红的双手。 提交于 2019-12-11 09:53:43

问题


For a Blazor server hosted page - is it possible to require authentication? Based on the following documentation:

https://docs.microsoft.com/en-au/aspnet/core/security/authorization/policies?view=aspnetcore-3.0

I've added the following to my Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc().SetCompatibilityVersion( CompatibilityVersion.Version_2_2 );

  services.AddAuthorization(options =>
  {
      options.AddPolicy("Private", policy => policy.RequireAuthenticatedUser() );
  });
}

And here's what I've put at the top of the fetchdata page:

@page "/fetchdata"
@using HollyTest.Data
@inject WeatherForecastService ForecastService
@attribute [Authorize(Policy="Private")]

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from a service.</p>

Can someone tell me if I'm on the right track?

来源:https://stackoverflow.com/questions/56964793/server-side-blazor-preview-6-is-it-possible-to-require-authentication-for-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!