<?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 Axel on Medium]]></title>
        <description><![CDATA[Stories by Axel on Medium]]></description>
        <link>https://medium.com/@rotter.axel?source=rss-f5f6a762032------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*gvlDgZh1B7gJboOe</url>
            <title>Stories by Axel on Medium</title>
            <link>https://medium.com/@rotter.axel?source=rss-f5f6a762032------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 13:16:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rotter.axel/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[Automatically Sign In to Identity Server 4 with Postman and Test .Net Core Controller methods]]></title>
            <link>https://medium.com/@rotter.axel/automatically-sign-in-to-identity-server-4-with-postman-and-test-net-core-controller-methods-bc5d8feb199e?source=rss-f5f6a762032------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc5d8feb199e</guid>
            <category><![CDATA[net-core-3]]></category>
            <category><![CDATA[postman]]></category>
            <category><![CDATA[test-automation]]></category>
            <category><![CDATA[identityserver4]]></category>
            <dc:creator><![CDATA[Axel]]></dc:creator>
            <pubDate>Mon, 15 Jun 2020 14:01:03 GMT</pubDate>
            <atom:updated>2020-06-19T15:08:07.109Z</atom:updated>
            <content:encoded><![CDATA[<h3>How I automatically test .Net Core controller methods that require Authorization with Postman</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*-FXHPc2G8PNMfR9m.png" /></figure><p>The purpose of this post is to explain how to automatically log in to your Identity Provider and run your controller tests.</p><p><strong>Prerequisites</strong></p><ul><li>You have a .NET Core API configured to connect to your Identity Provider (does not need to be Identity Server 4)</li><li>You are familiar with sending requests in Postman</li></ul><h3>Client Configuration</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*Ov89imulEFMywVza3QpHCA.png" /><figcaption>Fig 1 IdentityServer4 client configuration — Note the GrantType</figcaption></figure><p>Postman will act as the client app trying to authenticate and authorize. You configure it in the Config.cs class of your Identity Provider.</p><p>In my case I have an API that´s called festivalsapi to which we want to gain access with our token.</p><p><strong>GrantType.ResourceOwnerPassword: </strong>This grant type should not be used in production, but it is this configuration setting that allows us to log in and get a bearer token without having to open a browser window and type in our username and password.</p><h3>User Configuration</h3><p>In my case I have 2 roles I want to test. I define a user with an “Organizer” role and one with an “Admin” role. These will be my users to log in on the Identity Provider and call information on the API that requires this role.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*c-PcmwZCUrjxX6AOJmYz9g.png" /><figcaption>Fig 2 IdentityServer4 test user configuration</figcaption></figure><h3>Resource Configuration</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*1RkluS3Ze2WrBmGccmKAcg.png" /><figcaption>Fig 2 IdentityServer4 client configuration</figcaption></figure><p>Here you define the APIs you want to add to your IDP.</p><h3>Configure your API</h3><p>To configure your API all you have to do is the following:</p><ol><li>Install-Package IdentityServer4.AccessTokenValidation</li><li>Add UseIdentityServerAuthentication middleware to your Startup.cs — Configure method — before UseEndpoints (important).</li><li>Make sure your APIName matches the API name from Config.cs on your Identity Provider.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*ZLxIV6XZpxeKIboLIERJUw.png" /><figcaption>Fig 3 API configuration</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*_edDR1Pp2kXKUs12etFhMA.png" /><figcaption>Fig 4 API configuration</figcaption></figure><h3>[Authorize] Protected Controller Method</h3><p>The only relevant thing is that only a user with the “Festival_Admin” role can access this PATCH method. If you want to learn how to implement Policies instead of Roles let me know.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/881/1*pjH4dM8iShEIMFOLow0SKw.png" /></figure><h3>Postman Configuration</h3><p>Now the juicy part!</p><p>Because we configured Postman to get it´s Bearer Tokens with the <strong>GrantType.ResourceOwnerPassword </strong>we can now pass the Username and Password in the Body.</p><p>To automate this whole testing process the order of operations looks as follows:</p><ol><li>Sign in the users you want to test with</li><li>Store the bearer token for each logged in user in the environment variables</li><li>Run tests and retrieve the bearer token when necessary</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*phJCa35SgpgV4bwPi0LG3A.png" /><figcaption>Body to request a Bearer Token for a user without a role</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SCiMsjCdiQdA6_v--8rNUg.png" /><figcaption>Body to request a bearer token for a user with “Admin” role</figcaption></figure><p>I use the Tests tab to store the bearer token that was retrieved</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*13SvIuUGz6D8RzNQKy4AWg.png" /><figcaption>Storing the bearer token</figcaption></figure><p>Now I can test the controller methods that require authorization. Here I set the <strong>Authorization </strong>to <strong>Bearer Token</strong>. And retrieve the environment variable I stored with {{AdminToken}}</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NK_l-b0Nb70YnVBHHbtcIQ.png" /></figure><p>I test if the PATCH method returns a status code 204, then I know that the <strong>Authorization succeeded</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jkUf9sTD-rWVUrXhs7_NOA.png" /></figure><p>I test if the PATCH method returns a status code 403, then I know that the <strong>Authorization failed</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JKxA0B1H_bokNPBb6Z5hEA.png" /></figure><h3>Automatically Run all Tests with Postman Runner</h3><p>I use the Postman Runner to run all my tests. The order in which tests run is important.</p><ol><li>Select which request collection you want to run</li><li>Select the environment where you stored your bearer tokens as variables</li><li>Run the authorization requests before all others</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7H2SnD0Sd1vN5jnEFiHnFw.png" /></figure><p>After running the festival we can see the Token requests ran first. I even added a request without an authorization header to get a 401 Unauthorized code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bKP0w0Dd9xa41knvWagSsw.png" /></figure><p>And that´s how you can automatically run all controller tests on postman without having to log in. I would recommend using this method only in your development environment as you don´t want to allow anyone to log in with the <strong>GrantType.ResourceOwnerPassword </strong>grant.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc5d8feb199e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>