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

标签 authentication asp.net-core authorization adfs blazor-server-side

我有一个解决 .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/

相关文章:

c# - 从 MVC 客户端应用程序和移动应用程序保护对 .NET Web API 的访问

php - 关于 session 的登录脚本中未在 php 中设置错误

asp.net-core - .NET Core + Openiddict InvalidCastException

c# - 如何使用单页保护 Web API

.net - 如何防止用户使用 dotnet core 和 RESTful API 访问其他用户的数据?

session - CakePHP 保持从主域到子域的 session

c# - 使用 DotNetOpenAuth 实现 API key

asp.net - GraphQL.NET 如何将枚举添加到 GraphQL 类型

c# - 尚未注册类型 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' 的服务

.net - 具有多个身份验证过滤器的 ASP.net Web API 2 Controller