DrG*_*iff 8 authentication authorization adfs asp.net-core blazor-server-side
我有一个解决 .NET Core 3.1 预览版 2 的现有 Blazor(服务器)应用程序。
我需要追溯添加本地 ADFS(不是 Azure)安全性。我一直在尝试在 ASP.NET Core 中使用 WS-Federation关注 Microsoft 的Authenticate users,但它顽固地忽略了安全性。这篇文章当然是为 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();
....
Run Code Online (Sandbox Code Playgroud)
值得关注的一件事- 数据库中的表当前支持早期的身份验证模式(成员资格?)(由我们正在重写的应用程序使用)。它有表 [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);
}
}
Run Code Online (Sandbox Code Playgroud)
在 Configure 方法中,我添加了(虽然我不清楚是否需要):
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
Run Code Online (Sandbox Code Playgroud)
在 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>
Run Code Online (Sandbox Code Playgroud)
在我的一个剃刀页面 (MyPage.razor) 中,我有:
@page "/myRoute"
@attribute [Authorize]
Run Code Online (Sandbox Code Playgroud)
当我浏览具有 Autorize 属性的页面时,我收到以下消息:
未经授权
所以它不会调用我的 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" />
Run Code Online (Sandbox Code Playgroud)
我什至按照 Microsoft 的示例使用 WS-Federation without ASP.NET Core Identity,但没有运气。
小智 5
这是一篇较旧的帖子,但对我来说仍然首先出现在谷歌中,所以..
我或多或少也遇到了同样的问题。OP 在这个帖子中的帖子对我有帮助:
Blazor - 通过本地数据库存储库使用 ADFS 进行保护:如何/何时连接到 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);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1447 次 |
| 最近记录: |