无法收听前缀' http://127.0.0.1:55949 /'因为它与机器上的现有注册冲突

时间:2018-05-30 01:27:40

标签: c# httplistener

我正在学习OAuth,并从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浏览器),则不会调用redirectURI。 HttpListener将一直保持倾听。下次再次调用这些代码时,将抛出异常:

Failed to listen on prefix 'http://127.0.0.1:55949/' because it conflicts with an existing registration on the machine.

如何在长时间等待后修改代码以停止HttpListener,或者在下次调用这些函数时重新启动HttpListener。

0 个答案:

没有答案