简体   繁体   English

Blazor 服务器端 AllowAnonymousToFolder

[英]Blazor Server Side AllowAnonymousToFolder

I have added Azure AD authentication so that all requests to the application has to login.我添加了 Azure AD 身份验证,以便对应用程序的所有请求都必须登录。 But I would like to seperate this since I have a "Public" and "Private" folder for my pages.但我想将其分开,因为我的页面有一个“公共”和“私人”文件夹。

I found this https://docs.microsoft.com/en-us/aspnet/core/security/authorization/razor-pages-authorization?view=aspnetcore-5.0 which is what I need, but once I go to a page in the public folder, I still get prompted to login.我发现这个https://docs.microsoft.com/en-us/aspnet/core/security/authorization/razor-pages-authorization?view=aspnetcore-5.0这是我需要的,但是一旦我将 go 转到一个页面公用文件夹,我仍然被提示登录。

Expected result: All the pages in the /Pages/Public folder will not prompt login.预期结果:/Pages/Public 文件夹中的所有页面都不会提示登录。

Result: When navigationg to a page in the /Pages/Public folder Im prompted with a login request.结果:当导航到 /Pages/Public 文件夹中的页面时,我会提示登录请求。

Im relatively new at Server side Blazor and Net Core so its very possible that Im missunderstanding something or that this functionality isnt available for Blazor.我在服务器端 Blazor 和 Net Core 相对较新,所以我很可能误解了某些东西,或者 Blazor 不提供此功能。 If so then might I get a hint to what to look at instead?如果是这样,那么我可能会提示要查看的内容吗?

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
      Configuration.Bind("AzureAD", options);
      options.Events ??= new OpenIdConnectEvents();
      options.Events.OnTokenValidated += OnTokenValidatedFunc;
});
services.AddRazorPages(options =>
{
     //No error here but no effect either
     options.Conventions.AllowAnonymousToFolder("/Pages/Public");
}).AddMvcOptions(options =>
{
     var policy = new AuthorizationPolicyBuilder()
                      .RequireAuthenticatedUser()
                      .Build();
     options.Filters.Add(new AuthorizeFilter(policy));
                
}).AddMicrosoftIdentityUI();

Use @attribute [Authorize(Policy = "Whatever")] in a new _Imports.razor in the folder with all the pages which require Auth.在包含所有需要身份验证的页面的文件夹中的新_Imports.razor中使用@attribute [Authorize(Policy = "Whatever")] This will add the Authorize attribute to all pages in that folder and below.这会将 Authorize 属性添加到该文件夹及以下文件夹中的所有页面。 You will have to remove your global Auth.您将不得不删除您的全局身份验证。 requirement though.虽然要求。

So what I ended up with was splitting the project into two projects, with one part secured and the other public since I didnt find a way to have the whole project secure and only anonymize certain pages.所以我最终将项目分成两个项目,一个部分是安全的,另一个是公开的,因为我没有找到一种方法来保证整个项目的安全并且只匿名某些页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM