首页 > 解决方案 > 在 Blazor 服务器应用程序中托管身份服务器 4(Duende 服务器)

问题描述

是否可以在 Blazor Server 应用程序中托管 Identity Server 4?
我找到的所有文档都与在 ASP.NET 核心 Web 应用程序中托管 Identity Server 有关,但我想以类似的方式在 Blazor Server 应用程序中托管它。我相信这应该是可能的,因为 Blazor Server 本质上是由 ASP.NET 核心 Web 应用程序托管的。
基本上,我想做一些类似于拥有一个带有托管 asp.net 核心应用程序的 Blazor WASM 的事情,您可以在其中托管身份服务器和 API 并确保它们的安全,但只有一个 Blazor 服务器。
我对如何配置所有服务,尤其是 Identity Server 的客户端有点迷茫。我正在使用 .NET 6 和单个用户帐户的 Blazor Server 模板。Identity/Account 中的所有页面都是使用 Identity 脚手架生成的页面。
现在的主要问题是我能够访问登录页面并登录,并且我的用户名显示在 /Identity/ 的 .cshtml 页面中,但是当我返回 .razor 页面时,AuthenticationState 中没有用户.
我目前有类似的东西:

In Program.cs:

builder.Services.AddDefaultIdentity<IdentityUser>(options =>
{
    //Skipped configuration
})
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<DbContext>();

builder.Services.ConfigureApplicationCookie(options =>
{
    // Cookie settings
    options.Cookie.HttpOnly = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

    options.LoginPath = "/Identity/Account/Login";
    options.AccessDeniedPath = "/Identity/Account/AccessDenied";
    options.SlidingExpiration = true;
});

builder.Services.AddIdentityServer()
    .AddApiAuthorization<IdentityUser, VSIdentityDbContext>()
    .AddProfileService<IdentityProfileService>()

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("Authorised", policyBuilder =>
    {
        policyBuilder.RequireAuthenticatedUser()
                     .RequireRole("admin", otherroles...)
                     .Build();
    });
});

builder.Services.AddAuthentication()
    .AddIdentityServerJwt();

//Default config for Blazor Server

var app = builder.Build();

//Default config for Blazor Server

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

//Default config for Blazor Server

app.Run();
IdentityProfileService.cs:

public class IdentityProfileService : IProfileService
    {
        private readonly IUserClaimsPrincipalFactory<IdentityUser> claimsFactory;
        private readonly UserManager<IdentityUser> userManager;

        public IdentityProfileService(IUserClaimsPrincipalFactory<IdentityUser> claimsFactory, UserManager<IdentityUser> userManager)
        {
            this.claimsFactory = claimsFactory;
            this.userManager = userManager;
        }

        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var userId = context.Subject.GetSubjectId();
            var user = await userManager.FindByIdAsync(userId);
            var claimsPrincipal = await claimsFactory.CreateAsync(user);
            var claims = claimsPrincipal.Claims.ToList();

            var mappedClaims = new List<Claim>();

            foreach (var claim in claims)
            {
                if (claim.Type == JwtClaimTypes.Role)
                {
                    mappedClaims.Add(new Claim(ClaimTypes.Role, claim.Value));
                }
            }
            claims.AddRange(mappedClaims);
            context.IssuedClaims = claims;
        }

        public async Task IsActiveAsync(IsActiveContext context)
        {
            var userId = context.Subject.GetSubjectId();
            var user = await userManager.FindByIdAsync(userId);
            context.IsActive = user != null;
        }
    }
appsetting.json:

//Other stuff

"IdentityServer": {
    "Key": {
      "Type": "Development"
    },
    "Clients": {
      "BlazorServerProyectName": {
        "Profile": "IdentityServerSPA"
      }
    }
  },

标签: c#blazoridentityserver4.net-6.0

解决方案


推荐阅读