<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Xavier Hahn on Medium]]></title>
        <description><![CDATA[Stories by Xavier Hahn on Medium]]></description>
        <link>https://medium.com/@xavier.hahn?source=rss-679faef91f2b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fde5XpVVYpfci1KbDMLTag.jpeg</url>
            <title>Stories by Xavier Hahn on Medium</title>
            <link>https://medium.com/@xavier.hahn?source=rss-679faef91f2b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 14:10:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@xavier.hahn/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Continuous deployment of ASP .Net Core app to Azure with Azure DevOps Pipelines]]></title>
            <link>https://itnext.io/continuous-deployment-of-asp-net-core-app-to-azure-with-azure-devops-pipelines-d561198f4e61?source=rss-679faef91f2b------2</link>
            <guid isPermaLink="false">https://medium.com/p/d561198f4e61</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[azure-pipelines]]></category>
            <category><![CDATA[azure-devops]]></category>
            <category><![CDATA[aspnetcore]]></category>
            <category><![CDATA[azure]]></category>
            <dc:creator><![CDATA[Xavier Hahn]]></dc:creator>
            <pubDate>Wed, 04 Mar 2020 22:16:03 GMT</pubDate>
            <atom:updated>2020-03-05T08:06:00.613Z</atom:updated>
            <content:encoded><![CDATA[<h3>Continuous deployment of ASP .Net Core app to Azure with Azure DevOps Pipelines</h3><p>Part 1: Define the strategy | Azure Pipelines YAML recipes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*4PDKlXpRIzvTeuP-ZrijBw.png" /><figcaption><a href="https://www.flickr.com/photos/htakashi/16277420261">Delivery service truck</a> — <a href="https://www.flickr.com/photos/htakashi/">Takashi Hososhima</a></figcaption></figure><p>In my <a href="https://itnext.io/azure-devops-yaml-pipeline-recipes-578b5eda3e76">previous article</a> we saw how to use Azure Pipelines to continuously build and deploy Nuget packages to Azure DevOps Artifacts. In this next part we are going to see how to continuously deploy an ASP .Net core web application to Azure App Services using Azure pipelines. In this first part, I’m discussing CI/CD and deployment strategy, the next article will explain how to implement the strategy.</p><h3>Continuous integration, delivery, deployment?</h3><p>Before we dive into the technical part, I wanted to try to clarify those three terms. What is the difference between continuous integration, continuous delivery and continuous deployment?</p><p><em>Continuous integration</em> is the practice of systematically running automated build scripts when code is merged into a mainline branch (“master” for example). This build script will try to build the solution, and run automated tests (unit tests, maybe integration tests) on the build results.</p><p><em>Continuous delivery</em> is basically going one step further, it uses continuous integration results to create a packaged application that is ready to be delivered to a customer. In the case of a desktop application it could be creating the installer package (msi for example). For an ASP .Net Core app it’s usually calling the dotnet publish command and create a ready to deploy package. The deployment itself is not automated and would be done manually.</p><p><em>Continuous deployment</em> is again going one step further, automating the deployment of the package to your end users so that you can do a “push button” delivery. In the solution I’m demonstrating in this article, I am using Azure Pipelines validation features to “block” a deployment until someone has reviewed the delivery, but only a click in the interface is needed to deploy in production.</p><p>If you want to get deeper in the subject, you should read <a href="https://martinfowler.com/bliki/ContinuousDelivery.html">Martin Fowler’s small article on the subject</a> and this very complete and interesting <a href="https://circleci.com/blog/a-brief-history-of-devops-part-iv-continuous-delivery-and-continuous-deployment/?gclid=CjwKCAiAnfjyBRBxEiwA-EECLO6MyEgcKIfOpkcf8cqvv1yE8HUzA9rwtnQHRvjwew-3bDER_zHg5hoCT7MQAvD_BwE">Circle CI blog post</a>.</p><h3>Define your deployment strategy</h3><p>Creating build and release scripts without having defined how and when those scripts will be triggered as well as defining the environments where they will be deployed is quite useless. It is therefore very important to first define a good strategy for our continuous deployment.</p><p>This strategy will define when to trigger builds and deliveries, how many deployment environments we will have, and in which environment the delivery will be pushed based on from what triggered it.</p><p>I’m going to describe a strategy that my team and I have tested with success. Think of it as a sample, which can be modified based on your tastes and needs. I would also be very interested in hearing your thoughts and remarks on this strategy.</p><h4>GitHub-ish Flow</h4><p>A good Continuous Integration / Continuous Delivery (CI/CD) strategy begins by defining a good branching strategy for managing your source control. There are a few branching strategy, <a href="https://nvie.com/posts/a-successful-git-branching-model/">Git Flow</a> being probably one of the most well-known. You can check a <a href="https://www.creativebloq.com/web-design/choose-right-git-branching-strategy-121518344">few</a> <a href="https://docs.microsoft.com/en-us/azure/devops/repos/git/git-branching-guidance?view=azure-devops">articles</a> on the subject if you’re curious.</p><p>I’m going to use a simpler strategy, <a href="https://guides.github.com/introduction/flow/">GitHub Flow</a>. In its core, it’s very close to Git Flow except that it removes the <em>develop</em> branch, systematically deploying changes in production as soon as it has been merged to <em>master</em>, or even during the Pull Request.</p><p>In this strategy, <em>master</em> is locked, no one can merge work on it. To make changes you must create a <em>feature</em> branch that will be merged to master through a <em>Pull request</em>. This allows code to be reviewed and discussed before it is merged (and therefore deployed), mandating code reviews which increases code quality as well as knowledge of the source code throughout the team.</p><p>I’m calling my strategy a GitHub-ish flow because in a school-book GitHub Flow, the deployment in production is made during the Pull Request which is not something I’m going to setup. I prefer to deploy into a development environment once the pull request has been completed and the feature branch merged into <em>master</em>.</p><h4>GitHub Flow in Azure DevOps</h4><p>Azure DevOps allows you to create branch policies to implement GitHub Flow and mandate the use of Pull Requests to merge changes into, for example, your master branch.</p><p>To do this, go to the <em>Branches</em> menu of your repository, open the contextual menu with the three dots and select <em>Branch policies</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*axQL-eQUXs-aL4NclK-qeg.png" /></figure><p>This will bring you to a menu that allows you to protect your branch by setting a number of rules that needs to be true for open Pull requests to pass. I usually set the following rules:</p><ul><li>Require a minimum number of reviewers with 2 minimum reviewers. All other options stay unchecked. Reset votes if there are new changes could be useful in some case but proved to be a bit too harsh for us.</li><li>Check for linked work items, with <em>Required</em>. We use Azure DevOps for our project management and each change must be linked to a work item (usually a task). This allows us to semi automate the creation of change logs sent to our customers.</li><li>Check for comment resolution, with <em>Required. </em>Code reviews is an important part of our day to day work, our goal is to get the higher code quality that we can get. We mandate that comments that are written in the Pull requests are resolved before the completion. Usually it’s the commenter who should resolve the comment, but we accept that the reviewee resolves if he did <em>exactly</em> what the commenter asked.</li><li>Add build policy, <em>Required</em>. Pull requests are systematically built using the build pipeline that will be used during the continuous integration phase. Unit tests are run during the build. This way, we ensure that Pull requests pass build and unit tests.</li><li>For some parts of the code where we have specialists, we sometimes add automatic reviewers so that more thorough checks by those developers are done before things go into production.</li></ul><p>Once your policy is set on your branch, you will not be able to push to it directly, you will have an error message from Git if you try. Go to Pull request and start a new one to merge changes back to master.</p><h4>Defining deployment environments</h4><p>Continuous deployment (at least how I understand it) does not mean necessarily deploying directly all the code that you publish into your master branch directly to your <strong>customers</strong>. Doing so would require an acceptation workflow and was too complex for our business needs.</p><p>Instead we decided to define three environments:</p><ul><li>A “development” environment, or “beta”. This is where code merged into master is directly deployed. This environment is linked to its own database with no production data. It’s used as a support for internal demonstration of new features and for selected customers who agree to do tests for us, for example on specific features.</li><li>A “staging” environment where we prepare our official release right before it is sent to our customers. Publishing to this environment is triggered by pushing a tag with a version number on master. <br>Before we deploy to this environment, we copy the production database (either the whole database or just the structure and a subset of the database depending on the size and complexity of the DB) to a staging database on which this environment is attached. <br>This allows us to make sure our database migration scripts will not break in production and do final acceptation testing (manual and some automated).</li><li>A “production” environment which is the one that our customers use.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qfG2jC3t1h-14BpBgHmM1Q.png" /></figure><h4>Environments in Azure App Services</h4><p>Azure App Services has a nice feature to manage different environments of the same application. It is called <em>Deployment slots</em> and allows to create live running application with different code running which can be “hot” swapped. It means that one environment will “become” another one, redirecting directly your users without having to restart the application.</p><p>Be careful that the development/testing app service plans (F1 and B1) do not support deployment slots, you’ll need to be on a production plan to be able to add one, minimum S1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GWPSMUDnOOYjPREm51GpLg.png" /></figure><p>Creating a deployment slot is very simple, click on <em>Add Slot</em>, select a name for your new slot and click <em>Add</em>. After a few seconds you will see your new slot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TypmmqYLK5-rorDzdfCA7w.png" /></figure><p>Each slot is its own App service running on the same plan. You will get a publishing profile for each slot, which mean that you can publish different code on each slot.</p><p>In our case, we will deploy code only on the development and staging slots, the code will be put in production by swapping the staging and production environment. This will allow, in case of a catastrophic failure, to get back to the version previously deployed by swapping again.</p><p>Swapping can be done directly in the Azure portal by clicking on the <em>Swap</em> button, but we will see how to do it automatically through an Azure pipeline script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G_Evofw0CZzo6JnGLLDRHA.png" /></figure><p>Usually deployment slots will have different settings. For example, you will probably have different database, which means different connection strings.</p><p>This can easily be dealt with by setting the <em>Deployment slot setting</em> checkbox to true on the settings that we want to <strong>not be swapped</strong>. This checkbox defines that the setting is <em>specific</em> to this slot and should not be swapped. The settings that don’t have this checkbox will be copied to the swapped to environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r2U67DHMvNpW9zRlzSVDFA.png" /></figure><p>You can find more information about the deployment slots in <a href="https://docs.microsoft.com/en-us/azure/app-service/deploy-staging-slots">the official documentation</a>.</p><h3>Conclusion</h3><p>This concludes this first part of the article. We have seen how to define a strategy for continuous deployment and define Azure App Service environments that will be ready to implement our strategy.</p><p>In the next article I will describe the Azure Pipelines used to implement the continuous integration and deployment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d561198f4e61" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/continuous-deployment-of-asp-net-core-app-to-azure-with-azure-devops-pipelines-d561198f4e61">Continuous deployment of ASP .Net Core app to Azure with Azure DevOps Pipelines</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Azure DevOps yaml Pipeline Recipes]]></title>
            <link>https://itnext.io/azure-devops-yaml-pipeline-recipes-578b5eda3e76?source=rss-679faef91f2b------2</link>
            <guid isPermaLink="false">https://medium.com/p/578b5eda3e76</guid>
            <category><![CDATA[azure-pipelines]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[azure-devops]]></category>
            <category><![CDATA[continuous-deployment]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <dc:creator><![CDATA[Xavier Hahn]]></dc:creator>
            <pubDate>Thu, 13 Feb 2020 21:40:15 GMT</pubDate>
            <atom:updated>2020-02-15T23:09:39.134Z</atom:updated>
            <content:encoded><![CDATA[<p>Part 1: Introduction and Create NuGet packages</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*46n9HUKEYegyqad1snLe_w.jpeg" /><figcaption>source: <a href="https://pxhere.com/en/photo/925035">https://pxhere.com/en/photo/925035</a></figcaption></figure><h3>Introduction</h3><p>In our team, we are using Azure DevOps quite extensively. We manage quite a few projects, and most have continuous integration and deployment setup. We use quite a few features of Azure DevOps Pipelines, from Azure Artifacts Nuget packages to local agents to run on-premise tools during release and local deployments.</p><p>We have started using the yaml pipelines definition on small projects since they were introduced but waited that they got a bit more mature to migrate our most important projects to that new definition. We took the opportunity of that migration to rework the pipelines and take advantage of the improvements from the “old” UI style builds. For example, we use multi-stage to better separate the builds from the different level of releases.</p><p>In this series of article, I want to describe some of our recipes, how we defined our builds for each type of projects.</p><h3>The Yaml pipelines</h3><p>I won’t go into too much details on how to setup the yml pipeline. You should have already basic knowledge on how to create and configure a new yml pipeline. If you need help in getting started, head to the <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/get-started/pipelines-get-started?view=azure-devops">Azure Pipelines documentation</a>.</p><p>Yaml pipelines have a lot of advantages compared to the old “UI” based one. The most prominent being that, since they are part of the repository, they are simple source code that goes alongside the rest of the application code. If a change in the source code mandates a change of the pipeline, you can do it in the same commit / branch. It can be part of a pull request, and the changes can even be executed during that pull request.</p><h3>.Net library to NuGet packages</h3><p>Now let’s discuss our first recipe. We have a lot of .Net class library projects that we transform into NuGet packages to include in our main applications.</p><h4>The code</h4><p>I have probably lost already half the readers with my introduction, so let’s get to the point. Here’s the code, I will explain it in details after.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81ddd69cee767774dad7b942d686b661/href">https://medium.com/media/81ddd69cee767774dad7b942d686b661/href</a></iframe><h4>Using templates</h4><p>One disadvantage of migrating to the Yaml pipelines is that we lose “task groups”. Task groups, as the name implies, allowed to create a group of tasks that could be used in multiple pipelines and could use variables to set properties. This way, we could keep recipes in those task groups, use them in multiple pipelines and, if a change was needed, we could do it in all the pipelines at once.</p><p>One way to remedy that is by separating our yaml pipeline file into multiple files using templates.</p><p>Templates are nothing complex, they are a simple yaml file that you can add alongside your “normal” yaml pipeline file (or in a subfolder) that will define the “steps” part of a job definition.</p><p>Example of using and defining a pipeline that uses a template</p><pre>azure-pipeline.yml</pre><pre>jobs:<br>- job: demo build<br>  displayName: &#39;Build project&#39;<br>  steps:<br>  - template: build-steps.yml</pre><pre><br>build-steps.yml</pre><pre>steps:<br>- checkout: self<br>  clean: true<br>  lfs: true</pre><pre>- task: DotNetCoreCLI@2<br>  displayName: &#39;NuGet Restore&#39;<br>  inputs:<br>    command: restore<br>    projects: &#39;**/*.csproj&#39;</pre><pre>- task: DotNetCoreCLI@2<br>  displayName: Build all projects<br>  inputs:<br>    projects: &#39;**/*.csproj&#39;<br>    arguments: &#39;--configuration $(buildConfiguration) --no-restore&#39;</pre><p>One approach to share the steps between different projects could be to store the “common” build templates in a repository and use git submodule to include the common templates into all your projects.</p><h4>Versioning</h4><p>For those packages, we use the <a href="https://guides.github.com/introduction/flow/">GitHub flow</a> and do automatic versioning. Simply put, it means that the <em>master</em> branch of the repository is locked and we mandate using a PR to push work to it. Work on the branch are either new features or bug fixes. We do this to continuously improve our code through code reviews.</p><p>When the PR passes and the code is merged to master, we automatically create a beta release. Once we are confident about our version and want to create a new official release, we create a tag on master which triggers a new build and publish the major version.</p><p>To automate the version management, we use <a href="https://gitversion.net/docs/">GitVersion</a>. It is a tool that, from the current state of the currently checked out git branch and a set of overridable rules, can calculate a version number. It can be installed from the <a href="https://marketplace.visualstudio.com/items?itemName=gittools.usegitversion">Visual Studio Marketplace</a>. It adds a GitVersion task that will run the calculation and create a series of environment variables containing the version in different “styles”. Those environment variables can then be used in the build steps or to update the assembly versions.</p><h4>Update the assembly version</h4><p>GitVersion supports updating AssemblyInfo files, but in a very limited way. It doesn’t support modifying the assembly information that are now stored in the “new” csproj files.</p><p>To overcome this issue, we use another DevOps extension called <a href="https://marketplace.visualstudio.com/items?itemName=bleddynrichards.Assembly-Info-Task&amp;targetId=81fedd7b-ac69-4753-8129-f37a56066d21&amp;utm_source=vstsproduct&amp;utm_medium=ExtHubManageList">Assemly Info</a>. It offers two tasks, one for the Net Framework (with AssemblyInfo files) and one for the new Net core / standard project.</p><p>You simply add the task in the pipeline and use GitVersion’s environment variables to use them to modify the file info or assembly info. As you can see in the sample, I’m using the $(GitVersion.AssemblySemVer) variable for VersionNumber and FileVersionNumber but $(GitVersion.InformationalVersion) that contains the SHA1 of the git commit for the informational version.</p><h4>Build the project</h4><p>The build process is very standard, just call nuget restore followed by the build. I’m using wildcards to restore / build all csproj at once.</p><h4>Publish the build binaries</h4><p>Artifacts have two use. First, they will be available to download on the build result page. This allows us to check that the build stage was correctly setup. The second use is that they will be automatically downloaded on the second stage, the deployment stage.</p><p>I’m copying the compiled binaries to the artifact staging directory. This is completely optional, you could also ignore it and only publish the nupkg file. I like to copy the binaries in the artifact because it helps making sure that the builds work as expected.</p><h4>Create the nuget package and publish</h4><p>Finally, I’m using the .Net Core CLI task to pack the project and copy it directly into a subdirectory of the artifacts staging directory. I’m also setting the versionEnvVar parameter to the GitVersion.NuGetVersionV2 environment variable have a version compatible with the nuget versioning scheme.</p><p>Finally, I’m using the PublishBuildArtifacts step to publish the two folders as artifacts.</p><h4>Deploy</h4><p>The deployment stage is configured to trigger only if the build_pack stage has succeeded and if the commit triggering the pipeline is on master branch.</p><p>It has one single step, a NuGetCommand using thepush command that pushes all .nupkg files into an internal feed.</p><p>Obviously, for public NuGet package you might want to add other steps or other deployment, for example based on a tag to deploy on the public NuGet feed.</p><h3>Conclusion</h3><p>As you can see, it’s fairly easy to automate the deployment of a NuGet package using Azure DevOps pipeline. Using GitVersion allows to also automate the version number. Using template is also a good way to simplify reusing the same part of the pipeline in multiple projects.</p><p>In the next part, we’ll see a recipe for building a desktop application, creating and deploying an installer using WiX.</p><p>If you have questions, remarks, or improvement feel free to add a comment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=578b5eda3e76" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/azure-devops-yaml-pipeline-recipes-578b5eda3e76">Azure DevOps yaml Pipeline Recipes</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C# IEnumerator is hiding things]]></title>
            <link>https://itnext.io/c-ienumerator-is-hiding-things-d367dc31ebe5?source=rss-679faef91f2b------2</link>
            <guid isPermaLink="false">https://medium.com/p/d367dc31ebe5</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Xavier Hahn]]></dc:creator>
            <pubDate>Thu, 06 Feb 2020 20:07:19 GMT</pubDate>
            <atom:updated>2020-02-14T22:37:58.413Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*a9zS5BJKHtOdC8pfwzNiPA.jpeg" /></figure><p>Today, a colleague was working on serializing a rather complex part of one of our legacy application and went a bit overboard, letting the serializer go into uncharted territory. The app crashed with a nice <em>StackOverflowException</em> on an old school <em>IEnumerator</em> implementation.</p><p>The crash happened on the <em>Current</em> implementation which looked something like that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1743845c1db45d4676c1eada1398586d/href">https://medium.com/media/1743845c1db45d4676c1eada1398586d/href</a></iframe><p>What happened was pretty obvious, the original developer thought it would call the other implementation of the <em>Current</em> property but it actually called itself recursively until the stack was full and the app crashed. It was pretty easy to correct by moving the explicit implementation to the <em>Current</em> that returns <em>object</em> and keeping the rest of the code as is.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f2cc4159ba0e3fe34fc502fa0ed4b94/href">https://medium.com/media/4f2cc4159ba0e3fe34fc502fa0ed4b94/href</a></iframe><p>The code was now bug free (well that part anyway), and all was well in the world… except… all this got me thinking. How the hell did we get into that situation, and how was the app not crashing, this <em>Enumerator</em> being used rather often on some part of the code. More importantly, when does each version of the <em>Current</em> property gets called and why.</p><h3>The non generic IEnumerator and the generic one</h3><p><em>IEnumerator</em> and its generic counterpart, <em>IEnumerator&lt;T&gt;</em> are rather old interfaces, one being there since .Net 1, the other since .Net 2 (with the arrival of generics). The generic inherits from the non generic one, <em>IEnumerator</em> being still here probably mostly for compatibility reason. Here’s their definition.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/beed4f4f0323d3d0604a884c01a3493b/href">https://medium.com/media/beed4f4f0323d3d0604a884c01a3493b/href</a></iframe><p>They are quite simple and self explanatory. The only interesting thing is that they both have a read only property called <em>Current</em> but with different return type. C# doesn’t let you override a method by changing the return type, so… how is that possible?</p><p>It’s possible because in that case, it’s not overriding, it’s <em>hiding</em>. If you look at the <em>Current</em> defined of the generic <em>IEnumerator</em>, you’ll see the<em> new</em> keyword. Here’s what the <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/new-modifier">C# reference</a> has to say about the <em>new</em> modifier.</p><blockquote>When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base class version. Although you can hide members without using the new modifier, you get a compiler warning. If you use new to explicitly hide a member, it suppresses this warning.</blockquote><p>What does it actually mean? In what is hiding different than overriding? Let’s try a small example.</p><h4>Inheritance</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14f3b79da91690499dfa1b45865908d7/href">https://medium.com/media/14f3b79da91690499dfa1b45865908d7/href</a></iframe><p>If you run this sample, you’ll obviously get an output “woof woof”. It’s the base of object oriented programming. It doesn’t matter that your <em>Dog</em> is hiding inside an <em>Animal</em> variable, it’s still a dog and it will bark.</p><h4>Hiding</h4><p>Now, the more confusing hiding through the <em>new</em> keyword.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6eeddb90cc36606e6cb006af8a93443a/href">https://medium.com/media/6eeddb90cc36606e6cb006af8a93443a/href</a></iframe><p>Now the output is different based on the <strong>variable type</strong>. Even though our object is still a dog, if the variable it’s in is an <em>Animal</em>, the <em>Animal </em>implementation of the <em>Shout</em> method is called. When it’s in a <em>Dog</em> variable, it’s the <em>Dog</em> implementation that is used because it’s hiding the <em>Animal</em> one… ouch. One important thing to grasp is that we’re using <strong>hiding</strong> for a reason, in a <em>Dog</em> object, <strong>both</strong> <em>Shout</em> methods exist! You can actually access it by prefixing with the <em>Animal</em> class name.</p><p>This goes without saying that this is incredibly confusing and should be used with extreme precaution, you should have a really good reason to do it, like for the <em>IEnumerator&lt;T&gt;</em> interface.</p><h3>Implementing the generic IEnumerator&lt;T&gt;</h3><p>Back to our <em>IEnumerator… </em>What does happen when we actually implement the <em>IEnumerator&lt;T&gt;</em> interface?</p><p>Since the <em>IEnumerator.Current</em> property only <strong><em>hides</em> </strong>the <em>IEnumerator&lt;T&gt;.Current </em>property, both exist. This means that to implement both interfaces (mandatory since the generic inherits from the non generic), we need to implement the <strong>two versions</strong> of the <em>Current</em> property, one with a return type <em>object</em> and one with <em>T</em>.</p><p>To do this, we need to use <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/explicit-interface-implementation">explicit interface implementation</a>. By adding the name of the interface before the name of the property, we can define a property that implements the <em>IEnumerator </em>interface and another one that implements the <em>IEnumerator&lt;T&gt;</em> interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2e9182562c6b40beec30959141613ad/href">https://medium.com/media/d2e9182562c6b40beec30959141613ad/href</a></iframe><p>In this example, you can see that I have implemented three versions of the <em>Current</em> property. The first one doesn’t implement an interface, it belongs only to the <em>ConfusingEnumerator</em> class and it’s the only one available if you have a variable of type <em>ConfusingEnumerator</em>.</p><p>The second explicitly implements the <em>IEnumerator&lt;string&gt;</em> interface and the third one the <em>IEnumerator</em> one. The explicit implementations cannot be public. They are visible only through the interfaces.</p><p>As you can see with the execution result, depending on the type of the variable the object is in, the different implementation is called. This allows us to answer to both interfaces.</p><p>Although you can see that it’s possible to have three different implementation of the “same” <em>Current</em> property, it’s probably not how the interface was intended to be used, and the new keyword in the generic version is here to avoid the impossibility to override with different return type.</p><p>The “standard” implementation would be to implicitly implement the generic <em>Current</em> property and explicitly implement non generic, simply calling the implicit one, like so.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6cf8565211a5a0678a2644c40f4f6664/href">https://medium.com/media/6cf8565211a5a0678a2644c40f4f6664/href</a></iframe><h3>Conclusion</h3><p>I hope I could make the concept of hiding and explicit implementation of interface a bit less confusing and could explain the curious case of the generic <em>IEnumerator.</em></p><p>It’s by far not the only example of this generic interface that inherit from the generic one that exist in the .Net framework and it’s easy to implement them incorrectly.</p><p>Feel free to add remarks or ask questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d367dc31ebe5" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/c-ienumerator-is-hiding-things-d367dc31ebe5">C# IEnumerator is hiding things</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transform an image into a Quadtree]]></title>
            <link>https://medium.com/analytics-vidhya/transform-an-image-into-a-quadtree-39b3aa6e019a?source=rss-679faef91f2b------2</link>
            <guid isPermaLink="false">https://medium.com/p/39b3aa6e019a</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[numpy]]></category>
            <category><![CDATA[quadtree]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[matplotlib]]></category>
            <dc:creator><![CDATA[Xavier Hahn]]></dc:creator>
            <pubDate>Thu, 16 Jan 2020 22:49:23 GMT</pubDate>
            <atom:updated>2020-01-22T08:48:28.140Z</atom:updated>
            <content:encoded><![CDATA[<p>A fun experiment to discover a data structure</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AYsiqr93y5k8Kf8vjY39FQ.jpeg" /><figcaption><a href="https://www.pexels.com/photo/dawn-nature-tree-romania-56875/">Green Leafed Tree on Mountain</a></figcaption></figure><p>I recently discovered Quadtree and Octree data structures and I had to find an idea to experiment with them. The inspiration came from this nice gif on the <a href="https://en.wikipedia.org/wiki/Quadtree#QuadTree_class">Quadtree Wikipedia pag</a>e.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*szzDMHXHMUNA0-hnCG9i4w.gif" /><figcaption>Quadtree compression of an image. source: Wikipedia <a href="https://en.wikipedia.org/wiki/Quadtree#/media/File:Quadtree_compression_of_an_image.gif">https://en.wikipedia.org/wiki/Quadtree#/media/File:Quadtree_compression_of_an_image.gif</a></figcaption></figure><p>I thought it was a cool little experiment to try and recreate it. I fired up Jupyter notebook and started to experiment with Python, numpy, and matlplotlib.</p><h3>Quadtree</h3><p>A quadtree is a special tree that defines each node as having four children. They are very useful to subdivide a 2D space by splitting it recursively in four quadrants. The <a href="https://en.wikipedia.org/wiki/Quadtree#Polygonal_map_(PM)_quadtree">Wikipedia page</a> on the subject is very complete and offers a few examples of its use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/0*-YhAN5gbvCyDWlWD.png" /><figcaption>Store a simple image into a quadtree (<a href="https://en.wikipedia.org/wiki/File:Quad_tree_bitmap.svg">wikipedia</a>)</figcaption></figure><h3>Load the image</h3><p>Obviously, the first thing to do is to load the image and transform it into something I’ll be able to work with. Matplotlib has a cool method called <a href="https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imread.html"><em>imread</em></a> that allows to load a file and transform it into a <em>numpy.array</em> object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5766244833d6e9ac5a8529373aef5e49/href">https://medium.com/media/5766244833d6e9ac5a8529373aef5e49/href</a></iframe><p>The image is a photo of Split, Croatia from user yokok on <a href="https://www.flickr.com/photos/yokok/7034360407">Flickr</a>. The image is loaded into a three dimensional numpy array representing the height, width, and RGB value of each pixel. Using matplotlib you can simply call <em>plt.imshow</em> to display the image.</p><h3>Split the image in 4</h3><p>The next step in the algorithm is to be able to split the image into four “equal” parts. Of course, only images with a pair number of pixels on the height or width can be split into 4 exactly equal parts, but that doesn’t really matter for our algorithm. So let’s split Split.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2aea941f7ac593d1daa17eb1a8700424/href">https://medium.com/media/2aea941f7ac593d1daa17eb1a8700424/href</a></iframe><p>As you can see, splitting the image into 4 isn’t too complicated, it uses <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.array_split.html"><em>array_split</em></a> once in one direction, then on the other. You have to use a trick of using map to keep the correct dimensions.</p><h3>Reconstruct the image back</h3><p>When we’ll read the image from the quadtree, we’ll have to reconstruct a full image from the four split children.</p><p>We can do this easily by using numpy’s <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html">concatenate</a> method it takes an <em>axis</em> parameter to determine on which axis to concatenate the array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83443faa8c5bcb291837fd2f33c1604b/href">https://medium.com/media/83443faa8c5bcb291837fd2f33c1604b/href</a></iframe><h3>Calculate the mean color</h3><p>Final tool in our toolbox that we need to construct our quadtree, for each node we’ll calculate the mean color. This color will be saved in the node and will be used to display intermediary images.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e5eeb3fa4827d4b704ac3a20d90e59d/href">https://medium.com/media/2e5eeb3fa4827d4b704ac3a20d90e59d/href</a></iframe><p>Again, numpy is a great help, simply configuring the <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html"><em>mean</em></a> method by defining which axis to use allows us to get the mean color.</p><h3>Quadtree data structure</h3><p>Finally, we get to the core of the algorithm. I define a Quadtree class and an <em>insert</em> method that allows to insert an image into the Quadtree and a <em>show_image</em> that displays the image at a specific level.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/233c9e1803425d8a4a0ee68086b80cfe/href">https://medium.com/media/233c9e1803425d8a4a0ee68086b80cfe/href</a></iframe><p>The quadtree structure is a recursive structure with each quadtree containing four other quatree (or none for the leaves).</p><h4>Insert</h4><p>The insert method is a method that will be called recursively. A level is passed, starting at 0 for the first node and adding one on each level. It allows us to know when traversing the tree at which level we are.</p><p>We save this level as well as the mean color calculated for the current level (for the whole image received). Finally, we save the resolution in order to recreate the image with the same resolution as the one passed.</p><p>We check if all the pixels in the image are equal. If that’s the case, we stop here, we’ll be able to recreate the image at the current level by recreating an image from the resolution with the mean color. We save that it’s a leave by setting a flag.</p><p>If we have different pixel colors, we create four child quadtree and insert the respective quarter to each tree by calling the insert method passing the current level + 1.</p><h4>Show image</h4><p>To reconstruct the image at a specific level, we simply need traverse the quadtree, concatenating each split back into an image that is sent back to the parent caller.</p><p>The stop condition of the recursive method call is that the current node level is the same as the one passed in the level parameter, or the current node is a leaf (<em>final = true</em>).</p><p>If that’s the case, we create an image of the stored resolution with the mean color.</p><h3>Conclusion</h3><p>This was a fun little project that allowed me to understand better how quadtree are built and used. It’s definitely not the fastest method to construct it neither the better way to store the quadtree. There seems to be solutions to store quadtrees in a “flat” manner (in an array) that is very efficient, but I haven’t found an algorithm or samples yet. I’ll probably study that further.</p><p>You can find the full Jupyter notebook on my GitHub at <a href="https://github.com/Gimly/image-quadtree/blob/master/Transform_image_into_quadtree.ipynb">https://github.com/Gimly/image-quadtree/blob/master/Transform_image_into_quadtree.ipynb</a></p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39b3aa6e019a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/transform-an-image-into-a-quadtree-39b3aa6e019a">Transform an image into a Quadtree</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Adding authorization to Asp.Net Core app using Keycloak]]></title>
            <link>https://medium.com/@xavier.hahn/adding-authorization-to-asp-net-core-app-using-keycloak-c6c96ee0e655?source=rss-679faef91f2b------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6c96ee0e655</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[aspnetcore]]></category>
            <category><![CDATA[keycloak]]></category>
            <category><![CDATA[aspnet]]></category>
            <dc:creator><![CDATA[Xavier Hahn]]></dc:creator>
            <pubDate>Mon, 13 Nov 2017 21:46:13 GMT</pubDate>
            <atom:updated>2017-11-13T21:55:28.993Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using Keycloak’s roles functionalities to setup authorization in Asp.Net Core web apps and APIs.</h4><p>This is a follow-up to <a href="https://medium.com/@xavier.hahn/asp-net-core-angular-openid-connect-using-keycloak-6437948c008">my previous story</a> where I explained how to setup an Asp.Net Core app and an Angular app to authenticate users through a Keycloak server using OpenID Connect. In this article, we will go a step further and add authorization using Keycloak’s built-in “roles”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sn7WK08ON1AnvjWA-JkzMw.png" /><figcaption>Authorization (<a href="https://pixabay.com/p-2688911/?no_redirect">geralt from pixabay</a>)</figcaption></figure><h3>Setting up Keycloak</h3><p>I won’t go into all the details about how to setup a Keycloak server in this article as this was covered extensively in the previous article. Therefore, you should have a Keycloak server running, with a few users setup and a client app configured for your Asp.Net Core Api.</p><h4>Keycloak’s roles</h4><p>There are two types of roles in Keycloak, <em>Realm roles</em> and <em>Application roles. </em>Realm roles are roles that are setup at “Realm” level, this is useful if you need to “share” roles across applications boundaries. For example, if you have roles that are global to the company, that’s where you’ll want to add them.</p><p>To add realm roles, simply connect to Keycloak’s administrative console, select the role for which you want to setup roles and click on the <em>Roles</em> tab. It will list the current roles available in the realm, click on the <em>Add role</em> button. Add the role name, select save and you will have your new role.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WZdl2sFl_-B6Yjxef-aArg.png" /><figcaption>Adding a new role</figcaption></figure><p>Clients roles, are roles that are available only for one specific client application. This is a very likely scenario, as users that are administrators for one application might be a simple users for another. To add client roles, you need to select the <em>Clients</em> menu, then select the client for which you want to add roles. Select the client that we have created for our application in the previous article. Then, select the <em>Roles</em> tab, you should see an empty list. Click <em>Add role </em>and add a role named <em>Administrators.</em> When you save, you should get to the newly created role details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t1xAR-hPpwJlAMpS_zswNw.png" /></figure><h4>Adding a role to a user</h4><p>Now, let’s get to the <em>Users</em> menu and map a role to a user. In the users list, search for a user and click on its <em>id</em> to get to its details page. From there, select the <em>Role mapping</em> tab and you should see the realm role that you created at the top. Select the client in the drop down list and you should see the <em>Administrators</em> role that you created earlier.</p><p>Add both roles to the user.</p><h4>Adding the role to the access and id tokens</h4><p>By default, Keycloak doesn’t publish roles to the id and access tokens, but we will need them to authorize our users in the Asp.Net Core API. This is a configuration that we need to do in Keycloak’s client options. Go back to the .Net Core App client options page and select the <em>Mappers</em> tab you should see a number of built-in mappers.</p><p>Click on the <em>Create</em> button and fill-in the form as follow:</p><ul><li><strong>Name:</strong> can be anything you want, I’m using “User realm roles”</li><li><strong>Mapper Type:</strong> User Realm Roles</li><li><strong>Multivalued:</strong> true</li><li><strong>Token Claim Name:</strong> again, anything you want, I use user_realm_roles</li><li><strong>Claim JSON Type:</strong> string</li><li><strong>Add to ID token, add to access token, add to user info:</strong> true</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fpmQjBkOK_Y23d3eExjHVw.png" /></figure><p>Create another mapper, this time for the client role.</p><ul><li><strong>Name:</strong> can be anything you want, I’m using “User client roles”</li><li><strong>Mapper Type:</strong> User Client Roles</li><li><strong>Client ID:</strong> the client id of your application</li><li><strong>Multivalued:</strong> true</li><li><strong>Token Claim Name:</strong> again, anything you want, I use user_roles</li><li><strong>Claim JSON Type:</strong> string</li><li><strong>Add to ID token, add to access token, add to user info:</strong> true</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EmRnOlwOW3CD7QEPP9ESbw.png" /></figure><blockquote>Remark: here I’m creating both a client and a realm role, but this is not mandatory, depending on your application, you could have either only client roles, only realm roles, or both.</blockquote><h4>Testing the configuration</h4><p>We can do a little test using Postman and <a href="http://jwt.io/">jwt.io</a> to check that our mappings are correctly sent in the claims.</p><p>Open Postman, select the <em>Authorization</em> tab of any request and click on the <em>Get New Access Token</em> button. Enter a token name, and select <em>Authorization code</em> for <em>Grant Type</em>. Fill in the rest of the form as follow:</p><ul><li><strong>Callback URL:</strong> Anything, but make sure that you have added it to the list of allowed URLs for the client in Keycloak</li><li><strong>Auth URL:</strong> <a href="http://localhost:8080/auth/realms/master/protocol/openid-connect/auth">http://localhost:8080/auth/realms/master/protocol/openid-connect/auth</a></li><li><strong>Access Token URL:</strong> <a href="http://localhost:8080/auth/realms/master/protocol/openid-connect/token">http://localhost:8080/auth/realms/master/protocol/openid-connect/token</a></li><li><strong>Client ID:</strong> The client ID of your application</li></ul><p>The rest of the fields can be left as default. Click <em>Request Token</em> and authenticate with the user for which you added the roles. This will save your access token. Copy it and paste it to <a href="http://jwt.io/">jwt.io</a>. This will decrypt the token, allowing you to inspect the content. You should have both the <em>user_realm_roles</em> and <em>user_roles</em>. Great, we’re now ready to add the authorization to the Asp.Net Core Web API.</p><h3>Adding authorization to Asp.Net Core Web API</h3><p>We’ll start from the .Net Core solution as we left it at the end of the previous article. We’re going to use Asp.Net Core’s built-in <a href="https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles">Role Based Authorization</a>.</p><p>We’re going to transform the claim that we have created into a role policy. This is done through the IServiceCollection AddAuthorization extension method.</p><p>Open the <em>Startup.cs</em> file and add the following lines after the AddAuthentication call in the ConfigureServices method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1afba8bdd2a75b6ae8cd90f66a636c18/href">https://medium.com/media/1afba8bdd2a75b6ae8cd90f66a636c18/href</a></iframe><p>Now, we can simply add the authorization attribute with the role property set to Administrators to the controller classes or methods that we want to protect.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd65751f7cb3c2205dcba814c64b4cc2/href">https://medium.com/media/bd65751f7cb3c2205dcba814c64b4cc2/href</a></iframe><p>Run the project and use Postman to test your application with the authorization working correctly. Only users with the administrators role should be allowed to access methods.</p><blockquote>Remark: As you can see, the RequireClaim doesn’t seem to understand that the claim is an array, and treats it as a string instead, which is why you need the [] characters. This means that it won’t really work for cases where the user has more than one role. I don’t have a definitive answer as how to fix that issue. The only option I see for now is <a href="https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies">implementing a custom policy-based authorization</a>.</blockquote><h3>Conclusion</h3><p>As you can see, adding authorization to an Asp.Net Core Web API from Keycloak’s roles is relatively easy. Since Keycloak allows both client and realms-type roles, it offers a wide range of possibilities.</p><p>I’ve updated the <a href="https://github.com/Gimly/SampleNetCoreAngularKeycloak">previous article’s source code</a> to add the authorization example from this article.</p><p>Thanks for reading this article, feel free to add your comments, remarks or question and to share this article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6c96ee0e655" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Continuously deploy Meteor apps to Azure]]></title>
            <link>https://medium.com/@xavier.hahn/continuously-deploy-meteor-apps-to-azure-e1d3cda2a618?source=rss-679faef91f2b------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1d3cda2a618</guid>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[meteor]]></category>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[bitbucket]]></category>
            <dc:creator><![CDATA[Xavier Hahn]]></dc:creator>
            <pubDate>Fri, 03 Nov 2017 17:23:42 GMT</pubDate>
            <atom:updated>2017-11-03T17:23:42.290Z</atom:updated>
            <content:encoded><![CDATA[<h4>Setting up a continuous deployment of a Meteor app to Azure App Services with the help of Docker and Bitbucket Pipelines</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*tfEZslbOgaZk8XLI.jpg" /><figcaption>A Meteor being deployed to Azure (<a href="https://pixabay.com/fr/users/RafaelMousob-1260223/">Pixabay, RafaelMousob</a>)</figcaption></figure><p>Recently, I helped colleagues who are developing a <a href="https://www.meteor.com/">Meteor</a> application deploy their application to <a href="https://azure.microsoft.com/">Azure</a>. Our approach was that we wanted to have everything as automated as possible. The team is using <a href="https://bitbucket.org/">Bitbucket</a> to host their source and we wanted to deploy to Azure. The goal of this article is to get you through the whole process with a sample project.</p><h4>Creating a Meteor sample application</h4><p>Meteor is a relatively new (started in 2012, first stable version in 2017) web client and server JavaScript framework aimed at rapid prototyping. It can produce cross platform applications.</p><p>First, you’ll need to install the framework, head to <a href="https://www.meteor.com/install">https://www.meteor.com/install</a> and follow the instructions there. It should install a command-line interface (CLI) called <em>meteor</em>. We can use this CLI to scaffold one of the example applications that are offered by the Meteor team. I’ll use the <em>todo</em> sample, but you can use any application. Run the command meteor create --list will show you the list of all available samples. Let’s create the <em>todo</em> sample.</p><pre>meteor create todo</pre><p>You can then run the app to check that everything is working as expected. Beware, this will take some time as meteor is going to download all its packages and the runtime needed. Once the process is finished, open the browser to <a href="http://localhost:3000/">http://localhost:3000</a> to see the app, and test it.</p><pre>cd ./todo<br>meteor</pre><h4>Creating the docker image</h4><p>There are a few options to create ready to deploy Docker images for Meteor applications. The <a href="https://guide.meteor.com/deployment.html#docker">Meteor documentation</a> recommends 4 different packages, some which haven’t been maintained for a while. I decided to go with <a href="https://github.com/jshimko/meteor-launchpad">jshimko/meteor-launchpad</a> because it seemed to be the one whose repository was the most active.</p><p>We need to create a Dockerfile to create the Docker image. Dockerfiles are simple text document that contains all the commands a user would call to create the Docker image. It allows to automate the creation of the Docker image. In our case, the Dockerfile will be very easy. Create a file called Dockerfile (without extension) at the root of the source code and add the following line</p><pre>FROM jshimko/meteor-launchpad:latest</pre><p>Yep, that’s it. Dockerfiles are hierarchical and this one simply does everything that his parent is doing. If you are curious, check how the meteor launchpad image has been created, it’s very instructive. Basically it makes use of the ONBUILD Docker command that tells the image to run a command when he “child” image is being built. Therefore, just by adding that FROM line, the Docker build process will copy the source, code, run NPM, download meteor, and do all the other things needed to run the image. Once the image is built, it will run very quickly because everything will be in place and ready to run.</p><p>Before we run the image build, an important step, especially if you ran your app locally, is to create a .dockerignore file. If you don’t, all your local files, included the local builds, will be copied to the Docker image and will be used in the build process which can cause some really annoying to debug errors. Simply create a file called .dockerignore and add all the files that are not used to create your application. The most important files to ignore are the node_modules folder and the .meteor/local folder. A good approach is to ignore all the files that are ignored by the .gitignore file.</p><pre>node_modules/<br>.meteor/local/</pre><p>Now, we’re going to try to create the docker image locally. Since we have our Dockerfile ready, it’s just a simple call to docker build, setting a name for our newly created Docker image.</p><pre>docker build -t todo_test .</pre><p>This will build the image and save it with atodo_test name. Wait for the whole process to complete (it can take around 10 minutes). You can then test the application by running it and checking that it’s running correctly in your browser.</p><pre>docker run -d -p 80:3000 todo_test</pre><p>You can see that I’m linking the container’s port 3000 (the default meteor port) to the host’s port 80. To access the application, open your browser and go to <a href="http://localhost:80.">http://localhost:80.</a></p><p>Great, this means that we have our image ready to be published! But, that’s not exactly what we want, we’d like to have this annoying task automated by the continuous deployment, we’ll get to that, but first we need to have a place to store our image.</p><h4>Creating an Azure container registry account</h4><p>Since we are going to deploy our application to Azure, it makes sense to use Azure’s own container registry to store our images. This means that we need to setup this container registry before we start to build our continuous deployment.</p><p>Open the <a href="https://portal.azure.com/">Azure portal,</a> click <em>More services</em> and search for <em>Container registries.</em> Click on the <em>Add</em> button and fill-in the required fields. It’s all pretty standard settings, you can keep the SKU to Basic.</p><p>Once the registry has been created, open it. You will have the <em>Login server</em> information, which should be <em>nameOfYourRegistry.azurecr.io</em> keep that information somewhere, we’ll need them later. Open the <em>Access keys </em>tab and store the username and password.</p><p>If you want (this isn’t necessary) you can try to push an image to the registry from Docker’s command line interface. The commands are as follow:</p><pre>docker login yourRepoName.azurecr.io --username yourUsername --password yourPassword</pre><pre>docker tag todo_test yourRepoName.azurecr.io/todo_test:latest</pre><pre>docker push yourRepoName.azurecr.io/todo_test:latest</pre><p>This should upload the previously created image called <em>todo_test</em> to the container registry. Check that everything is working correctly before going to the next step.</p><h4>Setting up continuous deployment using Bitbucket Pipelines</h4><p>First, we need to get our code to Bitbucket, so we need a git repository. That’s probably the first thing that we should have done. But it’s OK, let’s do it now.</p><pre>git init<br>git add --all<br>git commit -m &quot;First commit, add existing project&quot;</pre><p>Now, login to Bitbucket, and create a new repository for this project. Then simply set the origin url to your newly created repository’s URL and push.</p><pre>git remote add &lt;your repo URL&gt;<br>git push</pre><p>All done? Great, now that we have the code in the repository, we can setup Pipelines. Pipelines is Bitbucket’s continuous integration / deployment tool. It’s based on Docker. What it will do is start a Docker image that you selected when a trigger is executed on your repository, for example when you have published code to the main branch. Then, once the container is running, it will execute a set of instructions on it which are used to deploy your code. All the process can be tracked by an UI on Bitbucket’s website.</p><p>Pipelines expects to have a file called bitbucket-pipelines.yml at the root of your repository. This yaml file will contain all the pipelines settings needed to create and deploy the image. Let’s create a Pipelines config file.</p><pre># enable Docker for your repository<br>options:<br>  docker: true</pre><pre>pipelines:<br>  branches:<br>    master:<br>      - step:<br>          script:<br>            - export IMAGE_NAME=demo.azurecr.io/todo-app:$BITBUCKET_COMMIT</pre><pre># build the Docker image (this will use the Dockerfile in the root of the repo)<br>            - docker build -t $IMAGE_NAME .<br>            # tag the image with the latest flag<br>            - docker tag $IMAGE_NAME yourRepoName.azurecr.io/todo-app:latest<br>            # authenticate with the Azure container registry<br>            - docker login test.azurecr.io --username $AZURE_CR_USERNAME --password $AZURE_CR_PASSWORD<br>            # push the new Docker image to the Docker registry<br>            - docker push $IMAGE_NAME<br>            - docker push test.azurecr.io/todo-app:latest</pre><p>The script is relatively straightforward, we’re doing all the steps we did manually previously with the little difference that we push the image twice. Once with the tag with the commit id, and once with latest. The goal is that the image in production will always link to latest, but we want to keep an history of all the images, so we push also the tag with commit id that is unique.</p><p>You can also see that there are a few $VARIABLES. $BITBUCKET_COMMIT is automatically replaced by Pipelines with the id of the commit. The two variables $AZURE_CR_USERNAME and $ AZURE_CR_PASSWORD are to be set in Bitbucket’s pipeline’s settings page. This is to secure those secrets so that they are not in scripts in the repository itself.</p><p>To set those, in Bitbucket, go to your repository, select <em>Settings</em>, then <em>Pipelines</em>/<em>Environment variables</em>. In this menu, add the two settings. For the password, don’t forget to check the <em>Secret</em> checkbox so that it doesn’t display when you go to the settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*04faiaYhVfqch9PUU9x-Zw.png" /></figure><p>While we’re in those settings already, go to the <em>Pipelines/Settings</em> page and enable pipelines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*UmA23kqbzHnoubXH9D1geQ.png" /></figure><p>Great, now commit your changes and push them to Bitbucket, this should trigger a Pipelines execution.</p><p>In Bitbucket, go back to your repository, select the <em>Pipelines</em> tab, and you should be greeted with something like that: Success.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r2Iq-wHNi4W1-lDDSIuuIQ.png" /></figure><p>Great, now go to Azure container registry, select the registry that you created previously and go to <em>Repositories</em>. You should see the repository called <em>todo-app.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m7vszFdlhIow7mSD-2qrAw.png" /></figure><p>We’re nearly there, now that the image is in the registry, we need to run it.</p><h4>Creating an App Services running the container</h4><p>There are a few ways to run this container on Azure, you could create a container service and run the container in Kubernetes for example. But here, since we have just one “monolithic” app, we will simply run it on App Services.</p><p>Go to the <em>App Services</em> tab in Azure and click add. Search for <em>Web app for Containers, </em>select the item of the same name and click create. Add an app name, select the subscription, resource group and plan, and select <em>Configure container. </em>Select <em>Azure Container Registry</em> in the window that is displayed and fill in the drop down lists with your registry and image name. Make sure to select the <em>latest</em> tag and click <em>OK</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/1*iaSjPT3byx-8CZNCNEHFZg.png" /></figure><p>Click <em>Create</em> and wait for your app to be created.</p><h4>Configuring App Services</h4><p>Now that the app is created, we need to configure a few environment variables to have the Meteor app work correctly. We need to set the MongoDB URL and tell Azure on which port the app is running. All this is done through environment variables. Go to your newly created app service, and select <em>Application settings.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JoEhG6Jk6VStZz4dZ_bV3g.png" /></figure><p>In the <em>App settings</em>, add the MONGO_URL and MONGO_OPLOG_URL to be the URL of your MongoDB and MongoDB OPLog.</p><blockquote>Talking about MongoDB, there are a few options to get MongoDB running in Azure. Cosmos DB for example is fully compatible with MongoDB and we could test that Meteor apps work correctly with it. Unfortunately, it’s not compatible with OPLog though. The option we ended up choosing was to use MongoDB Atlas, deployed on the same Azure datacenter that the application. It’s relatively cheap and it works like a charm.</blockquote><p>The other very important settings are the WEBSITES_PORT that tells Azure on which port the app is served, and DOCKER_ENABLE_CI to have continuous integration running.</p><p>Now, restart your app and navigate to it (the URL is in the app’s overview). It should be running correctly.</p><h4>Setup a container registry’s webhook</h4><p>There is one final step that we need to do in order to have the full continuous delivery in place. It is activating a web hook on the container registry to tell the app services that a new container image has been pushed.</p><p>First, you’ll need to get your publishing user name and password. In your app service overview, click on the <em>Get publish profile</em> button. It will download an XML file, get the information from the tag with the <em>profileName </em>called <em>Web deploy.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/0*KqSgMiEEOsCLE8UZ.png" /></figure><p>Go back to your container registry, select the <em>Webhooks</em> tab, and click <em>Add</em> and fill-in the form. It’s relatively straightforward, except for the <em>Service URI</em>. The service URI is in the form:</p><pre>https://&lt;publishingusername&gt;:&lt;publishingpwd&gt;@&lt;sitename&gt;.scm.azurewebsites.net/docker/hook</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*Iopv55Uz_nHYicto7Ieh6A.png" /></figure><p><em>publishingusername</em> should be took from the attribute <em>userName</em> of the publishing XML’s web deploy tag, <strong>with the starting $ sign</strong>. <em>publishingpassword</em> is took from the same element’s <em>userPWD</em> attribute. <em>siteName</em> is simply your app service site name (todo-app for example).</p><p>If you have an issue with that step, <a href="https://docs.microsoft.com/en-us/azure/app-service/containers/app-service-linux-ci-cd">check the official documentation there</a>.</p><p>Create the web hook and check that it pings. Now, try to change something in your app, push it to Bitbucket and see all the process running and watch your app changed in production.</p><h3>Conclusion</h3><p>Even though this element is relatively complex to get working, especially the part on Azure which has a few elements in preview that are probably going to be made easier in the future, it make the following deployment on the application something that you never think about, which is pretty awesome.</p><p>To go further, I would recommend to setup <em>slots</em> in the app services, this way you can get a staging environment and a production and swap both without down time for your app.</p><p>Feel free to comment if you have remarks or questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1d3cda2a618" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ASP.Net Core & Angular OpenID Connect using Keycloak]]></title>
            <link>https://medium.com/@xavier.hahn/asp-net-core-angular-openid-connect-using-keycloak-6437948c008?source=rss-679faef91f2b------2</link>
            <guid isPermaLink="false">https://medium.com/p/6437948c008</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[keycloak]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[aspnetcore]]></category>
            <dc:creator><![CDATA[Xavier Hahn]]></dc:creator>
            <pubDate>Tue, 26 Sep 2017 20:13:14 GMT</pubDate>
            <atom:updated>2018-02-02T10:55:14.344Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to setup user’s authentication for an ASP.Net Core web API and an Angular front end using Keycloak server for user federation and OpenID Connect.</h4><h3>Keycloak</h3><p><a href="http://www.keycloak.org/">Keycloak</a> is an open source identity provider owned by Red Hat. It allows to easily add authentication to any application and offers very interesting features such as user federation, identity brokering and social login. In the .Net ecosystem, one of its “competitor” would be <a href="https://github.com/identityserver">Identity Server</a> or <a href="https://github.com/openiddict/openiddict-core">OpenIddict</a> with <a href="https://www.asp.net/identity">Asp.Net Identity</a>.</p><p>The main difference between Keycloak and the two other mentioned solution is that it’s a standalone product and not a library. This means that it can run “on its own”. It’s good if you have an ecosystem of application (maybe built with different technologies) and don’t want to make one of the application the “master” that does all the user management. It’s also a bit more complete, with a fully fledged management user interface and support for multi tenancy.</p><p>My goal in this article is to describe how to install a simple instance of Keycloak, secure the API side of an application developed with ASP.Net Core WebApi and secure an Angular client side application that will use the WebApi as a backend.</p><p>Enough talking, let’s get started</p><h3>Installing Keycloak</h3><p>Keycloak has been built with Java on top of the Wildfly application server. There is a quite long installation procedure described in <a href="https://keycloak.gitbooks.io/documentation/server_installation/index.html">the documentation</a>. In our case, we want to have a simple test server to be able to develop our sample application and will do so using Docker.</p><p>I won’t explain here how to install docker, it’s pretty straightforward, head to <a href="https://www.docker.com/">the docker homepage</a> if you need guidance. I’ll just assume you already have docker installed on your machine and it’s working correctly.</p><p>There are quite a few docker images available on Docker Hub. We’re going to get the <a href="https://store.docker.com/community/images/jboss/keycloak">official “standard” package</a> that is basically a ready to use Keycloak installation, complete with the database and everything. You can pass a couple of environment variables to directly create the admin account because none is created by default.</p><p><em>Remark: I wouldn’t recommend using that installation of Keycloak in production as there is no redundancy and no backup of the data. I would recommend that you read on Keycloak’s high availability setup on the official documentation.</em></p><pre>docker run --name keycloak -p 8080:8080 -e KEYCLOAK_USER=&lt;USERNAME&gt; -e KEYCLOAK_PASSWORD=&lt;PASSWORD&gt; jboss/keycloak</pre><p>This will attach Keycloak to the port 8080 of the host machine. Once the machine has started, open your browser and go to localhost:8080. It should display Keycloak’s landing page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TnE6TBdCmp6vss34PT9MCw.png" /><figcaption>Keycloak’s default landing page</figcaption></figure><p>Click the <em>Administration Console</em> link and connect using the username and password combination that you have set in the Docker run command. You should now see Keycloak’s administration page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O2VWVrTTNMbWGd_GLIc7JA.png" /><figcaption>Keycloak’s administration page</figcaption></figure><p>Your Keycloak instance is now functional and ready to be used.</p><h3>Configuring Keycloak</h3><p>I’m not going to dive into too much details on how to configure Keycloak in this article, the only thing that we need to do to secure our application is creating a <em>Client</em>. To do this, click on the <em>Clients</em> option of the left menu. It will display a list of some pre-defined clients. Click on the <em>Create</em> button on the top right. In the displayed menu, select a name, for example <em>demo-app</em> and keep the client protocol as <em>openid-connect.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GPkhxOZkpwH79NwxeBcJQA.png" /><figcaption>The Add Client menu</figcaption></figure><p>Then, make sure to configure a valid redirect URI. To simplify, add * to allow any redirect URI. Enable also the implicit flow by selecting the checkbox.</p><h3>Creating the Asp.Net Core + Angular app</h3><p>To bootstrap the creation of the Asp.Net Core + Angular app, since .Net Core 2.0, there is now a generator that creates a Single Page App with Angular directly from the dotnet command line.</p><pre>mkdir demo-app<br>cd demo-app<br>dotnet new angular<br>npm install</pre><p>It’s important to call the npm install command after the end of the generator, it’s not run automatically.</p><p>Run the application using dotnet run and check that everything is working as expected.</p><h3>Securing the API</h3><p>Now we are going to setup the WebApi side to be able to secure it based on authentication and roles. That part was the most confusing for me, at first I thought that the application itself would have to manage the tokens and I couldn’t wrap my head on how that would be done. After a few trying and error and finally understood that it was Keycloak’s role to generate and manage the authentication Tokens and that the application should simply receive that token and “check” with Keycloak that it is indeed correct and coming from him. Implementing that part is very simple.</p><p>First, install the <a href="https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.JwtBearer/">Microsoft.AspNetCore.Authentication.JwtBearer</a> Nuget package into the solution.</p><pre>dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer</pre><p>Now, open the Startup.cs file and add the following to the ConfigureServices method</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f231f7d0a4d87b7753ab17edb5dbd07/href">https://medium.com/media/0f231f7d0a4d87b7753ab17edb5dbd07/href</a></iframe><p>You should then add the Authority and Audience in your configuration. Authority is the URL of your Keycloak instance and realm, in our case <a href="https://localhost:8080/auth/realms/Master">https://localhost:8080/auth/realms/Master</a>, and the Audience is the name of the Client ID of the client that we created earlier: demo-app. Add this to your configuration file for example, or to environment variables.</p><p>Now, all we have to do to secure an API endpoint is to add an Authorize attribute to the endpoint method. This will add a check that the method was called with an Authentication header containing a valid JWT token. Let’s leverage the existing demo code and add an attribute to the SampleDataController created by the scaffolding.</p><pre>[Authorize]<br>[Route(“api/[controller]”)]<br>public class SampleDataController : Controller {...}</pre><p>You can add the attribute to either the class (like I did here), in which case it will secure all the methods in the controller, or only on a method to secure just an API endpoint.</p><p>Let’s run the application again and try to call the WeatherForecast method of the SampleDataController. I’m using Postman to do the REST API calls.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q8AURdWe5hQIC01Io5iqsw.png" /><figcaption>Result of the call with the [Authorize] attribute (401 Unauthorized response)</figcaption></figure><p>We can continue our test with Postman by requesting Keycloak a token and adding it to our request. Click the Authorization tab (under the “GET” drop down) and select OAuth 2.0. Click the <em>Get new access token</em> button and enter the following options:</p><p>Token name: demo<br>Auth URL: <a href="http://localhost:8080/auth/realms/master/protocol/openid-connect/auth">http://localhost:8080/auth/realms/master/protocol/openid-connect/auth</a><br>Token URL: <a href="http://localhost:8080/auth/realms/master/protocol/openid-connect/token">http://localhost:8080/auth/realms/master/protocol/openid-connect/token</a><br>Client ID: demo-app</p><p>Then, click the <em>Request token</em> button. There should be a browser window that displays with Keycloak’s login. Enter your credentials and click Login. Keycloak will display the newly created token in the list of <em>Existing token</em>. Select it, select <em>Add token to: header</em> and click <em>Use token</em>. It will be added to the header in an Authorization parameter.</p><p>Let’s call the method again and verify that it’s working as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U3zZNVOeI63opeLB58SI1w.png" /><figcaption>The result of the call with the valid JWT token passed</figcaption></figure><p>Now that the API is secured, let’s do the Angular part.</p><h3>Implementing implicit flow in Angular</h3><p>OpenId connect’s implicit flow is based on redirecting the user to the identity provider’s login page and uses redirect URIs to verify the client’s identity. It is the only “flow” that doesn’t necessitate the use of a secret. This means that it’s the only valid flow that can be used by a purely client-side application like we are writing with Angular that can’t really keep a secret.</p><p>To implement the implicit flow, I’m going to use an open source library. There are two libraries that I found and that are certified by the OpenId foundation, and they have a very similar name: <a href="https://github.com/damienbod/angular-auth-oidc-client">angular-auth-oidc-client by Damien Bowden</a> and <a href="https://github.com/manfredsteyer/angular-oauth2-oidc">angular-oauth2-oidc by Manfred Steyer</a>. Both are very similar in their implementation and both projects are well supported. Here, I’ll use the angular-oauth2-oidc library. A lot of the explanation and code samples are coming from the library’s Readme.</p><p>Let’s start by installing the library using NPM</p><pre>npm install angular-oauth2-oidc --save</pre><p>Then, you need to import the module in your app.module in our case, we will add it to the app.module.shared.ts file. Simply add the import of the OAuthModuleand add it to the NgModule import property.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73d15b8d75024996abc36fb2580a92c0/href">https://medium.com/media/73d15b8d75024996abc36fb2580a92c0/href</a></iframe><p>Then, we’ll create a file in the ClientApp/app folder and call it auth.config.ts. It will export the configuration for the Oidc.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8debe64bde5d4832b0ced00108d40dd8/href">https://medium.com/media/8debe64bde5d4832b0ced00108d40dd8/href</a></iframe><p>Then, we need to configure the oauth service when the application starts. We’ll modify the app.component.ts file. We just inject a OAuthService from the constructor and call a method to setup the configuration that we created above and try the login.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1dc61cf18dab1a400b758d9a47f5e0e/href">https://medium.com/media/b1dc61cf18dab1a400b758d9a47f5e0e/href</a></iframe><p>Next step is to add the login menu to the navigation bar. Let’s edit the navmenu.component.ts file and create login and logout methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50b64d6dd6b381e513ee6908b131a37d/href">https://medium.com/media/50b64d6dd6b381e513ee6908b131a37d/href</a></iframe><p>and the html, add this to the end of the navigation menu.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f51a6283b7a6de0fdef76c4f6c6a2148/href">https://medium.com/media/f51a6283b7a6de0fdef76c4f6c6a2148/href</a></iframe><p>Finally, let’s add the authentication token to the headers of the call to the API. I also took the opportunity to move the call to the API out of the constructor to an OnInit method, it’s a pretty bad idea to have it in the constructor anyway.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a796024527a3e4fb3fc74521de0b2d1/href">https://medium.com/media/3a796024527a3e4fb3fc74521de0b2d1/href</a></iframe><p>As you can see, it’s just a matter of getting the access token using the oauth library and adding it to the header in an Authorization property.</p><h3>Conclusion</h3><p>We’re done with this basic sample, we went around all the important things and shown how to use Keycloak with an Asp.Net Core application.</p><p>I have put the full project available on GitHub here: <a href="https://github.com/Gimly/SampleNetCoreAngularKeycloak">https://github.com/Gimly/SampleNetCoreAngularKeycloak</a></p><p>I have also a follow-up article that will guide you on adding authorization using Keycloak’s groups. <a href="https://medium.com/@xavier.hahn/adding-authorization-to-asp-net-core-app-using-keycloak-c6c96ee0e655">https://medium.com/@xavier.hahn/adding-authorization-to-asp-net-core-app-using-keycloak-c6c96ee0e655</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6437948c008" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>