<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rytis</title>
    <description>The latest articles on DEV Community by rytis (@kbyte_it_services).</description>
    <link>https://dev.to/kbyte_it_services</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3277046%2F4b56b7fb-4bfe-43d6-98b9-e99b499a3089.png</url>
      <title>DEV Community: rytis</title>
      <link>https://dev.to/kbyte_it_services</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kbyte_it_services"/>
    <language>en</language>
    <item>
      <title>How We Built a Secure Invoice Generator with .NET 8 and Razor Pages</title>
      <dc:creator>rytis</dc:creator>
      <pubDate>Thu, 19 Jun 2025 15:10:37 +0000</pubDate>
      <link>https://dev.to/kbyte_it_services/how-we-built-a-secure-invoice-generator-with-net-8-and-razor-pages-552b</link>
      <guid>https://dev.to/kbyte_it_services/how-we-built-a-secure-invoice-generator-with-net-8-and-razor-pages-552b</guid>
      <description>&lt;p&gt;In this article, I’ll walk you through how we developed a secure, lightweight invoicing system using &lt;strong&gt;.NET 8&lt;/strong&gt;, &lt;strong&gt;Razor Pages&lt;/strong&gt;, and &lt;strong&gt;MySQL&lt;/strong&gt;. Our goal was to keep the stack simple while ensuring data security, PDF generation, and clean usability — all tailored for SMEs.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧱 The Stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;.NET 8&lt;/strong&gt; (Razor Pages)
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MySQL&lt;/strong&gt; as the data store
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dapper&lt;/strong&gt; for lightweight data access
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;QuestPDF&lt;/strong&gt; for PDF invoice generation
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microsoft Entra ID&lt;/strong&gt; for authentication (via OpenID Connect)
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We aimed for a balance between maintainability and scalability, while also conforming to security best practices.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔐 Authentication &amp;amp; Authorisation
&lt;/h2&gt;

&lt;p&gt;We integrated &lt;strong&gt;Microsoft Entra ID&lt;/strong&gt; for secure sign-in using OpenID Connect, which allowed us to centrally manage user access. This made it easier to enforce conditional access and multi-factor authentication for internal users managing invoices.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddAuthentication&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OpenIdConnectDefaults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AuthenticationScheme&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddMicrosoftIdentityWebApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetSection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AzureAd"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;Using Entra also gave us a single sign-on (SSO) experience across internal tools.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧾 Generating PDFs from Razor Pages
&lt;/h2&gt;

&lt;p&gt;We used &lt;strong&gt;QuestPDF&lt;/strong&gt;, an open-source NuGet package that allows programmatic layout of PDFs. The key benefit is full control over structure and styling, using C# directly.&lt;/p&gt;

&lt;p&gt;Example of an invoice rendering block:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;csharp&lt;br&gt;
component.Content(c =&amp;gt;&lt;br&gt;
{&lt;br&gt;
    c.Header().Text("Invoice #12345").FontSize(20);&lt;br&gt;
    c.Paragraph().Text("Billed To: John Doe");&lt;br&gt;
    c.Line();&lt;br&gt;
    // More sections here&lt;br&gt;
});&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The PDF is generated on the fly and downloaded directly from a secured page. No file system writing needed.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔐 Secure Design Considerations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Anti-forgery tokens&lt;/strong&gt; for form submissions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Role-based access control&lt;/strong&gt; on sensitive pages&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Audit logging&lt;/strong&gt; on invoice edits&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TLS-only endpoints&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server-side validation&lt;/strong&gt; on all input models&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We also avoided JavaScript-heavy solutions to keep the attack surface minimal.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Lessons Learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Razor Pages are surprisingly efficient for CRUD-heavy interfaces&lt;/li&gt;
&lt;li&gt;Integrating PDF generation is smoother when done purely in C#&lt;/li&gt;
&lt;li&gt;Security becomes simpler when you leverage Entra ID and isolate your admin portal&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  👨‍💻 Need Help with Secure Web Tools?
&lt;/h2&gt;

&lt;p&gt;If you’re a small to mid-sized business looking for reliable &lt;a href="https://kbyte.co.uk/it-support-uk/" rel="noopener noreferrer"&gt;&lt;strong&gt;IT Support in the UK&lt;/strong&gt;&lt;/a&gt;, our team at KByte provides tailored solutions — including secure web application development, infrastructure support, and more.&lt;/p&gt;




&lt;p&gt;Feel free to drop a comment if you’re building something similar, or have questions about integrating authentication, PDF tooling, or data access in Razor Pages.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
