Exemple #1
0
        // For basic authentication
        public HttpResponseMessage Login([FromUri] string username, [FromUri] string password)
        {
            try
            {
                bool getLogin = LoginSet.UsernamePassword(username, password);
                if (!getLogin)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, "Invalid username / password!"));
                }

                var AccountUser = new AccountUser()
                {
                    Username = username,
                    Password = password,
                };
                var jsonString = JsonConvert.SerializeObject(AccountUser);
                var token      = StringCrypter.Encrypt(jsonString, password);
                return(Request.CreateResponse(HttpStatusCode.OK, token));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "There error :" + ex.Message));
            }
        }
 public static bool Validate(IWin32Window owner, string applicationType, string applicationName, string fileName)
 {
     try {
         if (!File.Exists(fileName))
         {
             MessageBox.Show(owner, string.Format("File not found."), string.IsNullOrEmpty(applicationName) ? applicationName : DefaultApplicationInfo.ShortApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
             return(false);
         }
         AssemblyName inspectedAssemblyName = AssemblyName.GetAssemblyName(fileName);
         if (inspectedAssemblyName == null)
         {
             return(false);
         }
         if (inspectedAssemblyName.GetPublicKey().Length == 0)
         {
             return(false);
         }
         string publicKeyToken = Regex.Matches(Assembly.GetExecutingAssembly().FullName, @"PublicKeyToken=(?<tokenValue>(\w|\d)+)", RegexOptions.IgnoreCase).Cast <Match>().First().Value;
         if (!inspectedAssemblyName.FullName.Contains(publicKeyToken))
         {
             MessageBox.Show(owner, string.Format("License Error: Different PublicKeyToken."), string.IsNullOrEmpty(applicationName) ? applicationName : DefaultApplicationInfo.ShortApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
             return(false);
         }
         string licenseFile       = string.Empty;
         string userInfo          = string.Empty;
         string userInfoFile      = Path.ChangeExtension(fileName, "Config.User");
         bool   createNewUserInfo = !File.Exists(userInfoFile);
         if (createNewUserInfo)
         {
             using (LicenseValidatorForm form = new LicenseValidatorForm(applicationType, applicationName, fileName)) {
                 try {
                     form.ShowDialog(owner);
                     userInfo    = form.GetValue();
                     licenseFile = form.GetLicenseFile();
                 } catch {
                     throw;
                 }
             }
         }
         else
         {
             using (StreamReader reader = new StreamReader(userInfoFile)) {
                 userInfo = reader.ReadToEnd();
                 if (string.IsNullOrEmpty(userInfo))
                 {
                     MessageBox.Show(owner, string.Format("Failed to validate the license."), string.IsNullOrEmpty(applicationName) ? applicationName : DefaultApplicationInfo.ShortApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                     return(false);
                 }
                 try {
                     userInfo = StringCrypter.Decrypt(userInfo);
                 } catch {
                     throw;
                 }
             }
         }
         if (licenseFile.Length == 0)
         {
             licenseFile = Path.ChangeExtension(fileName, "Config.License");
         }
         if (!File.Exists(licenseFile))
         {
             MessageBox.Show(owner, string.Format("Lincense file not found."), string.IsNullOrEmpty(applicationName) ? applicationName : DefaultApplicationInfo.ShortApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
             return(false);
         }
         string license = string.Empty;
         using (StreamReader reader = new StreamReader(licenseFile)) {
             license = reader.ReadToEnd();
             if (string.IsNullOrEmpty(license))
             {
                 MessageBox.Show(owner, string.Format("Fail to validate the license."), string.IsNullOrEmpty(applicationName) ? applicationName : DefaultApplicationInfo.ShortApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return(false);
             }
             try {
                 license = StringCrypter.Decrypt(license);
             } catch {
                 throw;
             }
         }
         try {
             ValidateInternal(userInfo, license);
         } catch {
             throw;
         }
         if (createNewUserInfo)
         {
             using (StreamWriter writer = new StreamWriter(userInfoFile)) {
                 userInfo = StringCrypter.Encrypt(userInfo);
                 writer.Write(userInfo);
                 writer.Flush();
             }
         }
         return(true);
     } catch {
         return(false);
     }
 }