Example #1
0
        bool TryRegisterUser(WpApp app)
        {
            string key = null;

            if (app.Context.Post.TryGetValue(RegisterKeyName, out var keyvalue))
            {
                key = keyvalue.AsString();
            }
            else if (app.GetOption(RegisterKeyName).IsPhpArray(out var dataarray))
            {
                try
                {
                    var uri  = new Uri(app.GetSiteUrl(), UriKind.Absolute);
                    var data = ValidationData.FromArray(dataarray);
                    if (data.Verify(uri))
                    {
                        _isRegistered = true;
                        AdminNotice(app, $"License is valid until {data.Expiration:d}.", success: true);
                        return(true);
                    }
                    else if (data.Expiration <= DateTime.UtcNow && data.Key != null)
                    {
                        // AdminNotice(app, $"License has expired at {data.Expiration.ToString("d")}.", success: false);

                        // try to update the registration automatically
                        key = data.Key;
                    }
                }
                catch
                {
                    // ignore invalid data
                    AdminNotice(app, $"Invalid license data. Please register again.", success: false);
                }

                _isRegistered = false; // do not try to verify again
            }

            // request user validation:

            if (key != null)
            {
                var siteurl = app.GetSiteUrl();
                var uri     = new Uri(siteurl, UriKind.Absolute);
                if (TryRequestUserValidation(key, siteurl, out var data))
                {
                    app.UpdateOption(RegisterKeyName, data.ToArray());

                    if (data.Verify(uri))
                    {
                        _isRegistered = true;
                        AdminNotice(app, $"Registration suceeded. Thank you! License is valid until {data.Expiration:d}, then it will be renewed automatically.", success: true);
                        return(true);
                    }
                    else
                    {
                        AdminNotice(app, $"License has been verified, although it is no valid. Expiration date is {data.Expiration:d}.", success: false);
                    }
                }
                else
                {
                    AdminNotice(app, $"Invalid license '{key}'. Domain '{uri.Host}' has not been registered.", success: false);
                }
            }

            return(false);
        }