IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

suggestion for documentation

Open Triwaters opened this issue 9 years ago • 3 comments

Apologies if I have placed this suggestion in the wrong spot.

Being new to much of this, I struggled mightily with the lowly console app on page 33 of the current documentation (7.9 Protecting an API)

There was not much to read on the page itself, and what I finally got to work after combing through multiple examples provided in the IdentityServer demos here (but none specifically referenced in the documentation!) is the below code, which is a fair bit more elaborate than what is provided in the documentation.

The documentation lead me to believe I could simply add a console project to my solution, add the IdentityModel NuGet package, slap in a few lines of code as shown on p.33-34, and be off to the races.

Unless I'm mistaken, this is not so.

If there were a link in the documentation that pointed specifically to a particular demo project, I would not be offering this suggestion. But I didn't see one. So the below code is cobbled together from what I found in the provided demos... but there are many of them, and it's not always obvious which to try first.

I am am wondering how, for the next user, this code that specifically fits the documentation can be made more accessible.

Also, I found (again, by studying one of the demos that worked) that the following must be in the console .csproj file:

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
   <LangVersion>latest</LangVersion>
 </PropertyGroup>

This below is what worked for me in the Program.cs file. (And yes, I also found myself pulling in that Constants project as well. (The one that has Constants, ConsoleExtensions, etc.)

using IdentityModel.Client;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace IdSvr
{
    public class Program
    {
        public static async Task Main()
        {
            Console.Title = "Console Client Credentials Flow";

            try
            { 
                var response = await RequestTokenAsync();
            
                if (response.IsError)
                {
                    response.Error.ConsoleRed();
                    //return;
                }
                else
                {
                    "\n\nServer Token Received:".ConsoleGreen();
                    Console.WriteLine(response.Json);                
                }
            
                await CallServiceAsync(response.AccessToken);

            }
            catch (Exception ex)
            {
                // oh well
                ex.ToString().ConsoleRed();
            }
            finally
            {
                "\n\nPress Any Key To Close Console...".ConsoleGreen();
                Console.ReadKey();
            }
        }

        static async Task<TokenResponse> RequestTokenAsync()
        {
            try
            {
                var disco = await DiscoveryClient.GetAsync(Constants.Authority);
                if (disco.IsError)
                    throw new Exception(disco.Error);

                var client = new TokenClient(
                    disco.TokenEndpoint,
                    "client",
                    "secret");

                return await client.RequestClientCredentialsAsync("api1");
            }
            catch (Exception ex)
            {
                // oh well
                //ex.ToString().ConsoleRed();
                throw ex;
            }            
        }

        static async Task CallServiceAsync(string token)
        {
            try
            {

                var client = new HttpClient
                {
                    BaseAddress = new Uri(Constants.SampleApi)
                };
            
                client.SetBearerToken(token);
                var response = await client.GetAsync("identity");

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    "\n\nService claims:".ConsoleGreen();
                    Console.WriteLine(JArray.Parse(content));
                }
                else
                {
                    response.StatusCode.ToString().ConsoleRed();
                }
            }
            catch (Exception ex)
            {
                // oh well
                //ex.ToString().ConsoleRed();
                throw ex;
            }

        }
    }
}

Triwaters avatar Oct 13 '17 01:10 Triwaters

Thanks for your feedback - writing docs is hard.

You have probably noticed the "Edit" button on the docs. Feel free to improve the docs yourself. Thanks!

leastprivilege avatar Oct 13 '17 05:10 leastprivilege

Haha, I will try that out, but after you see the edits, you will certainly want to fix them up, as I will be dancing around the fact that I do not know what the csproj edits, for example, mean. Good documentation is hard. I appreciate all you have put into this project.

Triwaters avatar Oct 13 '17 14:10 Triwaters

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 14 '20 05:01 lock[bot]