<?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 Marcin Janiak on Medium]]></title>
        <description><![CDATA[Stories by Marcin Janiak on Medium]]></description>
        <link>https://medium.com/@marcinjaniak?source=rss-27b7f1460a24------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*TtXYx5nAahMsYMZd4w-Esg.jpeg</url>
            <title>Stories by Marcin Janiak on Medium</title>
            <link>https://medium.com/@marcinjaniak?source=rss-27b7f1460a24------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 02:38:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marcinjaniak/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[GraphQL: Simple authorization and authentication with HotChocolate 11  and ASP.NET Core 3]]></title>
            <link>https://medium.com/@marcinjaniak/graphql-simple-authorization-and-authentication-with-hotchocolate-11-and-asp-net-core-3-162e0a35743d?source=rss-27b7f1460a24------2</link>
            <guid isPermaLink="false">https://medium.com/p/162e0a35743d</guid>
            <category><![CDATA[aspnetcore]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[hot-chocolate]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Marcin Janiak]]></dc:creator>
            <pubDate>Tue, 31 Mar 2020 22:00:33 GMT</pubDate>
            <atom:updated>2020-04-02T17:47:02.520Z</atom:updated>
            <content:encoded><![CDATA[<h3>GraphQL: Simple authorization and authentication with HotChocolate 11 and ASP.NET Core 3</h3><p>Recently in the company, we have started to use GraphQL in business projects. Being more specific, a server-based implementation — <a href="http://hotchocolate.io/">HotChocolate</a>.</p><p>Basically most of the projects need some kind of authentication and some of the apps might also require to specify roles and permissions.</p><p>The key advantage of HotChocolate is a great integration with asp.net core mechanisms and also a rich ecosystem of various solutions, e.g:</p><ul><li><strong>Strawberry Shake </strong>— .NET client for GraphQL</li><li><strong>Banana Cake Pop</strong> — GraphQL IDE</li><li><strong>Green Donut </strong>— a port of facebook’s DataLoader utility</li></ul><p><strong>Few words to mention: The code below is simplified and should not be used in production as it is.</strong></p><h3>Installing required NuGet packages</h3><ul><li>HotChocolate.AspNetCore, 11.0.0-preview.118</li><li>HotChocolate.AspNetCore.Authorization, 11.0.0-preview.118</li><li>HotChocolate.AspNetCore.Playground, 11.0.0-preview.118</li><li>Microsoft.AspNetCore.Authentication.JwtBearer, 3.1.3</li><li>Microsoft.IdentityModel.Tokens, 6.5.0</li></ul><p>We want to start off by creating some kind of Identity Service implementation and adding authentication (JWT based) in a ConfigureServices and Configure method in Startup.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2927b0bd584459620da73651205d46c9/href">https://medium.com/media/2927b0bd584459620da73651205d46c9/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b122ce30788eb269e4a94dc31fa1f29b/href">https://medium.com/media/b122ce30788eb269e4a94dc31fa1f29b/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67ca447dcad769567f48cc70d28e6cec/href">https://medium.com/media/67ca447dcad769567f48cc70d28e6cec/href</a></iframe><p>It’s worth mentioning that you can (and actually you should) hook up some existing oauth2 solution instead of creating custom Identity Service.</p><p>Later on, let’s think about getting token and enforcing authorization on a GraphQL’s fields.</p><h3><strong>Getting token</strong></h3><p>There are actually three ways to get a JWT token.</p><ul><li>Querying for it</li><li>Mutating for it</li><li>Using REST request</li></ul><p>In my humble opinion, the best way (in a simple app without oauth2) is to mutate for it, because getting the token actually changes (mutates) “who can access API”. In a oauth2 solution, you should probably perform a REST request to /connect/token endpoint.</p><p>For the sake of simplicity, we are gonna do a mutation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/962ed227fed5f31b75584908d8a2f41a/href">https://medium.com/media/962ed227fed5f31b75584908d8a2f41a/href</a></iframe><p>Let’s start the project and navigate to <a href="https://localhost:5001/playground/">https://localhost:5001/playground/</a> in a browser or open up Banana Cake Pop (or even <a href="http://insomnia.rest/">Insomnia</a>) and write a mutation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a69496ee6738600839d7912dc43130f/href">https://medium.com/media/2a69496ee6738600839d7912dc43130f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*G1PK3E4uXr6mIlS4KSPvLQ.png" /><figcaption>Getting token in Banana Cake Pop</figcaption></figure><p>Feel free to check token in <a href="https://jwt.io/">jwt.io</a> (or <a href="https://jwt.ms/">jwt.ms</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5vLaUokkIVx1iRGNWB1cPg.png" /></figure><h3><strong>Spicing it up with an authentication</strong></h3><p>The cool thing with HotChocolate’s solution is that it is fully integrated with ASP.NET Core authorization and authentication.</p><p>You can feel free to use role-based or policy-based authentication.</p><p>Let’s add authentication part in ConfigureServices</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5b0ee4573aa897ca4783016cae890ad/href">https://medium.com/media/e5b0ee4573aa897ca4783016cae890ad/href</a></iframe><p>and Authentication in Configure (<strong>must be called before app.UseGraphQL()</strong>)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4cc415ca40b56cca551b06857a3ee86/href">https://medium.com/media/b4cc415ca40b56cca551b06857a3ee86/href</a></iframe><h3>Querying it out (and using GlobalState)</h3><p>Our query class is actually pretty simple. It returns all claims the current user has.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/580009153f7da58283b3231deb6177c7/href">https://medium.com/media/580009153f7da58283b3231deb6177c7/href</a></iframe><p><strong>Important thing</strong> is that the Authorize attribute comes from HotChocolate namespace:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*M3t0GIORvNs7nXAZLhaarg.png" /><figcaption>Authorize namespace</figcaption></figure><p>Actually you can define authorized (and authenticated) fields by using HotChocolate’s descriptors or in a schema first approach instead of attributes (as shown here). Please refer to an <a href="https://hotchocolate.io/docs/authorization">Authorization site in a HotChocolate docs</a>.</p><p><a href="https://hotchocolate.io/docs/authorization">Authorization · Hot Chocolate</a></p><h3>Using the Global State</h3><p>Let’s create our custom global state which is passed down in an execution path (to “deeper” resolvers) and an object which we want to persist.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76aaaf88af16ee24c41e0651425b8aab/href">https://medium.com/media/76aaaf88af16ee24c41e0651425b8aab/href</a></iframe><p>Now we want to be able to access HttpContext only once.</p><p>We can achieve it by adding a query request interceptor.</p><p>Add the following method to Startup class</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ded5d8534ee18c0f036773fdf783423/href">https://medium.com/media/5ded5d8534ee18c0f036773fdf783423/href</a></iframe><p>and add it as interceptor in ConfigureServices:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fcf4a8d40380bf0702a69fc83361db49/href">https://medium.com/media/fcf4a8d40380bf0702a69fc83361db49/href</a></iframe><p>Now we can use our custom global state attribute (CurrentUserGlobalState) in HotChocolate fields. No more magic strings! (at least here)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e35ee875aa936f8edc84927681251f6/href">https://medium.com/media/4e35ee875aa936f8edc84927681251f6/href</a></iframe><h3>Final test</h3><p>So, we already have a token mutation. We can sign in with a few existing roles:</p><ul><li>hr</li><li>dev</li><li>leader</li><li>employee</li></ul><p>The Identity Service checks email if it contains any of these words and adds appropriate roles to claims.</p><p>Feel free to sign in as a <em>hrleader@example.com </em>or explore it on your own.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*5Vn2_s1ia26Ksq9eEe-46g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*huYmmmB6DH7MW19fO_n5ng.png" /><figcaption>Query without token.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZacxotI3nyaepjgpOAtKVQ.png" /><figcaption>Query that tests multiple fields.</figcaption></figure><p>Query:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0efb8d2c2738bec2dfe99269515ce128/href">https://medium.com/media/0efb8d2c2738bec2dfe99269515ce128/href</a></iframe><p>HTTP Headers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f68abda7e6d5c4854fc8da6a3d65638c/href">https://medium.com/media/f68abda7e6d5c4854fc8da6a3d65638c/href</a></iframe><h3>Summary</h3><p>Implementing authentication and authorization with HotChocolate and ASP.NET Core is pretty straightforward. Support for role-based and policy-based auth grants us the desired flexibility.</p><p><a href="https://github.com/marcin-janiak/GraphQLAuthDemo"><strong>The full code is available here</strong></a></p><p><a href="https://github.com/marcin-janiak/GraphQLAuthDemo">marcin-janiak/GraphQLAuthDemo</a></p><p>Feel free to comment and ask questions.</p><h3><strong>References</strong></h3><ul><li><a href="http://graphql.org/">GraphQL: A query language for APIs.</a></li><li><a href="http://hotchocolate.io/">Hot Chocolate · A GraphQL Server for .NET core and .NET classic</a></li><li><a href="https://hotchocolate.io/docs/authorization">Authorization · Hot Chocolate</a></li><li><a href="https://github.com/ChilliCream/hotchocolate">ChilliCream/hotchocolate</a></li><li><a href="https://docs.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-3.1">Overview of ASP.NET Core Authentication</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=162e0a35743d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>