【问题标题】:ASP.NET Core Razor Pages integrated with Identity Server与 Identity Server 集成的 ASP.NET Core Razor 页面
【发布时间】:2020-12-10 13:25:02
【问题描述】:

我有一个包含 ASP.NET Core 3.1 和 Razor 页面的项目。该应用程序非常简单:一个用于保护静态文件的欢迎页面。当我想将我的小型网站与 Identity Server 4 集成时,我的问题就开始了。

通常,在我的其他 .NET Core 项目中,我在 Startup.cs 中添加了:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages(options => {
            options.Conventions.AuthorizePage("/Login");
        });

        services.Configure<IdentityServerConfiguration>(Configuration.GetSection("IdentityServerConfiguration"));

        services.AddDistributedMemoryCache();

        services.AddSession(options =>
        {
            options.Cookie.Name = ".cello.Session";
            options.IdleTimeout = TimeSpan.FromHours(12);
        });

        services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = "oidc";
        })
        .AddCookie(options =>
        {
            options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
            options.Cookie.Name = "cello.dashboard";
        })
        .AddOpenIdConnect("oidc", options =>
        {
            IdentityServerConfiguration idsrv = Configuration.GetSection("IdentityServerConfiguration").Get<IdentityServerConfiguration>();
            options.Authority = idsrv.Url;
            options.ClientId = idsrv.ClientId;
            options.ClientSecret = idsrv.ClientSecret;

#if 调试 options.RequireHttpsMetadata = false; #别的 options.RequireHttpsMetadata = true; #endif

            options.ResponseType = "code";

            options.Scope.Clear();
            options.Scope.Add("openid");
            options.Scope.Add("profile");
            options.Scope.Add("email");
            options.Scope.Add("roles");
            options.Scope.Add("offline_access");

            options.ClaimActions.MapJsonKey("role", "role", "role");

            options.GetClaimsFromUserInfoEndpoint = true;
            options.SaveTokens = true;

            options.SignedOutRedirectUri = "/";

            options.TokenValidationParameters = new TokenValidationParameters
            {
                NameClaimType = JwtClaimTypes.Name,
                RoleClaimType = JwtClaimTypes.Role,
            };
        });
    }

当我尝试添加 Microsoft.AspNetCore.Authentication.OpenIdConnect 时出现不兼容错误。

我必须使用这个库的 3.1.9 版本,而不是最新版本。

有什么想法吗?

【问题讨论】:

    标签: c# asp.net identityserver4


    【解决方案1】:

    2020 年 11 月,Microsoft 发布了 .NET 5 作为 .NET Core 的下一个版本。无法从面向 .NET Core 3.x.x 的项目中引用面向 .NET 5 的项目。最新版本的ASP.NET Core 包已经切换到net5.0(包括OpenIdConnect 包)。

    为了能够使用最新版本的Microsoft.AspNetCore.Authentication.OpenIdConnect,您应该migrate your project to .NET 5

    【讨论】:

      【解决方案2】:

      在你的情况下使用这个库的 v 3.1.* 是可以的。 Microsoft.AspNetCore.Authentication.OpenIdConnect/3.1.10 是与 .NET Core 3.1 兼容的最新版本。 如果您想使用Microsoft.AspNetCore.Authentication.OpenIdConnect/5.0.1,您的应用程序应该是.NET 5。

      此外,我不确定此包中是否有任何针对 .NET 5 的重大更改,除了与 .NET 5 兼容。

      【讨论】:

        猜你喜欢
        • 2019-12-15
        • 2019-04-17
        • 2019-07-19
        • 2021-04-19
        • 2018-06-15
        • 2020-08-22
        • 2019-05-22
        • 2018-11-26
        • 2021-10-27
        相关资源
        最近更新 更多