11using System . Net ;
2+ using QRCoder ;
23
34namespace RemoteAgent . Service . Web ;
45
@@ -49,7 +50,7 @@ internal static class PairingHtml
4950 h2 { margin-bottom: 4px; }
5051 p { color: #555; margin-top: 0; }
5152 #qr { margin: 16px auto; }
52- #qr canvas { border-radius: 8px; }
53+ #qr img { border-radius: 8px; display: block; margin: 0 auto ; }
5354 .key { font-family: monospace; background: #f5f5f5; padding: 12px; border-radius: 6px; word-break: break-all; margin: 16px 0; font-size: 13px; border: 1px solid #ddd; text-align: left; }
5455 .btn { display: block; padding: 14px; background: #0078d4; color: white; border-radius: 6px; text-decoration: none; font-size: 16px; margin-top: 8px; }
5556 .btn:hover { background: #005a9e; }
@@ -59,17 +60,7 @@ internal static class PairingHtml
5960 <body>
6061 <h2>Your API Key</h2>
6162 %%BODY%%
62- <div id="qr"></div>
63- <script src="https://cdn.jsdelivr.net/npm/qrcode@1.5.4/build/qrcode.min.js"></script>
64- <script>
65- (function() {
66- var data = %%QR_DATA%%;
67- if (!data) return;
68- QRCode.toCanvas(document.createElement('canvas'), data, { width: 256 }, function(err, c) {
69- if (!err) { document.getElementById('qr').appendChild(c); }
70- });
71- })();
72- </script>
63+ <div id="qr">%%QR_IMG%%</div>
7364 </body>
7465 </html>
7566 """ ;
@@ -96,13 +87,13 @@ public static string LoginPage(bool error = false, bool noPairingUsers = false)
9687
9788 public static string KeyPage ( string apiKey , string deepLink )
9889 {
99- string body , qrData ;
90+ string body , qrImg ;
10091
10192 if ( string . IsNullOrEmpty ( apiKey ) )
10293 {
10394 body = "<div class=\" warn\" >No API key is configured on this server. " +
10495 "Set <code>Agent:ApiKey</code> in <code>appsettings.json</code>.</div>" ;
105- qrData = "null " ;
96+ qrImg = "" ;
10697 }
10798 else
10899 {
@@ -111,10 +102,19 @@ public static string KeyPage(string apiKey, string deepLink)
111102 body = $ "<p>Scan the QR code with your Remote Agent app, or tap <strong>Open in App</strong>.</p>" +
112103 $ "<div class=\" key\" >{ encodedKey } </div>" +
113104 $ "<a class=\" btn\" href=\" { encodedLink } \" >Open in Remote Agent App</a>";
114- // Serialize as JSON string — safe to embed directly in <script>.
115- qrData = System . Text . Json . JsonSerializer . Serialize ( deepLink ) ;
105+ qrImg = GenerateQrPngImg ( deepLink ) ;
116106 }
117107
118- return KeyTemplate . Replace ( "%%BODY%%" , body ) . Replace ( "%%QR_DATA%%" , qrData ) ;
108+ return KeyTemplate . Replace ( "%%BODY%%" , body ) . Replace ( "%%QR_IMG%%" , qrImg ) ;
109+ }
110+
111+ private static string GenerateQrPngImg ( string data )
112+ {
113+ using var generator = new QRCodeGenerator ( ) ;
114+ var qrData = generator . CreateQrCode ( data , QRCodeGenerator . ECCLevel . M ) ;
115+ using var code = new PngByteQRCode ( qrData ) ;
116+ var png = code . GetGraphic ( 6 ) ;
117+ var b64 = Convert . ToBase64String ( png ) ;
118+ return $ "<img src=\" data:image/png;base64,{ b64 } \" width=\" 256\" height=\" 256\" alt=\" Pairing QR code\" />";
119119 }
120120}
0 commit comments