Example #1
0
        public void EnsureAuthenticated()
        {
            var computerInfo = _computerInfoService.GetComputerInfo();

            if (computerInfo.PersistentData.SessionId != null && computerInfo.PersistentData.SecretKey != null)
            {
                // Check if the existing session is still valid.
                var authInfo = _jamHostApiService.ValidateSession();
                if (authInfo.IsValid)
                {
                    // Already authenticated.
                    _userInfo.Authenticated = true;
                    _userInfo.FullName      = authInfo.FullName;
                    _userInfo.Email         = authInfo.EmailAddress;
                    _userInfo.AccountType   = authInfo.AccountType;
                    return;
                }
            }

            AuthForm way = new AuthForm(_siteInfo.GetSiteInfo(), _imageService, _jamHostApiService, _computerInfoService);

            if (way.ShowDialog() != DialogResult.OK)
            {
                Environment.Exit(1);
            }

            _userInfo.Authenticated = true;
            _userInfo.FullName      = way.AuthResult.FullName;
            _userInfo.Email         = way.AuthResult.EmailAddress;
            _userInfo.AccountType   = way.AuthResult.AccountType;

            way.Dispose();
            GC.Collect();
        }
        public AuthInfo Authenticate(string email, string password, string deviceIdentifier)
        {
            var siteInfo = _siteInfoService.GetSiteInfo();

            var url    = siteInfo.Url + @"jamcast/api/authenticate?_domain=" + siteInfo.Id;
            var client = new WebClient();

            dynamic resultParsed;

            try
            {
                var result = client.UploadValues(url, "POST", new NameValueCollection
                {
                    { "email", email },
                    { "password", password }
                });
                resultParsed = JsonConvert.DeserializeObject <dynamic>(Encoding.ASCII.GetString(result));
            }
            catch (WebException c)
            {
                resultParsed = new { has_error = true, error = $"WebException: {c.Message}" };
            }

            var hasError = (bool?)resultParsed.has_error;

            if (hasError.HasValue && hasError.Value)
            {
                return(new AuthInfo
                {
                    IsValid = false,
                    Error = (string)resultParsed.error
                });
            }

            return(new AuthInfo
            {
                IsValid = true,
                FullName = (string)resultParsed.result.fullName,
                EmailAddress = (string)resultParsed.result.email,
                SessionId = (string)resultParsed.result.sessionId,
                SecretKey = (string)resultParsed.result.secretKey,
                AccountType = (string)resultParsed.result.accountType
            });
        }