Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find DiscoveryClient for IdentityServer4

Trying to access discovery client for acceising other endpoints anf following with, http://docs.identityserver.io/en/aspnetcore1/endpoints/discovery.html

Installed IdentityModel nuget package in .Net 7.5 MVC application. But unable to find the DiscoveryClient.

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

Is there something change in Identitymodel for IdentityServer4

Also, unable to find parameter for "Tokenclient".

like image 355
Cod29 Avatar asked Mar 02 '20 17:03

Cod29


3 Answers

Able to figure out, change in IdentityModel, its all extension of HttpClient.

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

var client = new HttpClient();

var disco = await client.GetDiscoveryDocumentAsync("https://demo.identityserver.io");
like image 148
Cod29 Avatar answered Nov 15 '22 09:11

Cod29


Yes, you are correct. There are lot of changes in the IdentityModel NuGet package.

Below code will help you:

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)

For example if you want to create a token for password grant type then use below code:

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

httpClient.RequestPasswordTokenAsync(passwordTokenRequest);

I hope this will help you!

like image 32
Mahesh More Avatar answered Nov 15 '22 07:11

Mahesh More


If you used some sample code and the other answers aren't working, because HttpClient doesn't have GetDiscoveryDocumentAsync

    var client = new HttpClient();

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

Update your IdentityModel package, in Visual Studio:

Right click Dependencies -> Manage Nuget Packages -> Updates (select "All" in top right corner)

like image 41
Heinzlmaen Avatar answered Nov 15 '22 09:11

Heinzlmaen