Example #1
0
        public async Task <IActionResult> TwitchReturnUrlAction(string code, string scope, string state)
        {
            var    client = new HttpClient();
            Stream stream = _context.StreamModels.Where(sm => state.Contains(sm.Secret)).FirstOrDefault();

            if (stream != null)
            {
                var    baseUrl = _configuration.GetSection("WebServerWebAddress").Value;
                string url     = $"https://id.twitch.tv/oauth2/token?client_id={stream.ClientID}&client_secret={stream.Secret}&code={code}&grant_type=authorization_code&redirect_uri={baseUrl}/Stream/TwitchReturnUrlAction";

                var response = await client.PostAsync(url, new StringContent("", System.Text.Encoding.UTF8, "text/plain"));

                var responsestring = await response.Content.ReadAsStringAsync();

                JSONObjects.TwitchAuthToken authtoken = JsonConvert.DeserializeObject <JSONObjects.TwitchAuthToken>(responsestring);
                stream.AccessToken  = authtoken.access_token;
                stream.RefreshToken = authtoken.refresh_token;

                await _context.SaveChangesAsync();

                StatusMessage = "Stream Oauth complete";
            }

            return(RedirectToAction(nameof(Verwaltung)));
        }
        public async Task <IActionResult> TwitchReturnUrlAction(string code, string scope, string state)
        {
            var savedtoken = _context.SecurityTokens.Where(st => st.service == TokenType.Twitch).FirstOrDefault();

            savedtoken.code = code;
            var    client   = new HttpClient();
            string baseUrl  = _configuration.GetValue <string>("WebServerWebAddress");
            string url      = $"https://id.twitch.tv/oauth2/token?client_id={savedtoken.ClientID}&client_secret={savedtoken.secret}&code={code}&grant_type=authorization_code&redirect_uri={baseUrl}/Admin/TwitchReturnUrlAction";
            var    response = await client.PostAsync(url, new StringContent("", System.Text.Encoding.UTF8, "text/plain"));

            var responsestring = await response.Content.ReadAsStringAsync();

            JSONObjects.TwitchAuthToken authtoken = JsonConvert.DeserializeObject <JSONObjects.TwitchAuthToken>(responsestring);
            savedtoken.token        = authtoken.access_token;
            savedtoken.RefreshToken = authtoken.refresh_token;
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(SecurityTokens)));
        }