IdentityServer4 – debugging source
Do you have a .NET Core 2.2 project that is referencing IdentityServer4 (IdentityServer4.dll) 2.4.0, and would like to dig into the source code? Maybe you would like search for a specific error message in the source code, set a breakpoint there, watch variables and trace back where the values comes from. Or you would like add some custom logging statements, publish to a server to get a fair chance of pinpointing a problem and specific variable values. Welcome to my step-by-step experience.
Walkthrough
- Identify your referenced version of IdentityServer4. Right-mouse-button (RMB) on you .NET project, “Manage NuGet packages”, Installed tab, find “IdentityServer4” (by Brock Allen,Dominick Baier), and make a note of version at “Installed:”, e g 2.4.0.
- Find exactly that version on GitHub page https://github.com/IdentityServer/IdentityServer4/releases. Click on the label link, e g 2.4.0. Click on the green button “Clone or download” and select “Download ZIP”. Download and unzip. [If you do not select the same version you may later experience dotnetstandard vs netcoreapp targeting conflicts.]
- Open \IdentityServer4-2.4.0\src\IdentityServer4\IdentityServer4.sln in Visual Studio 2019.
- Tools > NuGet Package Manager > Package Manager Settings > Package Sources. Select the local source item and click the red X to delete the package source. [Otherwise you may experience “Can not find C:\… files”]
- Add your project to the solution. Remove the reference from your project to IdentityServer via RMB “Manage NuGet packages”, Installed tab, find “IdentityServer4” and choose Uninstall. RMB on your project > Add > Reference > Project > Check IdentityServer4 and not the other IdentityServer-subprojects. Choose OK. [If you refer to the other projects you may experience “Warning MSB3243 No way to resolve conflict between “IdentityServer4, Version=2.4.0.0, Culture=neutral, PublicKeyToken=f294d0afe402bb2b” and “IdentityServer4”. Choosing “IdentityServer4, Version=2.4.0.0, Culture=neutral, PublicKeyToken=f294d0afe402bb2b” arbitrarily.”]
- Build solution will complain that the IdentityServer-subprojects is referring to 2.4.0 and not 0.0.0 […]. RMB on the IdentityServer4-project > Package > Assembly version. Enter the IdentityServer4-project version. A solution build should now be successful.
Suggestions on how the referenced IdentityServer4-project can be used
- You may have noticed a specific error message and would like to have more information about the execution concerning that passage in code. Find the text in the IdentityServer4-project and set a breakpoint. Start the project and enjoy when the execution stops at your breakpoint, discovering variable values and call stack.
- Add “_logger.LogDebug(“debugging texts”);” in the source code, publish your project and deploy to server. Enjoy your additional loggings to be printed in the log files in the project folder. Maybe you would like to print out the value of a specific variable. Or simply follow the execution with different logging statements.