【问题标题】:Issue running IdentityServer4运行 IdentityServer4 的问题
【发布时间】:2023-03-25 19:42:01
【问题描述】:

我正在了解 IdentityServer4 https://identityserver4.readthedocs.io/en/release/quickstarts/0_overview.html,但似乎无法正常工作。

创建项目的 API 部分并进行相应配置后:

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
            {
                Authority = "http://localhost:5000",
                RequireHttpsMetadata = false,

                ApiName = "api1"
            });

            app.UseMvc();
        }

我收到以下错误:

'IApplicationBuilder' does not contain a definition for 'UseIdentityServerAuthentication' and no extension method 'UseIdentityServerAuthentication' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

任何建议我在这里缺少什么?

编辑 1: 我注意到即使软件包已安装并存在,我也无法引用命名空间。

using IdentityServer4.AccessTokenValidation;

using IdentityServer4;

【问题讨论】:

  • 您使用的是哪个版本的 Visual Studio?我正在使用 VS 2017。我遇到了同样的问题。但是,即使显示此问题,它也会构建。我按照@Jeremy Cook 的建议关闭并打开了 VS 2017,问题就消失了。
  • Vs2017。在 vs2015 中有同样的问题。重启 ide 没有帮助

标签: asp.net identityserver4


【解决方案1】:

好吧,我刚刚关闭并重新打开了 Visual Studio 2017,问题就消失了。也许 Visual Studio 第一次拉下 IdentityServer4.AccessTokenValidation 包时噎住了,重新打开项目导致它再次尝试,这次成功了。

【讨论】:

  • 这是一个假设是答案还是评论?
  • @johnny5 这是一个答案。我几乎包括在答案中“是的,我知道,有史以来最蹩脚的答案!”更重要的是,这个问题似乎并不特定于该软件包,而是一个工具问题,有望在一些 VS 更新后消失,例如 ardalis.com/force-nuget-to-reinstall-packages-without-updating
  • 我很困惑,虽然你是我们的 OP,因为措辞
  • 同理,关闭IDE再重新打开! .
  • 对我不起作用。在 Vs2017 和 AccessTokenValidation 2.2
【解决方案2】:

如果您从 net core 1.1 迁移到 2.0,则需要按照 Identity Server: API Migration to ASP.NET Core 2 中的说明迁移代码

在配置函数中

Before:
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = "http://localhost:5000",
    RequireHttpsMetadata = false,
    ApiName = "apiApp"
});

After:
app.UseAuthentication();

在 ConfigureServices 函数中

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = 
                               JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = 
                               JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
    o.Authority = "http://localhost:5000";
    o.Audience = "apiApp";
    o.RequireHttpsMetadata = false;
});

【讨论】:

    【解决方案3】:

    您需要this 包。并使用命名空间IdentityServer4.AccessTokenValidation

    【讨论】:

    • 已经安装了,但有趣的发现,我尝试引用命名空间并且它没有被拾取。
    • @Aeseir 我昨天在阅读教程时遇到了同样的错误。我在 VS 2017 和<TargetFramework>net461</TargetFramework>。不确定这是否相关。我可以看到该包包含在 .csproj 中,为 <PackageReference Include="IdentityServer4.AccessTokenValidation" Version="1.1.0" />
    【解决方案4】:

    我遇到了同样的问题,我通过关注IdentityServer 4 Quickstart Samples 解决了,尤其是Javascript Client 示例。

    所以,这就是它的工作原理:

    1. 检查依赖版本。报告了使用 .NET Core 2.0 运行的 IdentityServer 的问题。在我的 API 项目中,我使用了 Microsoft.Asp.NetCore.All 2.0.5 版和 IdentityServer4.AccessTokenValidation 2.3.0 版。

    2. 在 Startup.cs 中,在 Configure 方法中添加:app.UseAuthentication();因为它用于example

    3. 最后一步是向 ConfigureServices 方法添加身份验证,就像在 example 中一样:

      public void ConfigureServices(IServiceCollection services)
      {
        services.AddMvcCore()
          .AddAuthorization()
          .AddJsonFormatters();
      
        services.AddAuthentication("Bearer")
          .AddIdentityServerAuthentication(options =>
          {
              options.Authority = "http://localhost:5000";
              options.RequireHttpsMetadata = false;
      
              options.ApiName = "api1";
          });
       }
      

    这在我的情况下解决了。希望它也对你有用,请告诉我它是怎么回事。

    编码愉快。

    【讨论】:

    • 非常感谢,你为我节省了很多时间:)
    • 奇妙吧!与此页面上的其他建议不同,这也适用于 .Net Core 2.1。
    【解决方案5】:

    只是为了提供更新/答案,IdentityServer4 软件包的后续版本运行良好。

    当前使用 ID4 2.0 包。

    【讨论】:

    • 不是真的,我使用的是最新版本 2.0.6 并面临同样的错误!
    • 更正,我在 ValidationToken 包 2.2 上,仍然有同样的问题。重启VS也不行...
    【解决方案6】:

    你可以在githubIdentityServer4查看最新文档:

    它使用services.AddAuthentication 方法在ConfigureServices 方法中设置身份验证选项。

    这是他们正在使用的test server 代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-22
      • 2020-09-12
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多