https://github.com/googlesamples/oauth-apps-for-windows
,下面是我修改后的代码
string redirectURI = "http://127.0.0.1:55949/";
var http = new HttpListener();
http.Prefixes.Add(redirectURI);
http.Start();
string state = randomDataBase64url(32);
string code_verifier = randomDataBase64url(32);
string code_challenge = base64urlencodeNoPadding(sha256(code_verifier));
const string code_challenge_method = "S256";
string authorizationRequest = string.Format("{0}?response_type=code&scope=openid%20profile&redirect_uri={1}&client_id={2}&state={3}&code_challenge={4}&code_challenge_method={5}",
authorizationEndpoint,
System.Uri.EscapeDataString(redirectURI),
clientID,
state,
code_challenge,
code_challenge_method);
System.Diagnostics.Process.Start(authorizationRequest);
var context = await http.GetContextAsync();
this.Activate();
与goolgle的示例相比,我硬编码了重定向URL和端口,如果authorizationRequest没有正确响应(例如直接关闭web浏览器),则不会调用重定向URI。HttpListener将始终保持侦听。下次我再次调用这些代码时,将引发异常:
Failed to listen on prefix 'http://127.0.0.1:55949/' because it conflicts with an existing registration on the machine.
我应该如何修改代码以在长时间等待后停止HttpListener,或者在下次调用这些函数时重新启动HttpListener。