Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using LogicNP.CryptoLicensing;
- namespace Math4Office.Auth
- {
- public class Math4OfficeAuth
- {
- //free license case
- private enum appFeature { Measures = LicenseFeatures.Feature1 };
- //CryptoLicense license;
- private string _licenseCode = string.Empty;
- public Math4OfficeAuth(string licenseCode)
- {
- _licenseCode = licenseCode;
- }
- static CryptoLicense license;
- public static bool Authorize()
- {
- if (Math4OfficeAuth.ValidateToken())
- {
- return true;
- }
- else
- {
- // Pop up the login form and ask user to login.
- // Meanwhile return false
- return false;
- }
- }
- public static bool LoginUser()
- {
- // This function should display the login form.
- // Validate user using ValidateLogin function and update the Login
- // pop up based on errors. Return true if the login is successful
- // and return false if the user cancels the login form.
- // If ValidateLogin fails, update the form based on the error.
- LoginForm loginForm = new LoginForm();
- var formResult = loginForm.ShowDialog();
- if (formResult == System.Windows.Forms.DialogResult.Cancel)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- private int ValidateLogin(string userName, string password)
- {
- // return codes can be used to define the type of errors which occurred
- // 1 - Successfull
- // 0 - Unknown error
- // -1 - Invalid username or password
- // -2 - Some fault at server side
- return 1;
- }
- public static void LogoutUser()
- {
- string userAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
- string appDir = Path.Combine(userAppDataDir, "Math4Office");
- string licFileName = Path.Combine(appDir, "token.tok");
- string infoFileName = Path.Combine(appDir, "user.info");
- if (File.Exists(licFileName))
- {
- try
- {
- File.Delete(licFileName);
- }
- catch
- {
- if (File.Exists(licFileName))
- {
- using (StreamWriter fs = new StreamWriter(licFileName))
- {
- fs.WriteLine("");
- }
- }
- }
- }
- }
- private static bool ValidateToken()
- {
- bool result = false;
- string userAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
- string appDir = Path.Combine(userAppDataDir, "Math4Office");
- string licFileName = Path.Combine(appDir, "token.tok");
- if (File.Exists(licFileName))
- {
- using (StreamReader fs = new StreamReader(licFileName))
- {
- string authenticationKey = fs.ReadLine();
- string validationKey = "AMAAMACmsO3rk5KOGikVV615S52IgdxMMkdswb3e1lmmXDsihncRpqP47NscE8KVcacomy8DAAEAAQ==";
- license = new CryptoLicense(authenticationKey, validationKey);
- if (license.Status != LicenseStatus.Valid)
- {
- result = false;
- }
- else
- {
- result = true;
- //Application.Run(new Form1(lic.ExceptionalFeature()));
- }
- }
- }
- return result;
- }
- public bool Measures()
- {
- if (license.IsFeaturePresent((LicenseFeatures)appFeature.Measures, false))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- private static string GenerateToken()
- {
- byte[] time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
- byte[] key = Guid.NewGuid().ToByteArray();
- string token = Convert.ToBase64String(time.Concat(key).ToArray());
- return token;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement