<form id="form1" runat="server">
<asp:TextBox ID="txtUsername" runat="server" placeholder="Enter Username"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate QR Code" OnClick="btnGenerate_Click" />
<asp:Image ID="imgQRCode" runat="server" Width="200px" Height="200px" />
<br />
<!-- Username input with autocomplete -->
<asp:TextBox ID="TextBox1" runat="server" placeholder="Enter Username" autocomplete="off" list="usernameList"></asp:TextBox>
<datalist id="usernameList">
<!-- Options will be dynamically populated by C# code-behind -->
</datalist>
<!-- Button to redirect to the new URL -->
<asp:Button ID="btnRedirect" runat="server" Text="Go to Profile" OnClick="btnRedirect_Click" />
<!-- Username input with autocomplete -->
<asp:TextBox ID="TextBox2" runat="server" placeholder="Enter Username" autocomplete="off" oninput="autoRedirect()" />
<datalist id="usernameList">
<!-- Options populated by C# code-behind -->
</datalist>
<!-- Script to handle automatic redirection -->
<script type="text/javascript">
function autoRedirect() {
var username = document.getElementById('<%= txtUsername.ClientID %>').value.trim();
// Redirect only if a username is entered
if (username) {
// Encode the username for URL safety
var encodedUsername = encodeURIComponent(username);
// Construct the redirect URL with the username
var redirectUrl = "https://www.c-sharpcorner.com/profile?username=" + encodedUsername;
// Redirect to the constructed URL
window.location.href = redirectUrl;
}
}
</script>
</form>
<script type="text/javascript">
document.getElementById('<%= btnRedirect.ClientID %>').onclick = function () {
var username = document.getElementById('<%= txtUsername.ClientID %>').value.trim();
if (username === "") {
alert("Please enter a username.");
return false; // Prevents the postback if username is empty
}
return true; // Allows postback for valid input
};
</script>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Populate the datalist with previous usernames (retrieve from your database or any data source)
List<string> usernames = GetUsernames();
foreach (string username in usernames)
{
// Dynamically add options to the datalist for autocomplete
Literal option = new Literal
{
Text = $"<option value='{username}'></option>"
};
Page.Header.Controls.Add(option); // Adds to <datalist> with ID "usernameList"
}
}
}
private List<string> GetUsernames()
{
// Retrieve usernames from database. Example hardcoded list:
return new List<string> { "user1", "user2", "user3" };
}
protected void btnRedirect_Click(object sender, EventArgs e)
{
string username = txtUsername.Text.Trim();
if (!string.IsNullOrEmpty(username))
{
// Sanitize and validate the username as needed
string url = $"https://stackoverflow.com/{HttpUtility.UrlEncode(username)}";
// Redirect the user to the new URL
Response.Redirect(url);
}
else
{
// Handle empty username case (e.g., show an error message)
}
}
private string GenerateQRCode(string encryptedText)
{
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
{
QRCodeData qrCodeData = qrGenerator.CreateQrCode(encryptedText, QRCodeGenerator.ECCLevel.Q);
using (QRCoder.QRCode qrCode = new QRCoder.QRCode(qrCodeData))
{
using (Bitmap qrCodeImage = qrCode.GetGraphic(20))
{
// Convert to base64 to display on frontend
using (MemoryStream ms = new MemoryStream())
{
qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
string base64Image = Convert.ToBase64String(byteImage);
return "data:image/png;base64," + base64Image;
}
}
}
}
}
using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZXing;
using ZXing.QrCode;
using ZXing.QrCode.Internal;
protected void btnGenerate_Click(object sender, EventArgs e)
{
string username = txtUsername.Text;
string encryptedUsername = EncryptUsername(username);
// Generate QR Code from the encrypted username
string qrCodeImageUrl = GenerateQRCode(encryptedUsername);
// Display the QR code
imgQRCode.ImageUrl = qrCodeImageUrl;
}
private string EncryptUsername(string username)
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(username));
StringBuilder builder = new StringBuilder();
foreach (byte b in bytes)
{
builder.Append(b.ToString("x2"));
}
return builder.ToString();
}
}
0 Comments