HttpListener in WinUI app doesn't catch params in link

bexolder 41 Reputation points
2025-02-06T09:30:01.5333333+00:00

Hi! I try to get authorization code for Microsoft, using authorization via browser. I use HttpListener for catching redirect with code, but it's catch only redirect without parameters. In browser I saw redirect with code.

My code:

private string localhostCodeKey = "{0}/#code";
public const string RedirectScript = "<html><script type='text/javascript'>function redirect() {document.location.href = '/token?url_with_fragment=' + encodeURIComponent(document.location.href); }</script><body onload='redirect()'/></html>";

NameValueCollection queryParams;    
string code = string.Empty; 
if (args.RedirectUri.Contains("localhost"))
{
    codeKey = string.Format(localhostCodeKey, args.RedirectUri);
}   
else    
{   
    codeKey = string.Format(codeKey, args.RedirectUri);   
}    
string scope = AuthUtilities.FormatMicrosoftScope(args.ScopeType, args.CustomScopes);
scope = HttpUtility.UrlEncode(scope);

string signInUri = $"{Constants.MsAuthUri}" +
                   $"?client_id={args.ClientId}" +
                    "&response_type=code" +
                   $"&redirect_uri={args.RedirectUri}" +
                    "&response_mode=fragment" +
                   $"&scope={scope}";

if (httpListener.IsListening)
{
  httpListener?.Stop();
}
httpListener = new HttpListener();
httpListener.Prefixes.Add(args.RedirectUri + "/");
httpListener.Start();

Launcher.LaunchUriAsync(new Uri(signInUri));

var context = await httpListener.GetContextAsync();
var codeResponse = context.Response;
var buffer = Encoding.UTF8.GetBytes(Constants.RedirectScript);
codeResponse.ContentLength64 = buffer.Length;
var responseOutput = codeResponse.OutputStream;
Task responseTask = responseOutput.WriteAsync(buffer, 0, buffer.Length).ContinueWith((task) =>
{
responseOutput.Close();
httpListener.Stop();
});

queryParams = HttpUtility.ParseQueryString(context.Request.Url.AbsoluteUri);

code = queryParams[codeKey];```

`context.Request.Url.AbsoluteUri` contains only redirect uri. Similar code works fine with Google auth, I don't know what I am doing wrong.
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
822 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.
11,289 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 71,266 Reputation points
    2025-02-06T16:17:33.69+00:00

    It is probably a form post rather than a get. You will need to parse the body.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.