.NET5 和 OpenIdConnection 与 IdentityServer4

问题描述

我正在尝试将我的网络应用程序与 .NET5 和新的 Microsoft.AspNetCore.Authentication.OpenIdConnect 连接起来。显然,我的 OpenIdConnect 旧代码是有效的,但它不起作用。这是我要添加到 ConfigureServices 的内容:

services.AddSession(options =>
{
    options.Cookie.Name = ".puresourcecode.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 = "puresourcecode.cookie";
})
.AddOpenIdConnect("oidc",options =>
{
    options.Authority = idsrv.IdentityServerUrl;
    options.ClientId = idsrv.ClientId;
    options.ClientSecret = idsrv.ClientSecret;

#if DEBUG
        options.RequireHttpsMetadata = false;
#else
        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,};
});

这是我添加到配置的内容:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
});

那么,2个问题:

  • 是否可以将 .NET5 (ASP.NET) 项目连接到 IdentityServer4?
  • 是否可以在同一个项目中使用 Microsoft Identity 和 OpenIdConnect

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...