gpt4 book ai didi

authentication - Blazor 服务器 - 如何配置本地 ADFS 安全性?

转载 作者:行者123 更新时间:2023-12-03 17:32:45 25 4
gpt4 key购买 nike

我有一个解决 .NET Core 3.1 预览版 2 的现有 Blazor(服务器)应用程序。

我需要追溯添加本地 ADFS(不是 Azure)安全性。我一直在努力关注微软的 Authenticate users with WS-Federation in ASP.NET Core它顽固地无视安全。这篇文章当然是为 ASP.NET 而写的,而不是 Blazor...

到目前为止我所做的是:

public static void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

services.AddAuthentication()
.AddWsFederation(options =>
{
options.MetadataAddress = "https://adfs.Server.com/FederationMetadata/2007-06/FederationMetadata.xml";
options.Wtrealm = "https://localhost:44323/";
});

services.AddAuthorization();

services.AddRazorPages();
services.AddServerSideBlazor();

....

关注的一件事 - 数据库中当前包含支持早期身份验证模式(成员身份?)的表(由我们正在重写的应用程序使用)。它有表 [AspNetRoles] [AspNetUserClaims] [AspNetUserLogins] [AspNetUserRoles] 和 [AspNetUsers]。这些会被覆盖吗?
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder != null)
{
if (optionsBuilder.IsConfigured == false)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile($"appsettings.{Startup.CurrentEnvironment}.json")
.Build();

optionsBuilder
.UseSqlServer(configuration.GetConnectionString("MyDatabase"),
providerOptions => providerOptions.CommandTimeout(60));
}
}

base.OnConfiguring(optionsBuilder);
}
}

在 Configure 方法中,我添加了(虽然我不清楚是否需要):
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();

在 App.razor 中,我有:
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
@*<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />*@
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>

在我的一个 Razor 页面 (MyPage.razor) 中,我有:
@page "/myRoute"    
@attribute [Authorize]

当我浏览具有 Autorize 属性的页面时,我收到以下消息:

Not authorized



所以它不会调用我的 ADFS 服务器。它不应该只是自动执行此操作 - 用户不应该单击“登录”按钮。

我引用了以下 NuGet 包:
<PackageReference Include="Microsoft.AspNetCore.Authentication.WsFederation" Version="3.1.0-preview2.19528.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.0-preview2.19528.8" />

我什至遵循微软的例子 Use WS-Federation without ASP.NET Core Identity ,但没有运气。

最佳答案

这是我较旧的帖子,但仍然首先出现在 Google 中,所以..
我或多或少遇到了同样的问题。 OP 在此线程中的帖子帮助了我:
Blazor - Securing using ADFS with local DB repository: how/when to hook into SQL
更具体地说,添加到 Configure() 方法的中间件有所不同。我在我的解决方案中结束了这个:

app.UseAuthentication();
app.UseAuthorization();

app.Use(async (context, next) =>
{
ClaimsPrincipal user = context.User;

if (!user.Identities.Any(x => x.IsAuthenticated))
{
await context.ChallengeAsync(WsFederationDefaults.AuthenticationScheme).ConfigureAwait(false);
}

if (next != null)
{
await next().ConfigureAwait(false);
}
});

关于authentication - Blazor 服务器 - 如何配置本地 ADFS 安全性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58825931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com