Ejemplo n.º 1
0
        public async Task HandleRedirect(Func <string, Task> navigateAction)
        {
            Request     = OAuthRequest.BuildLoopbackRequest();
            State.Token = null;
            using (var listener = new HttpListener())
            {
                listener.Prefixes.Add(Request.RedirectUri);
                listener.Start();

                await navigateAction(Request.AuthorizationRequestUri);

                var context = await listener.GetContextAsync();

                string html   = string.Format("<html><body></body></html>");
                var    buffer = Encoding.UTF8.GetBytes(html);
                context.Response.ContentLength64 = buffer.Length;
                using (var stream = context.Response.OutputStream)
                {
                    var responseTask = stream.WriteAsync(buffer, 0, buffer.Length);
                }

                string error = context.Request.QueryString["error"];
                if (error != null)
                {
                    return;
                }

                string state = context.Request.QueryString["state"];
                if (state != Request.State)
                {
                    return;
                }

                string code = context.Request.QueryString["code"];
                _logger.LogInformation("Login successfull");
                ApplyToken(await Request.ExchangeCodeForAccessToken(code));
                _logger.LogDebug("Token information: {@token}", State.Token);
            }
        }