<?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 Michele Pisicoli on Medium]]></title>
        <description><![CDATA[Stories by Michele Pisicoli on Medium]]></description>
        <link>https://medium.com/@pisicoli.michele?source=rss-9fc5e8fc53d1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Pn6M25-J_5bDJMJszaFqBw.png</url>
            <title>Stories by Michele Pisicoli on Medium</title>
            <link>https://medium.com/@pisicoli.michele?source=rss-9fc5e8fc53d1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 08:03:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pisicoli.michele/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[Secure Asp.Net Core 2 API with Ethereum Blockchain]]></title>
            <link>https://medium.com/coinmonks/secure-asp-net-core-2-api-with-ethereum-blockchain-88001d5ddb6?source=rss-9fc5e8fc53d1------2</link>
            <guid isPermaLink="false">https://medium.com/p/88001d5ddb6</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[aspnetcore]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Michele Pisicoli]]></dc:creator>
            <pubDate>Wed, 18 Jul 2018 08:12:10 GMT</pubDate>
            <atom:updated>2018-07-19T18:49:20.927Z</atom:updated>
            <content:encoded><![CDATA[<h3>The problem</h3><p>A common design for Decentralized Application (DApp) consist to rely not only on <strong>Ethereum blockchain</strong> but on an API layer too. In this scenario, the DApp interact with the smart-contract through user’s Ethereum account and to the API layer through a JWT Token issued in exchange of user credentials.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*myVxQt4aTKJzFYnFj4mypQ.png" /><figcaption>A typical DApp architecture</figcaption></figure><p>The goal is to<strong> use Ethereum account as user credentials </strong>to request the JWT token.</p><p>A possible simplest way to do so could be to requests the user to make a transaction on Ethereum with an additional random generated data and then check the transaction and the random data before issuing the JWT. This approach has several side effect:</p><ol><li>The user must do a transaction and pay <strong><em>gas </em></strong>for a simple authentication.</li><li>The user must wait for 12–120 seconds (based on gas spent) to complete the authentication process.</li><li>All the login operations of every user became immutably public on Ethereum blockchain.</li></ol><p>This way is not practical and has several user experience limitation, we need a way for a user to prove that she/he own the private key related to the account she/he wants to use to sign in, without (of course) asking for the private key and without asking her/him to make a transaction.</p><h3>The solution</h3><p>This tutorial was inspired to me by this <a href="https://medium.com/metamask/the-new-secure-way-to-sign-data-in-your-browser-6af9dd2a1527">post </a>from Metamask team member <a href="https://medium.com/@danfinlay">Dan Finlay</a>. Basically, your DApp could prompt a user to sign a text message using his private key. This sign operation doesn’t generate a transaction and it is handled by Metamask add-on transparently (btw, your account need to be unlocked). Once signed, the account, the message, and the signature are sent to the API Token endpoint. The authentication method first extrapolates the account (aka the public key) from the signature by a function that accepts as input the signature and the plain message. If the computed Ethereum address is equal to the account provided by the user, a JWT token is issued for that account.</p><p>Is important to note that the entire authentication flow is made without asking for a username/password or OAuth external service. The mechanism used to authenticate the user, is the same Ethereum use to guarantee security in Ethereum blockchain. This is possible thanks to <strong>web3.personal.sign</strong> JSON RPC provided by Go Ethereum (Geth) through Metamask plugin.</p><p>Server side the counterpart JSON RPC is called to retrieve the account from the signature: <strong>web3.personal.ecrecover</strong>. In this tutorial, we’ll build an Asp.Net Core 2 project as the API layer and a simple HTML/javascript client as a DApp to practically demonstrate this authentication process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UknISGD_zTEXJBKIe3v0JA.png" /><figcaption>Authentication flow</figcaption></figure><ol><li>From DApp user click the login button. This requires the web3 object provided by Metamask.</li><li>Metamask asks the user to sign a message via <strong>web3.personal.sign</strong> JSON RPC.</li><li>The signature is sent to the API layer that verifies the account via <strong>web3.personal.ecrecover</strong> JSON RPC.</li><li>Once verified, the API layer issue the JWT.</li></ol><h3>Prerequisites</h3><ol><li>Install <a href="https://metamask.io/">Metamask add-on</a> for Chrome or Firefox. This add-on “<em>brings Ethereum on your browser</em>”. Practically Metamask provide a <a href="https://github.com/ethereum/web3.js/"><strong>web3</strong></a><strong> </strong>object that is used to interact with Ethereum blockchain from your DApp, take care of your private key and manage transactions from within the browser.</li><li>Optional. Run a <a href="https://github.com/ethereum/go-ethereum">Geth</a> node. I’ll show you two methods to recover an Ethereum account from the signature, one of them require your API layer to call a JSON RPC against a Geth node. <br>Note: <strong>Infura </strong>is not enough for now because they not allow most of web3.personal.* JSON RPC. For development purpose running a Geth node, is straightforward. In a production environment, running a Geth node is not a simple task due to security concerns. The best way to do so is relying on Blockchain as a Service (BaaS) stack provided by <a href="https://aws.amazon.com/it/blockchain/templates/">AWS </a>or <a href="https://azure.microsoft.com/en-us/solutions/blockchain/">Azure</a>.</li><li>Developement stack: <strong>Visual studio 2017</strong> and Node Package Manager (<strong>NPM</strong>).</li><li>Basic knowledge of Ethereum/Asp.Net Core/Frontend development, basic knowledge of JWT authentication flow.</li></ol><h3>Let’s do this!</h3><p>Open Visual Studio 2017, create the EthereumJwtSolution and add two <strong>Asp.Net Core 2 Web Application</strong> project: <strong>EthereumJwtApi </strong>and <strong>EthereumJwtClient</strong>. Choose the empty project scaffolding for both projects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*ixVNDbAcqejNXbkdJTj_JA.png" /><figcaption>Create two Asp.Net Core 2 Web Application</figcaption></figure><p><strong>EthereumJwtClient </strong>is just an HTML/Javascript client. We’ll build the client application on top of Asp.Net Core just for easily run it on IIS Express.</p><p>We need to prepare EthereumJwtApi to create and handle JWT token in order to protect some secure endpoint. The task is straightforward because Asp.Net Core 2 has a built-in JWT mechanism ready to be plugged in our application. Open <em>Startup.cs</em> and modify the <em>ConfigureServices</em> method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a78c16bf43e1add20ebf95a498f1d47b/href">https://medium.com/media/a78c16bf43e1add20ebf95a498f1d47b/href</a></iframe><p>then modify the <em>Configure</em> method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82978231c3f52bc272bfeca39d54515f/href">https://medium.com/media/82978231c3f52bc272bfeca39d54515f/href</a></iframe><p>we are telling our API application to use the <strong>JWT Authentication</strong> service. In order to work with our client, we need to configure a <strong>Cors </strong>policy too. We define the JWT configuration settings in <em>appsetting.json</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/508c933515d8c7e176f8e189dbccfe78/href">https://medium.com/media/508c933515d8c7e176f8e189dbccfe78/href</a></iframe><p>Create a simple as a possible secured endpoint for testing purpose:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10d83bda50c1c5689527043fdf57af3a/href">https://medium.com/media/10d83bda50c1c5689527043fdf57af3a/href</a></iframe><p>The <em>TokenController.cs</em> will take care for JWT requests and the related token issue:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97f326f71eeb2e09ab6ffb960088b6f5/href">https://medium.com/media/97f326f71eeb2e09ab6ffb960088b6f5/href</a></iframe><p>This is a typical JWT controller, the core methods, <em>Authenticate </em>and <em>Authenticate2</em> is not yet implemented. Once implemented they will do the same job: recover the Ethereum address from the signature and check if it’s equal to the Ethereum address provided by the client.</p><p>The <em>LoginVM </em>represents the user credentials provided by the client and <em>UserVM</em> represents a “<em>server side</em>” signed-in user:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/beb2f03ce0f0b57c4a389f7a3a35fa5f/href">https://medium.com/media/beb2f03ce0f0b57c4a389f7a3a35fa5f/href</a></iframe><p>The <em>Authenticate </em>method will take <strong>Signature </strong>and <strong>Message </strong>properties as input for the <strong>ecRecover </strong>function, the <em>Authenticate2</em> method will take <strong>Signature </strong>and <strong>Hash </strong>properties instead. I’ll explain later the difference.</p><h3>The client</h3><p>As we said, our DApp is a simple HTML/ES6 client. We’ll build the client on top of Asp.Net Core 2 just to take advantage of <strong>IIS Express</strong> and <strong>Visual Studio IDE</strong>. For this reason the <em>Configure </em>method in the <em>Startup.cs</em> class will be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bd8a54587ea5c7514a07f61923b52f0/href">https://medium.com/media/7bd8a54587ea5c7514a07f61923b52f0/href</a></iframe><p>Make the DApp an <strong>NPM </strong>project and install the prerequisites to use ES6 Javascript standard. This is not mandatory, feel free to build your DApp using your own stack.</p><p>Run Powershell from the project folder and run these NPM commands:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6133347494f735c81ac309bb970f7680/href">https://medium.com/media/6133347494f735c81ac309bb970f7680/href</a></iframe><p>In order to configure webpack/babel, create <em>webpack.config.js</em> file whit the following configuration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/028fe952c28220f21dffba283802f94a/href">https://medium.com/media/028fe952c28220f21dffba283802f94a/href</a></iframe><p>We have instructed webpack to build the <em>src/main.js</em> file to<em> /www/js/main.build.js</em>.</p><p>Install Ethereum specific packages:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a232c03940870e70d1c3af6dee344a3b/href">https://medium.com/media/a232c03940870e70d1c3af6dee344a3b/href</a></iframe><p><strong>Web3</strong> is a javascript wrapper that simplifies JSON RPC calls against Ethereum blockchain. <strong>Ethereumjs-util</strong> provides some Ethereum specific utilities.</p><p>Let’s build a very simple HTML page. We need a login button and another button to load some secured data from our API layer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb4c35f8c059b41ed085b0245f82ebcc/href">https://medium.com/media/cb4c35f8c059b41ed085b0245f82ebcc/href</a></iframe><p>The DApp logic will reside in the src/main.js file, as we specified in the <em>webpack.config.js</em> file. The <em>src/main.js</em> file will be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8197a9fca306318110e9bcb1575f11dd/href">https://medium.com/media/8197a9fca306318110e9bcb1575f11dd/href</a></iframe><ol><li><strong>coinbase </strong>and <strong>accessToken </strong>are globals that will store the user Ethereum account and the JWT token respectively.</li><li>The <strong>init </strong>function initialize the web3 object from the provider provided by Metamask, then it tries to retrieve the user’s account (the coinbase). This requires you unlock your account signing in Metamask.</li><li>The <strong>require </strong>function is just a wrapper around the <em>hxr </em>object to make ajax call easily to API layer.</li><li>The <strong>load_data_btn</strong> click handler makes an ajax call to the API layer secured endpoint. This requires a valid accessToken in order to work, otherwise, the API layer will respond with a 401 HTTP response.</li><li>The <strong>login_btn</strong> click is a two-step function. First, it asks the user to sign an arbitrary message. Once signed, it sends the account, the signature, the plain message and a prefixed hash to the token endpoint.</li></ol><p>Note that <strong>web3.personal.sign</strong> takes as input the bytes array of the plain string in Hex format (0x…).</p><p>As we said, server side, we’ll recover the public key from the signature using two different ways: in one we will use <strong>web3.personal.ecrecover</strong> JSON RPC (the <strong>web3.personal.sign</strong> counterpart); in the other one, we will use an underlying <strong>ecrecover</strong> offline function. According to the documentation, <strong>web3.personal.sign</strong> use the underlying <strong>sign </strong>function to sign a hashed and prefixed message, so, in order to use the underlying <strong>ecrecover </strong>counterpart, we need to compute and send this hash to the Token endpoint too.</p><p>Run both the applications and navigate to the client with the browser where Metamask add-on is installed. Remember that in order to build the <em>src/main.js</em> file to <em>js/main.build.js</em> you need to run <strong>webpack</strong> command from Powershell. If all works, the client will retrieve the coinbase and you’ll see your account displayed on the page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0a0O68ivo7T-UZS176jQhA.png" /><figcaption>The working DApp index page</figcaption></figure><p>If you click on the Request Data button now, you’ll get a 401 HTTP response. If you click on the Login button, Metamask will prompt you with a sign request:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*Vuufof7dR1hb-gXtkSudTA.png" /><figcaption>Metamask sign prompt</figcaption></figure><p>Once signed, the handler will make an ajax call to the Token endpoint. At this stage the authenticate methods doesn’t check any signature so the endpoint will always issue the JWT token. Once received the JWT token, the client will be able to call the secured endpoint via ajax. If you click the Request Data button now, you will receive a 200 HTTP response and your data payload:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HpWWCE7U8XaNrT61_tGnuA.png" /><figcaption>We got a JWT token so we are able to make authorized api call</figcaption></figure><h3>Retrieve the Ethereum account from a signature</h3><p>Until now, <strong>EthereumJwtApi </strong>is a simple JWT Asp.Net core sample because it doesn’t provide any valid authentication method.</p><p>The key part of the TokenController are the two Authenticate methods and their abilities to retrieve the Ethereum account from the signature. In order to do so, you need to install <strong>Nethereum.Web3</strong> NuGet package. Nethereum is a .Net implementation of Ethereum.</p><p>The <em>Authenticate </em>method simply make a JSON RPC call to the <strong>web3.personal.ecrecover</strong> function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0a4959c73bd9e5e33d0511aa7a37f98/href">https://medium.com/media/c0a4959c73bd9e5e33d0511aa7a37f98/href</a></iframe><p><strong>PRO:</strong></p><ol><li><strong>web3.personal.sign</strong> is the counterpart of the <strong>web3.personal.sign</strong> so you don’t need to worry about its underlying implementation.</li></ol><p><strong>CONS:</strong></p><ol><li>Requires your own Geth node. Parity is not supported and Infura doesn’t allow web3.personal.* JSON RPC.</li></ol><p><em>Authenticate2</em> method show an alternative way, it uses an offline implementation of the underlying <strong>ecrecover </strong>function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe6812f1fc7baeeb2b0e68f538233dff/href">https://medium.com/media/fe6812f1fc7baeeb2b0e68f538233dff/href</a></iframe><p><strong>PRO:</strong></p><ol><li>Doesn’t require JSON RPC call in order to work. <strong>MessageSigner.EcRecover </strong>is an offline function provided by <strong>Nethereum</strong>.</li></ol><p><strong>CONS:</strong></p><ol><li>You need to take care of the <strong>web3.personal.sign</strong> implementation in order to recover the account correctly. For this reason, client side, we have computed the prefixed message hash accordingly.</li></ol><h3>Conclusion</h3><p>Now you have the knowledge and a backbone project to secure your Asp.Net Core 2 API with <strong>Ethereum</strong>. Just a few notes:</p><ol><li><strong>web3 1.0.0</strong> is in beta, <strong>web3.personal.sign</strong> implementation can vary over time. Be sure to use this kind of authentication method on a codebase you can maintain.</li><li>Maybe Infura can decide to allow <strong>web3.personal.ecrecover</strong> one of these days ;-)</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88001d5ddb6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/secure-asp-net-core-2-api-with-ethereum-blockchain-88001d5ddb6">Secure Asp.Net Core 2 API with Ethereum Blockchain</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>