asp.net-mvc - 无法找到 IdentityServer4 的 DiscoveryClient

标签 asp.net-mvc identityserver4 identityserver3 identitymodel

尝试访问发现客户端以访问其他端点并跟随,
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”的参数。

最佳答案

是的,你是对的。 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)

例如,如果要为密码授予类型创建 token ,请使用以下代码:
PasswordTokenRequest passwordTokenRequest = new PasswordTokenRequest()
            {
                Address = discoveryDocument.TokenEndpoint,
                ClientId = ClientName,
                ClientSecret = ClientSecret,
                GrantType = GrantTypes.ResourceOwnerPassword,
                Scope = scope,
                UserName = userName,
                Password = password
            };

httpClient.RequestPasswordTokenAsync(passwordTokenRequest);

我希望这能帮到您!

关于asp.net-mvc - 无法找到 IdentityServer4 的 DiscoveryClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60493640/

相关文章:

swagger - Swagger 和 IdentityServer4 范围无效

asp.net-core - 电子邮件未确认的用户仍处于登录状态

asp.net-mvc - 将 LINQ to EF 投影到 View 模型时出现异常。获取 "only parameterless constructor and initializers"异常

c# - NHibernate 的工作单元/存储库管理器?

asp.net-mvc - 使用 ASP.NET MVC,我可以使用 jQuery GET 和 FilePathResult 下载文件吗?

asp.net-core - expires_in 或 expires_at 用于 OpenId 连接中的访问 token ?

c# - 在 ASP.NET 5 (MVC6) 中请求 BinaryRead

.net - 如何调用请求 header 上需要不记名 token 的 NSwag 客户端方法?

asp.net-identity - 如何向IdentityServer DI框架注册ApplicationUserManager?

c# - 如何在同一项目中运行 IdentityServer 和 WebAPI