You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为已启用Windows Authentication的Blazor Server应用实现SSO?

Blazor Server 从Windows Authentication迁移到SSO(SAML/OIDC)的最简方案

你完全不需要重新创建应用,直接在现有项目上扩展或替换认证方式即可,下面分两种协议给出最简实现步骤,以及Windows Auth的兼容说明:


Open ID Connect(OIDC)实现(推荐,.NET原生支持更省心)

OIDC是.NET生态支持最好的SSO协议,步骤最少:

  • 安装NuGet包:Microsoft.AspNetCore.Authentication.OpenIdConnect
  • 改造Program.cs的认证配置:
    找到原有的AddAuthentication代码,替换或扩展成如下逻辑(可以保留Windows Auth作为备选,也可以直接替换):
    builder.Services.AddAuthentication(options =>
    {
        // 默认用Cookie保存认证状态,挑战时用OIDC
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
    .AddCookie()
    .AddOpenIdConnect(options =>
    {
        // 这些参数要从公司身份提供商(IdP)获取
        options.Authority = "公司OIDC提供商的基础地址";
        options.ClientId = "你的应用在IdP上注册的Client ID";
        options.ClientSecret = "你的应用在IdP上注册的Client Secret"; // 机密客户端需要这个
        options.ResponseType = "code";
        // 按需添加需要的权限范围
        options.Scope.Add("openid");
        options.Scope.Add("profile");
        options.SaveTokens = true;
        // 回调地址必须在IdP上注册,格式示例:https://your-app-domain/signin-oidc
        options.CallbackPath = "/signin-oidc";
    });
    
    确保app.UseAuthentication()和app.UseAuthorization()代码放在app.MapBlazorHub()之前,否则认证逻辑不生效。
  • 页面层无需大改:原有的<AuthorizeRouteView>、<AuthorizeView>以及[Authorize]特性都能直接适配OIDC认证后的用户身份。

SAML 2.0实现(需第三方库)

因为.NET没有原生SAML支持,用最流行的ITfoxtec.Identity.Saml2库来实现:

  • 安装NuGet包:ITfoxtec.Identity.Saml2.MvcCore(适配Blazor Server场景)
  • 在appsettings.json里添加SAML配置:
    "Saml2": {
        "IdPMetadata": "公司IdP提供的元数据地址(或本地XML文件路径)",
        "Issuer": "你的应用在IdP上注册的唯一标识",
        "CallbackPath": "/Saml2/Acs",
        "LogoutPath": "/Saml2/Logout"
    }
    
  • 配置Program.cs:
    builder.Services.AddSaml2(options =>
    {
        builder.Configuration.GetSection("Saml2").Bind(options);
    });
    
    同样要保证UseAuthentication和UseAuthorization在MapBlazorHub之前。
  • 添加回调控制器:
    创建一个Saml2Controller来处理SAML的登录回调和登出请求:
    public class Saml2Controller : Controller
    {
        private readonly Saml2Configuration _samlConfig;
        private readonly Saml2AuthnResponse _authnResponse;
    
        public Saml2Controller(IOptions<Saml2Configuration> samlConfig, Saml2AuthnResponse authnResponse)
        {
            _samlConfig = samlConfig.Value;
            _authnResponse = authnResponse;
        }
    
        [HttpPost]
        public async Task<IActionResult> Acs()
        {
            await _authnResponse.ReadSamlResponseAsync(Request);
            if (_authnResponse.IsValid())
            {
                var claimsIdentity = new ClaimsIdentity(_authnResponse.Claims, Saml2AuthenticationDefaults.AuthenticationScheme);
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));
                return RedirectToPage("/");
            }
            return BadRequest("SAML认证失败");
        }
    
        public async Task<IActionResult> Logout()
        {
            var logoutRequest = new Saml2LogoutRequest(_samlConfig, User);
            return Redirect(logoutRequest.CreateLogoutRequestUrl());
        }
    }
    
  • 页面层同样无需大改,标准授权组件直接可用。

原有Windows Authentication的兼容问题

  • 如果你需要保留Windows Auth作为备选,可以在认证配置里同时添加AddNegotiate()(.NET 6+的Windows Auth方式),设置默认方案为Cookie,这样用户可以根据场景选择认证方式,或者通过路径规则自动切换。
  • 如果不需要保留Windows Auth,直接移除原有的AddNegotiate()或AddWindowsAuthentication()配置,替换成上述OIDC/SAML的配置即可,不会影响现有Blazor页面的授权逻辑——只要你用的是.NET标准的授权组件,就能自动识别新的认证身份。

内容的提问来源于stack exchange,提问作者Mdarende

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 04:35:30