Skip to content

Commit

Permalink
Feature/net6 workaround (#102)
Browse files Browse the repository at this point in the history
* Add .NET6.0 workaround

It seems the QRCode Class is no longer available in QRCoder for .NET6. I needed this library in one of my projects. So i decided to publish my workaround to here. Hopefully it is of use to some

* Delete misc.xml

* Update Google.Authenticator/TwoFactorAuthenticator.cs

Co-authored-by: Frans Lytzen <[email protected]>

Co-authored-by: Frans Lytzen <[email protected]>
  • Loading branch information
TristanBoland and flytzen authored Apr 13, 2022
1 parent 7904671 commit c464ba3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net452;net5.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net452;net5.0;net6.0</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
5 changes: 4 additions & 1 deletion Google.Authenticator/Google.Authenticator.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net45;net5.0;net6.0</TargetFrameworks>
<Product>Google Authenticator Two-Factor</Product>
<Title>Google Authenticator Two-Factor Authentication Library</Title>
<Description>Google Authenticator Two-Factor Authentication Library (Not officially affiliated with Google.)</Description>
Expand Down Expand Up @@ -35,6 +35,9 @@
<PropertyGroup Condition=" '$(TargetFramework)' == 'net5'">
<DefineConstants>NET5_0;NETCOREAPP</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net6'">
<DefineConstants>NET6_0;</DefineConstants>
</PropertyGroup>

<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>
Expand Down
25 changes: 17 additions & 8 deletions Google.Authenticator/TwoFactorAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,24 @@ private static string GenerateQrCodeUrl(int qrPixelsPerModule, string provisionU
var qrCodeUrl = "";
try
{
using (var qrGenerator = new QRCodeGenerator())
using (var qrGenerator = new QRCodeGenerator())
using (var qrCodeData = qrGenerator.CreateQrCode(provisionUrl, QRCodeGenerator.ECCLevel.Q))
using (var qrCode = new QRCode(qrCodeData))
using (var qrCodeImage = qrCode.GetGraphic(qrPixelsPerModule))
using (var ms = new MemoryStream())
{
qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
qrCodeUrl = $"data:image/png;base64,{Convert.ToBase64String(ms.ToArray())}";
}
#if NET6_0
using (var qrCode = new PngByteQRCode(qrCodeData))
{
var qrCodeImage = qrCode.GetGraphic(qrPixelsPerModule);
qrCodeUrl = $"data:image/png;base64,{Convert.ToBase64String(qrCodeImage)}";
}
#else
using (var qrCode = new QRCode(qrCodeData))
using (var qrCodeImage = qrCode.GetGraphic(qrPixelsPerModule))
using (var ms = new MemoryStream())
{
qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
qrCodeUrl = $"data:image/png;base64,{Convert.ToBase64String(ms.ToArray())}";
}
#endif

}
catch (TypeInitializationException e)
{
Expand Down

0 comments on commit c464ba3

Please sign in to comment.