可以使用 SAML V2 来保护 Blazor WebAssembly 应用程序。下面是实现此目的的基本步骤:
1.将 Sustainsys.Saml2.AspNetCore2 包添加到 Blazor WebAssembly 应用程序项目中。
2.在 Startup.cs 中,添加以下代码段以启用 SAML2 身份验证:
services.AddAuthentication()
.AddSaml2(options =>
{
options.SPOptions.EntityId = new EntityId("{EntityId}");
options.IdentityProviders.Add(
new IdentityProvider(
new EntityId("{IdpEntityId}"),
options.SPOptions)
{
AllowUnsolicitedAuthnResponse = true,
Binding = Saml2BindingType.HttpRedirect
});
});
其中,EntityId 和 IdpEntityId 分别表示您的应用程序和身份验证提供程序的实体 ID。
3.在 App.razor 中,使用以下代码段包装您的应用程序组件:
@(await HttpContextProvider.GetContextAsync().ConfigureAwait(false)).ChallangeAsync()
其中,MyApp 是您的应用程序组件。
4.添加以下代码段以接收 SAML2 断言:
var saml2Defaults = new Saml2Defaults();
app.UseSaml2Authentication(new Saml2AuthenticationOptions(false)
{
SPOptions = new SPOptions
{
EntityId = new EntityId(saml2Configuration.EntityId),
ReturnUrl = saml2Configuration.ReturnUrl,
AllowUnsolicitedAuthnResponse = true,
AssertionConsumerServiceBinding = Saml2BindingType.HttpPost,
AuthenticationServiceUrl = new Uri($"{saml2Configuration.IdentityProviderUrl}/sso"),
ArtifactResolutionServiceUrl = new Uri($"{saml2Configuration.IdentityProviderUrl}/artifact"),
SingleLogoutServiceUrl = new Uri($"{saml2Configuration.IdentityProviderUrl}/slo"),
SigningCertificate = CertificateHelper.LoadCertificate(saml2Configuration.SigningCertificatePath),
SignatureValidation