首页 > 解决方案 > Blazor - 使用带有本地 DB 存储库的 ADFS 保护:如何/何时挂钩 SQL

问题描述

我有一个 Blazer Server 应用程序,它现在使用来自本地 ADFS 服务器的身份验证。确定用户后,我现在需要加载他们的权限。我们认为这不能通过 ADFS 服务器的声明来提供因此希望在数据库中进行配置,但需要了解如何/何时获取此信息。

关于挂钩到 ADFS,我的代码如下(欢迎提出任何改进建议)

App.razor

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(Program).Assembly">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                <NotAuthorized>
                    <h1>Sorry</h1>
                    <p>You're not authorized to reach this page.</p>
                    <p>You may need to log in as a different user.</p>
                </NotAuthorized>
            </AuthorizeRouteView>
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <h1>Sorry</h1>
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>

appsettings.Development.json

{
  "DetailedErrors": "true",
  "ConnectionStrings": {
    "MyDB": "Data Source=x.x.x.x;Initial Catalog=xxxxx;user id=me;password=sshhh;Persist Security Info=False;"
  },
  "Ida": {
    "ADFSMetadata": "https://adfs.ourServer.com/FederationMetadata/2007-06/FederationMetadata.xml",
    "Wtrealm": "https://localhost:44323/"
  } 
}

Startup.cs(仅显示安全相关代码)

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.WsFederation;
public class Startup
{
    public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        .....
        app.UseAuthentication();
        app.Use(async (context, next) =>
        {
            context.Response.Headers.Add("X-Frame-Options", "DENY");
            var user = context.User;
            if (user?.Identities.HasNoItems(identity => identity.IsAuthenticated) ?? true)
            {
                await context.ChallengeAsync(WsFederationDefaults.AuthenticationScheme).ConfigureAwait(false);
            }

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

        ....
    }

...

    public void ConfigureServices(IServiceCollection services)
    {
        var wtrealm = this.Configuration.GetSection("Ida:Wtrealm").Value;
        var metadataAddress = this.Configuration.GetSection("Ida:ADFSMetadata").Value;

        services
            .AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
            })
            .AddWsFederation(options =>
            {
                options.Wtrealm = wtrealm ;
                options.MetadataAddress = metadataAddress;
                options.UseTokenLifetime = false;
            })
            .AddCookie();
        ....
    }
}

关于上面的代码有什么建议吗?当用户进入我们的站点(任何页面)时,他们会自动被推送到 ADFS 服务器进行身份验证。似乎还可以,但会阻止用户注销....

因此,我们从 ADFS 中获得了一些标识用户的声明,例如他们的 UPN 名称。我的想法是去数据库并加载该用户拥有的所有角色/权限/权限。

任何指针都将是最受欢迎的......假设我是这方面的新手。

标签: authenticationasp.net-identityadfsclaims-based-identityblazor-server-side

解决方案


执行此操作的常用方法是为 ADFS编写自定义属性提供程序。

然后您可以从数据库中获取您想要的角色并将它们添加到声明中。


推荐阅读