Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using LogicNP.CryptoLicensing;
  8.  
  9. namespace Math4Office.Auth
  10. {
  11.     public class Math4OfficeAuth
  12.     {
  13.         //free license case
  14.         private enum appFeature { Measures = LicenseFeatures.Feature1 };
  15.         //CryptoLicense license;
  16.         private string _licenseCode = string.Empty;
  17.         public Math4OfficeAuth(string licenseCode)
  18.         {
  19.             _licenseCode = licenseCode;
  20.         }
  21.         static CryptoLicense license;
  22.         public static bool Authorize()
  23.         {
  24.             if (Math4OfficeAuth.ValidateToken())
  25.             {
  26.                 return true;
  27.             }
  28.             else
  29.             {
  30.                 // Pop up the login form and ask user to login.
  31.                 // Meanwhile return false
  32.                 return false;
  33.             }
  34.         }
  35.  
  36.         public static bool LoginUser()
  37.         {
  38.             // This function should display the login form.
  39.             // Validate user using ValidateLogin function and update the Login
  40.             // pop up based on errors. Return true if the login is successful
  41.             // and return false if the user cancels the login form.
  42.             // If ValidateLogin fails, update the form based on the error.
  43.             LoginForm loginForm = new LoginForm();
  44.             var formResult = loginForm.ShowDialog();
  45.             if (formResult == System.Windows.Forms.DialogResult.Cancel)
  46.             {
  47.                 return false;
  48.             }
  49.             else
  50.             {
  51.                 return true;
  52.             }
  53.         }
  54.  
  55.         private int ValidateLogin(string userName, string password)
  56.         {
  57.             // return codes can be used to define the type of errors which occurred
  58.             // 1    - Successfull
  59.             // 0    - Unknown error
  60.             // -1   - Invalid username or password
  61.             // -2   - Some fault at server side
  62.  
  63.             return 1;
  64.         }
  65.  
  66.         public static void LogoutUser()
  67.         {
  68.             string userAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  69.             string appDir = Path.Combine(userAppDataDir, "Math4Office");
  70.             string licFileName = Path.Combine(appDir, "token.tok");
  71.             string infoFileName = Path.Combine(appDir, "user.info");
  72.  
  73.             if (File.Exists(licFileName))
  74.             {
  75.                 try
  76.                 {
  77.                     File.Delete(licFileName);
  78.                 }
  79.                 catch
  80.                 {
  81.                     if (File.Exists(licFileName))
  82.                     {
  83.                         using (StreamWriter fs = new StreamWriter(licFileName))
  84.                         {
  85.                             fs.WriteLine("");
  86.                         }
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.  
  92.         private static bool ValidateToken()
  93.         {
  94.             bool result = false;
  95.            
  96.             string userAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  97.             string appDir = Path.Combine(userAppDataDir, "Math4Office");
  98.             string licFileName = Path.Combine(appDir, "token.tok");
  99.  
  100.             if (File.Exists(licFileName))
  101.             {
  102.                 using (StreamReader fs = new StreamReader(licFileName))
  103.                 {
  104.                     string authenticationKey = fs.ReadLine();
  105.                     string validationKey = "AMAAMACmsO3rk5KOGikVV615S52IgdxMMkdswb3e1lmmXDsihncRpqP47NscE8KVcacomy8DAAEAAQ==";
  106.                     license = new CryptoLicense(authenticationKey, validationKey);
  107.  
  108.                     if (license.Status != LicenseStatus.Valid)
  109.                     {
  110.                         result = false;
  111.                     }
  112.                     else
  113.                     {
  114.                         result = true;
  115.                         //Application.Run(new Form1(lic.ExceptionalFeature()));
  116.                     }
  117.  
  118.                 }
  119.                
  120.             }
  121.  
  122.             return result;
  123.         }
  124.  
  125.         public bool Measures()
  126.         {
  127.             if (license.IsFeaturePresent((LicenseFeatures)appFeature.Measures, false))
  128.             {
  129.                 return false;
  130.             }
  131.             else
  132.             {
  133.                 return true;
  134.             }
  135.         }
  136.  
  137.         private static string GenerateToken()
  138.         {
  139.             byte[] time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
  140.             byte[] key = Guid.NewGuid().ToByteArray();
  141.             string token = Convert.ToBase64String(time.Concat(key).ToArray());
  142.             return token;
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement