Published at 01/05/2023, 09:02
In this guide, we will walk you through the process of creating a hardware ID (HWID) authentication system in C# using the FluxAuth SDK. This authentication system ensures that only users with valid license keys can access your application and that their access is tied to a specific hardware device.
The C# library allows you to interact with the FluxAuth API for user authentication and variable fetching.
You can install the C# library by either copying the source code into your project or by referencing the compiled binary.
To initialize the library, set the Application
property to your Flux application ID.
using Flux;
Auth.Application = "your-application-id";
To authenticate a user, call the Authenticate
method with the user’s license key and an optional hardware ID.
try {
Auth.Authenticate(license, hwid);
} catch (Exception e) {
Console.WriteLine("Authentication failed: " + e.Message);
return;
}
To get a field from the authenticated response, call the GetField<T>
method with the field name.
int expiresAt = Auth.GetField<int>("expiresAt");
Console.WriteLine("License expires at " + expiresAt);
To get a variable from the Flux API, call the GetVariable<T>
method with the variable name.
string variableValue = Auth.GetVariable<string>("variable-name");
Console.WriteLine("Variable value: " + variableValue);
To download a variable as a file, call the DownloadVariable
method with the variable name.
byte[] fileData = Auth.DownloadVariable("variable-name");
File.WriteAllBytes("output-file-name", fileData);
using Flux;
Auth.Application = "your-application-id";
// Get an unauthenticated variable
Console.WriteLine(Auth.GetVariable<string>("welcome-str"));
try {
Auth.Authenticate("your-license", "your-hwid");
} catch (Exception e) {
Console.WriteLine("Authentication failed: " + e.Message);
return;
}
Console.WriteLine($"License expires at {Auth.GetField<int>("expiresAt")}");
Console.WriteLine($"Authenticated variable: {Auth.GetVariable<string>("authenticated")}");
var file = Auth.DownloadVariable("file");
File.WriteAllBytes("file.bin", file);
To create a function that retrieves the hardware ID of a user’s device, you can use the following code:
using System;
using System.Management;
public static string GetHardwareId()
{
var cpuInfo = string.Empty;
var management = new ManagementClass("win32_processor");
var managementCollection = management.GetInstances();
foreach (var managementObject in managementCollection)
{
cpuInfo = managementObject.Properties["processorID"].Value.ToString();
break;
}
return cpuInfo;
}
This function uses the System.Management
namespace to retrieve the processorID of the user’s device, which can be used as the hardware ID.
Now, you can use this function to get the hardware ID and pass it to the Authenticate
method:
string hwid = GetHardwareId();
try {
Auth.Authenticate(license, hwid);
} catch (Exception e) {
Console.WriteLine("Authentication failed: " + e.Message);
return;
}
With this implementation, your application will now require a valid license key and hardware ID for users to access it. The FluxAuth API will handle the authentication process and provide you with the necessary information to manage your users and their access to your application.