【问题标题】:Unable to find DiscoveryClient for IdentityServer4找不到 IdentityServer4 的 DiscoveryClient
【发布时间】:2020-09-23 21:15:49
【问题描述】:

尝试访问发现客户端以访问其他端点并跟随, http://docs.identityserver.io/en/aspnetcore1/endpoints/discovery.html

在 .Net 7.5 MVC 应用程序中安装了 IdentityModel nuget 包。但是找不到DiscoveryClient

var discoveryClient = new DiscoveryClient("https://demo.identityserver.io");
var doc = await discoveryClient.GetAsync();

Identitymodel 中的 IdentityServer4 是否有变化

另外,找不到“Tokenclient”的参数。

【问题讨论】:

    标签: asp.net-mvc identityserver4 identityserver3 identitymodel


    【解决方案1】:

    能够弄清楚,IdentityModel的变化,它的所有扩展HttpClient

    https://identitymodel.readthedocs.io/en/latest/client/discovery.html

    var client = new HttpClient();
    
    var disco = await client.GetDiscoveryDocumentAsync("https://demo.identityserver.io");
    

    【讨论】:

      【解决方案2】:

      是的,你是对的。 IdentityModel NuGet 包有很多变化。

      下面的代码会帮助你:

      HttpClient httpClient = new HttpClient();
      
      //Below code will give you discovery document response previously we were creating using DiscoveryClient()
      
      // They have created `.GetDiscoveryDocumentAsync()` extension method to get discovery document.
      
      DiscoveryDocumentResponse discoveryDocument = await httpClient.GetDiscoveryDocumentAsync();
      
      
      // To create a token you can use one of the following methods, which totally depends upon which grant type you are using for token generation.
      
      Task<TokenResponse> RequestAuthorizationCodeTokenAsync(AuthorizationCodeTokenRequest)
      Task<TokenResponse> RequestClientCredentialsTokenAsync(ClientCredentialsTokenRequest)
      Task<TokenResponse> RequestDeviceTokenAsync(DeviceTokenRequest)
      Task<TokenResponse> RequestPasswordTokenAsync(PasswordTokenRequest)
      Task<TokenResponse> RequestRefreshTokenAsync(RefreshTokenRequest)
      Task<TokenResponse> RequestTokenAsync(TokenRequest)
      

      例如,如果您想为密码授予类型创建令牌,请使用以下代码:

      PasswordTokenRequest passwordTokenRequest = new PasswordTokenRequest()
                  {
                      Address = discoveryDocument.TokenEndpoint,
                      ClientId = ClientName,
                      ClientSecret = ClientSecret,
                      GrantType = GrantTypes.ResourceOwnerPassword,
                      Scope = scope,
                      UserName = userName,
                      Password = password
                  };
      
      httpClient.RequestPasswordTokenAsync(passwordTokenRequest);
      

      希望对你有帮助!

      【讨论】:

        【解决方案3】:

        如果您使用了一些示例代码并且其他答案不起作用,因为 HttpClient 没有 GetDiscoveryDocumentAsync

            var client = new HttpClient();
        
            var disco = await client.GetDiscoveryDocumentAsync("https://localhost:5001");
        

        在 Visual Studio 中更新您的 IdentityModel 包:

        右键依赖->管理Nuget包->更新(在右上角选择“全部”)

        【讨论】:

          猜你喜欢
          • 2017-08-08
          • 1970-01-01
          • 2020-06-15
          • 2021-03-02
          • 2019-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-16
          相关资源
          最近更新 更多