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

寻求可兼容.NET Framework4.8与Blazor(.NET7)的通用身份认证方案

可行的临时兼容方案

方案1:共享Cookie认证(最直接的并行方案)

核心思路是让新旧应用使用相同的Cookie格式和加密密钥,Blazor Server通过自定义逻辑对接旧的Membership Provider验证用户身份。

步骤1:统一机器密钥

在Web Forms应用的web.config中配置明确的机器密钥(如果之前是自动生成的,必须手动指定并复制到Blazor应用):

<system.web>
  <machineKey 
    validationKey="YOUR_VALIDATION_KEY" 
    decryptionKey="YOUR_DECRYPTION_KEY" 
    validation="SHA1" 
    decryption="AES" />
</system.web>

步骤2:Blazor Server配置兼容Cookie认证

在Blazor的Program.cs中配置Cookie认证,使用和Web Forms一致的Cookie名称、域名,并自定义验证逻辑对接Membership:

using Microsoft.AspNetCore.Authentication.Cookies;
using System.Web.Security;

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        // 和Web Forms默认的Cookie名保持一致
        options.Cookie.Name = ".ASPXAUTH";
        // 跨子域名共享的话设置统一域名
        options.Cookie.Domain = "your-domain.com";
        options.Events = new CookieAuthenticationEvents
        {
            OnValidatePrincipal = async context =>
            {
                var username = context.Principal.Identity?.Name;
                if (string.IsNullOrEmpty(username))
                {
                    context.RejectPrincipal();
                    await context.HttpContext.SignOutAsync();
                    return;
                }

                // 调用Membership Provider验证用户有效性
                if (!Membership.ValidateUser(username, null))
                {
                    context.RejectPrincipal();
                    await context.HttpContext.SignOutAsync();
                }
            }
        };
    });

// 确保添加授权中间件
builder.Services.AddAuthorization();

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

注意事项

  • Blazor Server中引用System.Web.dll时,需确保项目兼容.NET Framework 4.8的依赖,或把Membership的验证逻辑封装到** .NET Standard 2.0类库**中,让新旧应用都能调用。
  • 验证逻辑可根据实际情况调整,比如从Cookie票据中提取用户标识,而非仅依赖用户名。

方案2:搭建统一身份验证API中间层

如果不想在Blazor中直接依赖Membership Provider,可以单独写一个身份验证API(基于.NET Framework或.NET Core),作为新旧应用的统一身份入口:

  • API封装Membership Provider的验证、用户信息查询逻辑;
  • Web Forms应用调用API完成登录,生成兼容的Cookie;
  • Blazor Server应用调用API验证用户,使用JWT或Cookie认证;
  • 这种方案隔离了新旧应用的认证依赖,后续迁移完成后可直接切换到.NET Core Identity。

方案3:自定义Blazor AuthenticationStateProvider

通过实现Blazor的AuthenticationStateProvider,直接对接Membership Provider的用户体系,让Blazor应用基于旧认证系统提供身份状态:

using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
using System.Web.Security;

public class MembershipAuthStateProvider : AuthenticationStateProvider
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public MembershipAuthStateProvider(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public override Task<AuthenticationState> GetAuthenticationStateAsync()
    {
        ClaimsPrincipal principal;
        var httpContext = _httpContextAccessor.HttpContext;
        var username = httpContext?.User.Identity?.Name;

        if (!string.IsNullOrEmpty(username) && Membership.ValidateUser(username, null))
        {
            // 构建包含用户声明的身份
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, username)
                // 添加角色、其他自定义声明
            };
            var identity = new ClaimsIdentity(claims, "MembershipAuth");
            principal = new ClaimsPrincipal(identity);
        }
        else
        {
            // 未认证的空身份
            principal = new ClaimsPrincipal(new ClaimsIdentity());
        }

        return Task.FromResult(new AuthenticationState(principal));
    }

    // 可选:提供手动触发身份状态更新的方法
    public void NotifyAuthenticationStateChanged()
    {
        NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
    }
}

然后在Program.cs中注册该服务:

builder.Services.AddScoped<AuthenticationStateProvider, MembershipAuthStateProvider>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddAuthorization();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 17:45:55