HttpListenerContext Request QueryString

Frederico Regateiro 0 Reputation points
2023-11-22T17:03:18.33+00:00

I've created a sample to do a oauth request toa service.

I'm using a HttpListener to receive the url query string from the redirect_uri, but the redirect_uri that the oauth server sends is like

http://localhost:62941/#access_token=

The # is causing the Request QueryString to be empty.

using var httpListener = new HttpListener();
httpListener.Prefixes.Add("http://localhost:62941/");
httpListener.Start();

//wait for server captures redirect_uri  
HttpListenerContext context = await httpListener.GetContextAsync();

httpListener.Stop();

// the code is null because the QueryString is empty
var code = context.Request.QueryString.Get("access_token");

How can i read the uri from the redirect_uri?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,629 questions
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,496 Reputation points
    2023-11-22T17:28:30.8366667+00:00

    There's a context.Request.Uri.Fragment you can use. You'll just need to split on the = sign.


  2. Hui Liu-MSFT 47,336 Reputation points Microsoft Vendor
    2023-11-24T09:26:30.1933333+00:00

    Hi,@ Frederico Regateiro. Welcome to Microsoft Q&A Forum.

    If you are dealing with the OAuth2.0 implicit flow to handle redirects and capture tokens from URL fragments, you could check out the following.

    In OAuth 2.0 implicit flow, the authorization server usually sends the access token as part of the URL fragment to the client-side JavaScript. Handling OAuth 2.0 implicit flow often involves using a client-side technology, such as a browser, WebView, or embedded browser control, to handle redirects and capture the token from the URL fragment. The server-side component typically doesn't directly capture the access token in this flow.

    Here's a simple example using the WebView2 control in a WPF application:

    You could add the Microsoft.Web.WebView2 NuGet package.

    MainWindow.xaml:

    
        <Grid>
            <wv:WebView2 Name="webView" />
        </Grid>
    

    Handle OAuth Flow in Code-Behind (MainWindow.xaml.cs):

     
    using System;
    using System.Windows;
    using Microsoft.Web.WebView2.Core;
    
    public partial class MainWindow : Window
    {
        private const string AuthorizationUrl = "YOUR_OAUTH_AUTH_URL";
        private bool isAccessTokenReceived = false;
    
        public MainWindow()
        {
            InitializeComponent();
    
            webView.EnsureCoreWebView2Async(null);
    
            webView.Source = new Uri(AuthorizationUrl);
    
            webView.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
        }
    
        private void CoreWebView2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
            if (!isAccessTokenReceived && e.Uri.Fragment.Contains("access_token"))
            {
                var uri = new Uri(e.Uri.AbsoluteUri);
                var accessToken = uri.Fragment.Split('=')[1];
    
                MessageBox.Show($"Access Token: {accessToken}");
    
                isAccessTokenReceived = true;
                Close();
            }
        }
    }
    
    

    Replace "YOUR_OAUTH_AUTH_URL" with the actual OAuth authorization URL.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments